Windows Develop Bookmark and Share   
 index > Windows Forms General > Dynamic event handler for dynamic button
 

Dynamic event handler for dynamic button

Hi,

I have dynamic buttoncreated according to the requirements... Iam not sure how do I create dynamic event handlers for the buttons? Is there any examplewhere I can get help from?

For example:

If a button with text Label name is created. I would like to create a event handler such that when the user clicks on labelname button itshould display a messagebox with the label name.

Is this possible? ifso how?

Awaiting your response!
Thanks!
Raamkum  Wednesday, February 25, 2009 1:05 AM
First do it with the designer. Then open the node next to your form in Solution Explorer and double-click the Designer.cs file. Check out the code that was generated by the designer. That's the code you'll need to write.
Hans Passant.
nobugz  Wednesday, February 25, 2009 1:55 AM
nobugz said:

First do it with the designer. Then open the node next to your form in Solution Explorer and double-click the Designer.cs file. Check out the code that was generated by the designer. That's the code you'll need to write.


Hans Passant.



That is how to do it programmatically, which is best. This control will be quite annoying.

namespaceStatePatternDemo
{
classMyButton:Button
{
publicMyButton()
{
this.Click+=newEventHandler(MyButton_Click);
return;
}
voidMyButton_Click(objectsender,EventArgse)
{
MessageBox.Show(this.Name);
}
}
}

The only good use for this control is its' novelty.

Rudedog =8^D


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Wednesday, February 25, 2009 6:40 PM
If you don't want to create a class, like Rudedog2 suggested, you can use the following approach:

privateButtonCreateButton()
{
ButtonmyButton=newSystem.Windows.Forms.Button();
myButton.Location=newSystem.Drawing.Point(5,5);
myButton.Name="buttonName";
myButton.Size=newSystem.Drawing.Size(75,23);
myButton.TabIndex=1;
myButton.Text="buttonText";
myButton.UseVisualStyleBackColor=true;
//Twooptionshere
//FirstoneisusingDelegate
myButton.Click+=delegate(objectsender,EventArgse)
{
MessageBox.Show(myButton.Text);
};
//Secondoneisusinglambdaexpression
myButton.Click+=((sender,e)=>MessageBox.Show(myButton.Text));
Controls.Add(myButton);
returnmyButton;
}

I hope this helps, Rafael Medeiros
RMedeiros  Wednesday, February 25, 2009 8:13 PM
Custom control inherited from Button class.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Wednesday, February 25, 2009 1:17 AM
First do it with the designer. Then open the node next to your form in Solution Explorer and double-click the Designer.cs file. Check out the code that was generated by the designer. That's the code you'll need to write.
Hans Passant.
nobugz  Wednesday, February 25, 2009 1:55 AM
Rudedog2 said:

Custom control inherited from Button class.


Mark the best replies as answers. "Fooling computers since 1971."



can you please give me an example?
Raamkum  Wednesday, February 25, 2009 2:54 AM
nobugz said:

First do it with the designer. Then open the node next to your form in Solution Explorer and double-click the Designer.cs file. Check out the code that was generated by the designer. That's the code you'll need to write.


Hans Passant.



That is how to do it programmatically, which is best. This control will be quite annoying.

namespaceStatePatternDemo
{
classMyButton:Button
{
publicMyButton()
{
this.Click+=newEventHandler(MyButton_Click);
return;
}
voidMyButton_Click(objectsender,EventArgse)
{
MessageBox.Show(this.Name);
}
}
}

The only good use for this control is its' novelty.

Rudedog =8^D


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Wednesday, February 25, 2009 6:40 PM
If you don't want to create a class, like Rudedog2 suggested, you can use the following approach:

privateButtonCreateButton()
{
ButtonmyButton=newSystem.Windows.Forms.Button();
myButton.Location=newSystem.Drawing.Point(5,5);
myButton.Name="buttonName";
myButton.Size=newSystem.Drawing.Size(75,23);
myButton.TabIndex=1;
myButton.Text="buttonText";
myButton.UseVisualStyleBackColor=true;
//Twooptionshere
//FirstoneisusingDelegate
myButton.Click+=delegate(objectsender,EventArgse)
{
MessageBox.Show(myButton.Text);
};
//Secondoneisusinglambdaexpression
myButton.Click+=((sender,e)=>MessageBox.Show(myButton.Text));
Controls.Add(myButton);
returnmyButton;
}

I hope this helps, Rafael Medeiros
RMedeiros  Wednesday, February 25, 2009 8:13 PM

You can use google to search for other answers

Custom Search

More Threads

• How set CurrentUICulture in the backgroundworker thread?
• OOP Concept
• Data extraction from online websites
• Scrollbar issue (.NET 2.0, C#)
• treeview focus trigers AfterSelect on first node
• not able to locate line of code geberating the pb
• Coding a font/charset viewer
• Need help with keydown, arrow keys, repeat and multiple controls
• Large text files on textbox\richtextbox
• how to add in a legend to be display in my diagram using c#