locked
How can I access WinForms designer events? RRS feed

  • Question

  • Sorry if this is a bit of a noob question, but I just started playing around with the extensibility model in VS2008.

    I had an idea for an add-in which could assist with automating a necessary drudge task.  For it to work optimally, it needs to code generate a method to the designer.cs file at the same time the designer writes the InitializeComponent() method, presumably when a control is added to a form or when it is removed.

    Since I am new to this, I am not entirely sure what is and isn't supported and my searches haven't turned up much.

    Does such an event exist?  If so, where might I find the beast?

    I really just need a point in the right direction here, I'm solid on the code generation stuff.

    Thursday, April 1, 2010 7:01 PM

Answers

  • See if these articles help. They show you how to get the IDesignerHost, the PropertyDescriptors and the services to get component changes, event bindings, etc.

    HOWTO: Manipulating controls of Windows forms from Visual Studio .NET add-ins.
    http://www.mztools.com/resources_vsnet_addins.aspx

    HOWTO: Add an event handler from a Visual Studio add-in
    (same link)

     


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Proposed as answer by Carlos J. Quintero Friday, April 2, 2010 11:42 AM
    • Marked as answer by CS001 Monday, April 12, 2010 7:04 PM
    • Marked as answer by CS001 Monday, April 12, 2010 7:05 PM
    Friday, April 2, 2010 11:42 AM

All replies

  • Hi CS001,

    I think IComponentChangeService should be what you are looking for.

    I have the following code working in Add-In

    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
    	handled = false;
    	if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    	{
    		if(commandName == "CSVSAddInWindowsFormDesigner.Connect.CSVSAddInWindowsFormDesigner")
    		{
    
                Window window =
                    _applicationObject.ActiveDocument.ActiveWindow;
                IDesignerHost host = window.Object as IDesignerHost;
                IServiceProvider provider = host as IServiceProvider;
    
                IComponentChangeService service =
                    provider.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
    
                service.ComponentAdded += new ComponentEventHandler(service_ComponentAdded);
                service.ComponentRemoved += new ComponentEventHandler(service_ComponentRemoved);
                                   
    
    			handled = true;
    			return;
    		}
    	}
    }
    
    void service_ComponentRemoved(object sender, ComponentEventArgs e)
    {
    }
    
    void service_ComponentAdded(object sender, ComponentEventArgs e)
    {
    }

     


    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    See what's happening in MSDN forum? Follow us at Twitter.
    Friday, April 2, 2010 7:00 AM
  • See if these articles help. They show you how to get the IDesignerHost, the PropertyDescriptors and the services to get component changes, event bindings, etc.

    HOWTO: Manipulating controls of Windows forms from Visual Studio .NET add-ins.
    http://www.mztools.com/resources_vsnet_addins.aspx

    HOWTO: Add an event handler from a Visual Studio add-in
    (same link)

     


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Proposed as answer by Carlos J. Quintero Friday, April 2, 2010 11:42 AM
    • Marked as answer by CS001 Monday, April 12, 2010 7:04 PM
    • Marked as answer by CS001 Monday, April 12, 2010 7:05 PM
    Friday, April 2, 2010 11:42 AM