Thanks Jeffrey
works fine!
have a good time!
jahrens
namespace NamingConventionExtention
{
using System;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("62A2C7D0-DB40-495C-A5CC-CE9A5022A4C0"),
ProgId("NamingConventionExtention.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private _DTE applicationObject;
private AddIn addInInstance;
private EnvDTE.WindowEvents windowEvents;
private Window activeWindow;
private IDesignerHost designerHost;
private Hashtable componentChangeServices = new Hashtable();
private DataSet ds;
/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
try
{
windowEvents = applicationObject.Events.get_WindowEvents(null);
windowEvents.WindowActivated += new
_dispWindowEvents_WindowActivatedEventHandler(windowEvents_WindowActivated);
windowEvents.WindowClosing += new
_dispWindowEvents_WindowClosingEventHandler(windowEvents_WindowClosing);
windowEvents.WindowCreated +=new
_dispWindowEvents_WindowCreatedEventHandler(windowEvents_WindowCreated);
ds = new DataSet();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
+ "\\NamingConventions.config");
ds.ReadXml(doc.SelectSingleNode("/configuration//appSettings//add[@key=\"NamingConventionsPath\"]//@value").Value);
ds.Tables["NamingConventions"].PrimaryKey = new
DataColumn[]{ds.Tables["NamingConventions"].Columns["Namespace"]};
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode,
ref System.Array custom)
{
}
/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}
/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
}
/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
}
private void windowEvents_WindowActivated(Window GotFocus, Window LostFocus)
{
HandleActiveWindowChange(GotFocus);
}
private void windowEvents_WindowClosing(Window Window)
{
HandleActiveWindowChange(null);
}
private void windowEvents_WindowCreated(Window Window)
{
HandleActiveWindowChange(Window);
}
private void HandleActiveWindowChange(Window newActiveWindow)
{
activeWindow = newActiveWindow;
designerHost = null;
if(activeWindow != null &&
!componentChangeServices.ContainsKey(activeWindow))
{
designerHost = activeWindow.Object as IDesignerHost;
if(designerHost == null) return;
IComponentChangeService componentChangeService =
designerHost.GetService(typeof(IComponentChangeService)) as
IComponentChangeService;
if(componentChangeService == null) return;
componentChangeService.ComponentAdded += new
ComponentEventHandler(componentChangeService_ComponentAdded);
componentChangeServices.Add(activeWindow, null);
}
}
private void componentChangeService_ComponentAdded(object sender,
ComponentEventArgs e)
{
try
{
int i = 1;
string type = e.Component.GetType().ToString();
DataRow row = ds.Tables["NamingConventions"].Rows.Find(type);
if(row == null) return;
string prefix = row["Prefix"].ToString();
if(e.Component.Site.Container.Components[prefix] == null)
{
e.Component.Site.Name = prefix;
return;
}
string name = prefix + i;
while(e.Component.Site.Container.Components[name] != null)
{
i++;
name = prefix + i;
}
e.Component.Site.Name = name;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
"Jeffrey Tan[MSFT]" - 31 Mar 2006 02:46 GMT
You are welcome
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.