I want to develop an IDE using .NET (VB.NET or C#) for which I need to create the form and controls at runtime that can fire events. I can create the controls, but how do I create a form that responds to events at runtime? Can I do it in .NET or do I have to do it in VC++? If you want to attach at runtime some handlers to specific events fired from the controls, you have to use reflection. First, you get a reference to the EventInfo...
object for the event you want to "catch":
System.Windows.Forms.ListBox myListBox = new System.Windows.Forms.ListBox(); EventInfo ei = typeof(System.Windows.Forms.ListBox).GetEvent ("SelectedIndexChanged");
Next, you can use the AddHandler to attach the method you want to it, provided the signatures match, of course:
ei.AddEventHandler(myListBox, new EventHandler(OnSelectedIndexChanged));
Dig deeper on C# programming language
Related Q&A from Daniel Cazzulino
Here Daniel Cazzulino explains how to load a DSL (domain specific language) domain model instance file programmatically. This requires the .NET type ...continue reading
Here we offer a glimpse at 12 of .NET development expert Danny Cazzulino's top ASP.NET questions and answers.continue reading
C# developers should NOT be modifying InitializeComponent method in the code-behind (or any of the variable definitions) by hand.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.