Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: buttons VB.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 11:13am
Rate this: bad
good
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
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  
v4
Comments
CPallini at 13-Dec-12 16:48pm
   
Please note the OP is not asking of C#.
By the way, my 5.
Sergey Alexandrovich Kryukov at 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 at 13-Dec-12 17:02pm
   
Answer update, you help is credited.
Thank you again.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check this video out, i haven't seen it but the name looks promising.
http://www.youtube.com/watch?v=hkNySHGdt2E[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
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