martedì 31 luglio 2007

Links

Catturare eventi
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=140862&SiteID=1
http://www.codeguru.com/forum/showthread.php?threadid=336968
http://www.dotnet247.com/247reference/msgs/37/186291.aspx
http://www.mycsharp.de/wbb2/thread.php?goto=nextoldest&threadid=35424&sid=762265119e031fe804b76e38e9ae8e04
http://www.mztools.com/articles/2005/MZ006.htm

http://www.codeproject.com/dotnet/CodeDepot.asp
http://msdn2.microsoft.com/en-us/library/bb166005(VS.80).aspx

Frequently Asked Questions About Visual Studio .NET Automation

http://msdn2.microsoft.com/en-us/library/aa290364(VS.71).aspx

Handling Language-Specific Project Events

Once you have set up references to language-specific project objects, you will then most likely want to handle their events. Doing this is basically a two-step process: first declare the event handler and then add the event handler itself to your code.

As I previously mentioned, the language-specific event objects are late-bound, and thus, not visible in the IDE unless you use the Extensibility Browser. Here is a list of the late-bound event objects (for DTE.Events):

  • CodeModelEvents
  • CSharpBuildManagerEvents
  • CSharpReferencesEvents
  • CSharpProjectItemsEvents
  • CSharpProjectsEvents
  • DebuggerEvents
  • VBBuildManagerEvents
  • VBImportsEvents
  • VBProjectItemsEvents
  • VBProjectsEvents
  • VBReferencesEvents
  • VCProjectEngineEventsObject

The following procedure demonstrates how to handle the three Visual Basic .NET project item events: ItemAdded, ItemRemoved, and ItemRenamed. It is followed by code that demonstrates how to do the same thing in Visual C# .NET.

// Visual C# .NET
namespace CSharpEventHandler
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
using System.Windows.Forms;

[GuidAttribute("9BBE7601-DBC7-48F7-8AC3-EB415380D205"),
ProgId("CSharpEventHandler.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;

MessageBox.Show("Connecting add-in");
try
{
CSharpProjectItemsEvents = (EnvDTE.ProjectItemsEvents)
applicationObject.Events.GetObject
("CSharpProjectItemsEvents");
CSharpProjectItemsEvents.ItemAdded += new
_dispProjectItemsEvents_ItemAddedEventHandler
(CSharpProjectItemAdded);
CSharpProjectItemsEvents.ItemRemoved += new
_dispProjectItemsEvents_ItemRemovedEventHandler
(CSharpProjectItemRemoved);
CSharpProjectItemsEvents.ItemRenamed += new
_dispProjectItemsEvents_ItemRenamedEventHandler
(CSharpProjectItemRenamed);
}
catch(System.Exception)
{
}
}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
MessageBox.Show("Disconnecting add-in");
if(CSharpProjectItemsEvents != null)
{
CSharpProjectItemsEvents.ItemAdded -= new
_dispProjectItemsEvents_ItemAddedEventHandler
(CSharpProjectItemAdded);
CSharpProjectItemsEvents.ItemRemoved -= new
_dispProjectItemsEvents_ItemRemovedEventHandler
(CSharpProjectItemRemoved);
CSharpProjectItemsEvents.ItemRenamed -= new
_dispProjectItemsEvents_ItemRenamedEventHandler
(CSharpProjectItemRenamed);
}
}

public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
}

public void OnBeginShutdown(ref System.Array custom)
{
}

private void CSharpProjectItemAdded(EnvDTE.ProjectItem ProjectItem)
{
MessageBox.Show("Item added");
}

private void CSharpProjectItemRemoved(EnvDTE.ProjectItem
ProjectItem)
{
MessageBox.Show("Item removed");
}

private void CSharpProjectItemRenamed(EnvDTE.ProjectItem
ProjectItem, string OldName)
{
MessageBox.Show("Item renamed");
}

private _DTE applicationObject;
private AddIn addInInstance;
private EnvDTE.ProjectItemsEvents CSharpProjectItemsEvents;
}
}