重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你还要把过程与控件事件绑定
成都创新互联专注于阳新企业网站建设,响应式网站设计,商城网站制作。阳新网站建设公司,为阳新等地区提供建站服务。全流程按需求定制网站,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
AddHandler 控件.事件名,addressof 事件过程
RemoveHandler 这个是取消绑定
没发现自动生存的事件过程后面还有一个Hander button1.Click之类的,这就是所谓事件句柄。反而跟过程名没关系,改成阿猫阿狗也可以。
例外也可以像 Private WithEvents obj as ControlClass 这么声明控件变量,估计像vb6那样会在下拉列表中列出事件系列。
哎呀,说了半天跑题了。 ff不存在嘛多半不是它的作用域范围内,应该把ff变量定义在类中,而不是类中的某个过程中。
最好把代码添多一点,把ff部分也添出来看看。
Private WithEvents NewTextBox As TextBox
'通过使用WithEvents关键字声明一个对象变量为新的命令按钮
Private Sub Command1_Click()
If NewTextBox Is Nothing Then
Set NewTextBox = Controls.Add("VB.TextBox", "cmdNew", Form1)
NewTextBox.Move 200, 200
NewTextBox.Width = Form1.Width - 450
NewTextBox.Height = Form1.Height - 1400
NewTextBox.Visible = True
End If
End Sub
Private Sub Command2_Click()
If NewTextBox Is Nothing Then
Exit Sub
Else
Controls.Remove NewTextBox
Set NewTextBox = Nothing
End If
End Sub
自己在窗体上加一个按钮,以下是详细代码(整个类)
Public Class Form1
Private N As Integer '用来记添加要加入textbox的个数
Private PL As Integer = 10 'textbox相对于窗体的Left
Private PT As Integer = 10 'textbox相对于窗体的Top
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.AutoScroll = True '窗体自动显示滚动条
N = 10 '初始化N为10
For i = 1 To N
Dim newtextbox As New TextBox
newtextbox.Left = PL
newtextbox.Top = PT
Me.Controls.Add(newtextbox)
PT += newtextbox.Height + 10 '各 newtextbox上下间隔10-------PL不变是希望左对齐
Next
End Sub
End Class
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控件
下面这段代码完成,在窗体上用语句添加2个 GroupBox控件,且在每个GroupBox控件中添加4个 RadioButton 控件。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
'添加2个GroupBox
Dim MyGroupBox(2) As GroupBox
For i = 1 To 2
'将一个GroupBox控件加入到Form上
MyGroupBox(i) = New GroupBox
Me.Controls.Add(MyGroupBox(i))
'设置该GroupBox控件的属性
MyGroupBox(i).Height = 240
MyGroupBox(i).Width = 600
MyGroupBox(i).Top = (i - 1) * (240 + 20) + 20
MyGroupBox(i).Left = 20
'修改新加入控件的Text值
MyGroupBox(i).Text = "GroupBox" CStr(i)
Next
'每个GroupBox中添加4个单选按钮
Dim MyRadioButton1(4) As RadioButton
Dim MyRadioButton2(4) As RadioButton
For i = 1 To 4
MyRadioButton1(i) = New RadioButton
Me.Controls.Add(MyRadioButton1(i))
MyRadioButton1(i).Parent = MyGroupBox(1)
'设置该GroupBox控件的属性
MyRadioButton1(i).Height = 20
MyRadioButton1(i).Width = 120
MyRadioButton1(i).Top = (i - 1) * (20 + 20) + 40
MyRadioButton1(i).Left = 20
'修改新加入控件的Text值
MyRadioButton1(i).Text = "RadioButton1_" CStr(i)
Next
For i = 1 To 4
MyRadioButton2(i) = New RadioButton
Me.Controls.Add(MyRadioButton2(i))
MyRadioButton2(i).Parent = MyGroupBox(2)
'设置该GroupBox控件的属性
MyRadioButton2(i).Height = 20
MyRadioButton2(i).Width = 120
MyRadioButton2(i).Top = (i - 1) * (20 + 20) + 40
MyRadioButton2(i).Left = 20
'修改新加入控件的Text值
MyRadioButton2(i).Text = "RadioButton2_" CStr(i)
Next
End Sub
比如说这样添加的计时器:
Dim Timer1 As New Timer
你要在合适的地方开始,比如窗体Load事件中,这样:
Timer1.Interval = 1000 '单位毫秒,1000毫秒=1秒
Timer1.Enabled = True '启用计时器
然后在代码窗口上方下拉列表里选择Timer1的Tick事件,自动添加代码:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
这是计时器触发的事件,每秒触发一次,在这里面输入循环判断是否有日程即可。提示:你可以用Now对象获取当前的日期和时间。