Click here to Skip to main content
Sign Up to vote bad
good
See more: buttonsVB.NET
Hello
I need to add buttons dynamically to my form in a number of enregestrement in a database I want to know how to add the event to these buttons
I'm on VS 2005 VB
thank you
Posted 13-Dec-12 10:13am


3 solutions

  Permalink  
Comments
Sergey Alexandrovich Kryukov - 13-Dec-12 16:35pm
My 4 this time. It's interesting to note, that "NewEventHandler" is a total bogus, but it sneaks from project to project. Anonymous method should always be considered or at least mentioned. Please see my answer. --SA
CPallini - 13-Dec-12 16:46pm
I don't agree: while I appreciate the convenience of anonymous methods, 'is a total bogus' look a bit excessive to me.
Sergey Alexandrovich Kryukov - 13-Dec-12 16:51pm
OK, perhaps phrased it not accurately. (How could you think that I'm going to say such a stupid thing? This is not what I meant. :-) It's a total bogus not because of anonymous alternative, not at all. The named handlers are very important! I'm only talking about bogus form of the syntax: btnDynamic.Click += new EventHandler(this.DynamicButtonClick); is strictly equivalent to the clear btnDynamic.Click += this.DynamicButtonClick; And to me, writing redundant words is crime. Do you see the point now? —SA
Check this video out, i haven't seen it but the name looks promising.
http://www.youtube.com/watch?v=hkNySHGdt2E[^]
  Permalink  
Add a button:
Button myButton = new Button();
 
//...

myButton.Parent = someParent; // where someParent is some container control like Panel or Form
Same as
someParent.Controls.Add(myButton);
Now, I don't think your question was how to add event. You can only do it in your own class. You probably need to know how to add an event handler to the invocation list of the existing event instance. This is how:
myButton.Click += (sender, eventArgs) => { // arguments type are inferred from event type
    DoSomethingOnClick();
};
In case you are using C# v.2, lambda form is not available, but still, use the anonymous method using older syntax, without type inference:
myButton.Click += delegate(object sender, System.EventArgs eventArgs) { // arguments types are required
    DoSomethingOnClick();
};
 
[EDIT]
 
In VB.NET, adding a button is the same, but adding a handler has very different syntax:
AddHandler myBitton.Click, AddressOf SomeClickHandlerMethod
 
Anonymous form:
AddHandler myBitton.Click, Sub()
    DoSomethingOnClick()
EndSub
 
Sorry for the confusion about C# vs VB.NET code (resolved thanks to CPallini), but I must note that you need to understand at least some C# if you want to get help or use the work of others in .NET. More of better quality help comes in C#, which is also a standardized language, in contrast to VB.NET.
 
—SA
  Permalink  
Comments
CPallini - 13-Dec-12 16:48pm
Please note the OP is not asking of C#. By the way, my 5.
Sergey Alexandrovich Kryukov - 13-Dec-12 16:55pm
Oh, my fault. Again. :-) I'll add VB.NET form of adding handler... Thank you, Carlo. —SA
Sergey Alexandrovich Kryukov - 13-Dec-12 17:02pm
Answer update, you help is credited. Thank you again. —SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 577
1 Ron Beyer 341
2 Tadit Dash 243
3 samadhan_kshirsagar 229
4 OriginalGriff 201
0 Sergey Alexandrovich Kryukov 7,061
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,370
4 CPallini 3,034


Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid