如何code一个按钮的Click事件? [英] How to code a Button's Click Event?
问题描述
我使用的WinForm和fb.net。
有人可以为我提供了如何创建一个按钮单击事件的例子吗?
我有
暗淡但作为windows.forms.button
but.name
but.text
but.location
等等
但我怎么做我创建的点击和它背后的code?
I'm using winform and fb.net. Can someone provide me with an example of how to create a buttons click event? I have dim but as windows.forms.button but.name but.text but.location etc. but I how do I create the Click and the code behind it?
推荐答案
您可以使用:
AddHandler button.Click, AddressOf HandlerMethod
在VB中,你可以指定一个方法处理特定控件的特定事件,如果你不是在飞行创造它 - 你只需要的AddHandler
当你重(说)动态填充表格一组按钮。
In VB you can specify that a method handles a particular event for a particular control, if you're not creating it on the fly - you only need AddHandler
when you're (say) dynamically populating a form with a set of buttons.
下面是一个简短但完整的例子:
Here's a short but complete example:
Imports System.Windows.Forms
Public Class Test
<STAThread>
Public Shared Sub Main()
Dim f As New Form()
Dim b As New Button()
b.Text = "Click me!"
AddHandler b.Click, AddressOf ClickHandler
f.Controls.Add(b)
Application.Run(f)
End Sub
Private Shared Sub ClickHandler(sender As Object, e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
b.Text = "Clicked"
End Sub
End Class
编辑:要关闭的形式,最简单的办法是让原始的控制形式:
To close the form, the simplest way is to get the form of the originating control:
Private Shared Sub ClickHandler(sender As Object, e As EventArgs)
Dim c As Control = DirectCast(sender, Control)
Dim f as Form = c.FindForm()
f.Close()
End Sub
这篇关于如何code一个按钮的Click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!