动态添加按钮到我的表单 [英] add buttons dynamically to my form

查看:104
本文介绍了动态添加按钮到我的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我需要在数据库中的一些enregestrement中动态添加按钮到我的表单我想知道如何将事件添加到这些按钮

我我在VS 2005 VB

谢谢

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

推荐答案

添加一个按钮:

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



如果你使用C#v.2,lambda表格不可用,但仍然使用匿名方法使用较旧的语法,不使用类型推断


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









在VB中.NET,添加按钮是一样的,但添加处理程序的语法却截然不同:





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





很抱歉有关C#与VB.NET代码的混淆(由于CPallini已解决)但是我必须注意,如果你想获得帮助或者在.NET中使用其他人的工作,你至少需要了解一些C#。与VB.NET相比,更多更好的质量帮助来自C#,这也是一种标准化语言。



-SA


例如,请参阅:动态按钮控件事件处理WinForms Windows窗体 [ ^ ]
See, for instance: "Dynamic Buttons Controls Event Handling WinForms Windows Forms"[^]


查看此视频,我没有看到它,但名称看​​起来很有希望。

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


这篇关于动态添加按钮到我的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆