vb.net给控件加事件两种方法:
1. 用WithEvents声明变量vb.net新增事件,这样vb.net新增事件你就可以像对待拖上去的控件一样操作:
Public Class Form3
WithEvents btn As New Button
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
MsgBox(Now)
End Sub
End Class
2. 使用AddHandler 语句:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim btn As New Button
AddHandler btn.Click, AddressOf btn_Click
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
MsgBox(Now)
End Sub
End Class
WithEvents (Visual Basic):
AddHandler 语句:
如何在 vb.net 为动态生成的控件添加事件并传递数据'把图片的声明放在外面,类里面过程外面,而且要用As,不然等会儿没法用
Dim myPicture As New System.Windows.Forms.PictureBox()
'动态生成的控件,加一行
Private Sub UserControl1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Panel3.Controls.Add(myPicture)
myPicture.Size =New System.Drawing.Size(115, 160)
myPicture.TabStop =False
myPicture.Name ="p"
myPicture.Cursor = Cursors.Hand
AddHandler myPicture.Click, AddressOf mypic_Click '添加事件
AddHandler returnid, AddressOf idReturned '绑定事件和过程
End Sub
'再来个事件 , 放在声明的地方,就是类里面、过程外面
Private Event returnid(ByVal id As String) '这个事件可以传递值哦~
'普通的单击事件
Private Sub mypic_Click()
RaiseEvent returnid(myPicture.Name)
End Sub
'会传值的事件过程
Private Sub idReturned(ByVal id As String)
Dim bookInfo As New BookInfo
bookInfo.Show()
Me.Parent.Enabled = False
'用id变量做点什么吧
End Sub
如果要改事件的签名(参数一类的)的话,得重写控件 。你可以这样自己弄一个事件 。
在VB.net中,怎么在类中添加事件首先vb.net新增事件: textbox里没有显示vb.net新增事件,是因为SerialPort1和TextBox2不是同一线程创建vb.net新增事件的,需要跨线程操作 。需要用到委托 , 这样才能显示出来 。其次: vb.net新增事件我觉得用串口的接收数据事件更好一些 。
vb.net动态菜单,如何添加事件?动态生成的控件:
Private Sub UserControl1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myPicture = New System.Windows.Forms.PictureBox()
Me.Panel3.Controls.Add(myPicture)
myPicture.Size = New System.Drawing.Size(115, 160)
myPicture.TabStop = False
myPicture.Name = "p"
myPicture.Cursor = Cursors.Hand
AddHandler myPicture.Click, AddressOf mypic_Click '添加事件
End Sub
处理事件的过程:
Private Sub mypic_Click() 'ByVal id As String
Dim bookInfo As New BookInfo
bookInfo.Show()
Me.Parent.Enabled = False
vb.net 动态菜单如何在指定位置添加新项,并添加单击事件?常见事件有鼠的标单击、双击、移动;
有键盘的键按下、键松开、按键;
有窗体加载、窗体卸载等等 。
【vb.net新增事件的简单介绍】关于vb.net新增事件和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- python爬虫代码怎么懂,python爬虫基础代码
- vb.netxml配置文件的简单介绍
- 冒险类游戏ios,冒险类游戏有哪些
- 监控硬盘坏了怎么重装系统,监控硬盘格式化后数据还能恢复吗
- linux释放空间命令,释放linux cache
- 华为荣耀x1怎么升级鸿蒙系统,华为荣耀x1怎么升级鸿蒙系统30
- go语言binary包 go语言的包
- 拍摄蔷薇花带什么镜头,拍摄蔷薇花带什么镜头好
- 玩电脑用什么cpu好,电脑什么处理器玩游戏性价比高