'把图片的声明放在外面,类里面过程外面,而且要用As,不然等会儿没法用
成都创新互联公司服务热线:13518219792,为您提供成都网站建设网页设计及定制高端网站建设服务,成都创新互联公司网页制作领域十余年,包括iso认证等多个方面拥有丰富的网站营销经验,选择成都创新互联公司,为网站保驾护航!
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
如果要改事件的签名(参数一类的)的话,得重写控件。你可以这样自己弄一个事件。
两种方法:
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 语句:
软糖来回答罗。
“调用事件”有歧义,你的意思是不是下面两种:
调用事件处理方法
直接调用对象名.方法名
'例如有这样一个按钮点击事件处理程序
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
秒表.Start()
End Sub
'调用它直接用
Button1_Click(me, new EventArgs())
引发事件
Button1.Click.Invoke(me, new EventArgs())
另外 RaiseEvent 也可以引发事件,但不能使用 RaiseEvent 来引发派生窗体的控件事件,只有自己定义的类的事件才能用它引发。
如满意,请采纳,谢谢。
新建窗体,把maximizebox属性和MinimizeBox 属性设为false,再添加一个Label1放置文字一个Picturebox1放图片。
在窗体的load事件中:
Private Sub Form1_Load() Handles Me.Load
Text="我的VB.NET程序"
PuctureBox1.Image=Image.FromFile(图片路径如e:\pic.jpg)
End Sub
窗体Click事件:
Private Sub Form1_Click() Handles Me.Click
Text="单击窗体"
Label1.Text="欢迎使用VB.NET"
End Sub
窗体DoubleClick事件
Private Sub Form1_DoubleClick() Handles Me.DoubleClick
Text="双击窗体"
Label1.Text = "谢谢使用!"
End Sub
Dim Text() As Object
Private Sub Form_Load()
Dim i As Integer
Dim nTop As Long
ReDim Text(1 To 10)
For i = 1 To 10
Set Text(i) = Controls.Add("VB.TextBox", "Text1" CStr(i))
Text(i).Height = 300
Text(i).Top = nTop
Text(i).Visible = True
nTop = nTop + 30 * 15
Next i
End Sub
添加动态控件。你可以自己试试看。 form1上什么都不加运行这个程序后会发现自动生成了10个text控件