Click here to Skip to main content
11,925,022 members (51,254 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB buttons VB.NET
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 11:13am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Add a button:
Button myButton = new Button();

myButton.Parent = someParent; // where someParent is some container control like Panel or Form
Same as
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
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


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()

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.

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.
Sergey Alexandrovich Kryukov 13-Dec-12 17:02pm
Answer update, you help is credited.
Thank you again.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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?

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Check this video out, i haven't seen it but the name looks promising.[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151125.3 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100