且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态添加按钮到我的表单

更新时间:2022-12-30 12:34:22

添加一个按钮:

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.***.com/watch?v=hkNySHGdt2E [ ^ ]
Check this video out, i haven''t seen it but the name looks promising.
http://www.***.com/watch?v=hkNySHGdt2E[^]