重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
.....
成都创新互联-专业网站定制、快速模板网站建设、高性价比玉州网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式玉州网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖玉州地区。费用合理售后完善,10多年实体公司更值得信赖。
Px += 0
Py += 20
AddHandler pic(i).Click, AddressOf Pic_Click '添加这一句,绑定单击图片框事件
Next
FlowLayoutPanel1.Controls.AddRange(pic)
End Sub
Dim l As Point, s As Size, f As Boolean
Private Sub Pic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pic As PictureBox = CType(sender, PictureBox)
If f Then
pic.Location = l
pic.Size = s
f = False
Else
l = pic.Location
s = pic.Size
pic.BringToFront()
pic.Location = FlowLayoutPanel1.Location
pic.Size = FlowLayoutPanel1.Size
f = True
End If
End Sub
动态生成的控件:
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
'把图片的声明放在外面,类里面过程外面,而且要用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
如果要改事件的签名(参数一类的)的话,得重写控件。你可以这样自己弄一个事件。
执行键盘事件方法如下:
第一步,点击VS工具。
第二步,打开后,新建一个Windows窗体应用程序。
第三步,请看右下方Form的属性栏。
第四步,双击KeyDown。
第五步,输入提示按键代码,如图所示。
第六步,再次输入代码,设置属性KeyPreview为true。
第七步,运行程序,按下举例F5,则会弹出提示,表明已经成功获。
两种方法:
1. 用WithEvents声明变量,这样你就可以像对待拖上去的控件一样操作:
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 语句:
常见事件有鼠的标单击、双击、移动;
有键盘的键按下、键松开、按键;
有窗体加载、窗体卸载等等。