源码如下:
Private Sub Text1_LostFocus(Index As Integer)
If Not IsNumeric(CInt(Text1(?).Text)) Then
MsgBox ("input error! please repress")
Text1(?).SetFocus
End If
End Sub
Private Sub Text1_LostFocus(Index As Integer)
If Not IsNumeric(CInt(Text1(?).Text)) Then
MsgBox ("input error! please repress")
Text1(?).SetFocus
End If
End Sub
msgbox "偶是老"+str(index)
end sub
If Not IsNumeric(CInt(Text1(index).Text)) Then
MsgBox ("input error! please repress")
Text1(index).SetFocus
End If
End Sub
ActiveControl 属性
返回拥有焦点的控件。当窗体被引用时,如在 ChildForm.ActiveControl 中,如果被引用的窗体是活动的,ActiveControl 指定将拥有焦点的控件。在设计时是不可用的;在运行时是只读的。语法object.ActiveControlobject 所在处代表一个对象表达式,其值是“应用于”列表中的一个对象。说明可以使用 ActiveControl 来访问控件的属性或调用其方法:例如,Screen.ActiveControl.Tag = "0"。如果在窗体上的所有控件都是不可见的或不可使用的,那么将产生一个运行时错误。每个窗体都可以有一个活动控件 (Form.ActiveControl),而不管窗体是否是活动的。在应用程序中,可以在每个窗体上编写处理活动控件的代码,即使此窗体不是活动窗体。这个属性在多文档接口 (MDI) 应用程序中尤其有用,其中工具栏上的一个按钮必须初始化为 MDI 子窗体中的控件上的一个动作。当用户单击工具栏上的“复制”按钮时,代码可以引用 MDI 子窗体的活动控件中的文本,例如 ActiveForm.ActiveControl.SelText。注意 如果计划将 Screen.ActiveControl 传递给一个过程,那么在那个过程中必须用子句 As Control 而不是指定控件的类型 (As TextBox 或 As ListBox)声明参数,即使 ActiveControl 总是引用相同类型的控件。
ActiveControl 属性示例
这个例子显示活动控件的文本。要试用此例,可以先将下面的代码粘贴到包含 TextBox、Label 和 CommandButton 控件的窗体的声明部分中,然后按下 F5 键并单击此窗体。Private Sub Form_Click ()
If TypeOf Screen.ActiveControl Is TextBox Then
Label1.Caption = Screen.ActiveControl.Text
Else
Label1.Caption = "Button: " + Screen.ActiveControl.Caption
End If
End Sub这个例子显示了如何使用工具栏上的按钮在实现剪切、复制、粘贴和删除操作中,利用 Clipboard 对象。要试一试这个例子,可以将 TextBox 和 CheckBox 控件放到 Form1 上,然后创建一个新的 MDI 窗体。在 MDI 窗体上,插入一个 PictureBox 控件,然后在 PictureBox 中插入一个 CommandButton。将 CommandButton 的 Index 属性设置为 0 (创建一个控件数组)。将 Form1 的 MDIChild 属性设置为 True。要运行该例子,只须将这些代码复制到 MDIForm 的声明部分,然后按下 F5 键。注意,当 CheckBox 拥有焦点时,按钮将不可用,因为现在 CheckBox 取代了 TextBox 而成为活动控件。Private Sub MDIForm_Load ()
Dim I ' 声明变量。
Command1(0).Move 0, 0, 700, 300 '在工具栏上为按钮定位。
For I = 1 To 3 '创建别的按钮。
Load Command1(I) '创建按钮。
Command1(I).Move I * 700, 0, 700, 300 '放置并调整按钮大小。
Command1(I).Visible = True '显示按钮。
Next I
Command1(0).Caption = "Cut" '设置按钮标题。
Command1(1).Caption = "Copy"
Command1(2).Caption = "Paste"
Command1(3).Caption = "Del"
End SubPrivate Sub Command1_Click (Index As Integer)
'ActiveForm 是指 MDI 窗体中的活动窗体。
If TypeOf ActiveForm.ActiveControl Is TextBox Then
Select Case Index
Case 0 '剪切。
'复制选中的文本到剪贴板上。
Clipboard.SetText ActiveForm.ActiveControl.SelText
'删除选中的文本。
ActiveForm.ActiveControl.SelText = ""
Case 1 '复制。
' 复制选中的文本到剪贴板上。
Clipboard.SetText ActiveForm.ActiveControl.SelText
Case 2 '粘贴。
'把剪贴板上的文本放到文本框中。
ActiveForm.ActiveControl.SelText = Clipboard.GetText()
Case 3 '删除。
'删除选中的文本。
ActiveForm.ActiveControl.SelText = ""
End Select
End If
End Sub
Private Sub Text1_LostFocus(Index As Integer)
If Not IsNumeric(CInt(Text1(?).Text)) Then
MsgBox ("input error! please repress")
Text1(?).SetFocus
End If
End Sub你是想当用户输入错误的时候回到原文本框,对吗?
Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)
If Not IsNumeric(CInt(Text1(Index).Text)) Then
MsgBox ("input error! please repress")
text1.selstart = 0
text1.sellength = text1.maxlength '将原来输入的内容全部选中
Cancel = True
End If
End SubIndex是数组的下标。在Validate事件中Cancel设置为真,焦点即返回原控件。