|
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.- Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, February 26, 2009 4:59 AM
-
| | nobugz Wednesday, February 25, 2009 1:55 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.
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." - Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, February 26, 2009 4:59 AM
-
| | 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 - Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, February 26, 2009 4:59 AM
-
| | 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.- Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, February 26, 2009 4:59 AM
-
| | nobugz Wednesday, February 25, 2009 1:55 AM |
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 |
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." - Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, February 26, 2009 4:59 AM
-
| | 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 - Marked As Answer byBruce.ZhouMSFT, ModeratorThursday, February 26, 2009 4:59 AM
-
| | RMedeiros Wednesday, February 25, 2009 8:13 PM |
|