Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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

//...

myButton.Parent = someParent; // where someParent is some container control like Panel or Form
Same as
C#
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:
C#
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:
C#
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:
VB
AddHandler myBitton.Click, AddressOf SomeClickHandlerMethod


Anonymous form:
VB
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
 
Share this answer
 
v4
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
 
Share this answer
 
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900