在无模式表单上输入文本框的验证消息会中断文本选择 [英] Validation message of text box entry on modeless form interrupts text selection
问题描述
我尝试在用户窗体中使用以下代码检查在textbox1
中输入的数据是否为数字,如果没有向用户显示msgbox
并在textbox1
中选择文本,但是下面的代码未选择文本当用户窗体为vBModeless
Hi I try this code in my Userform to check if Data entered in textbox1
is a number and if is not show a msgbox
to user and select text in textbox1
, but below code doesn't select text in textbox1
when Userform is vBModeless
Private Sub TextBox1_Change()
If Not IsNumeric(TextBox1) Then
MsgBox " only number"
TextBox1.SetFocus
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End If
End Sub
有什么解决办法吗?
推荐答案
在我的Excel版本中,msgbox始终为vbModal,不能为vbModeless,只能将其Modal范围属性设置为应用程序级别或系统级别
In my version of Excel A msgbox is always vbModal, it cannot be vbModeless, you can only set its Modal scope property to be as application level or system level
- 在应用程序级别,它将停止应用程序,直到响应为止
- 在系统级别,它会挂起所有应用程序,直到用户响应为止
- At Application level, it stops the application until it is responded
- At system level it suspends all applications until the user responds to it
为了做你打算做的事;我创建了一个无模式用户窗体并将其用作消息框
In order to do what you intend to do; I have created a Modeless UserForm and use it as a message box
代码变为
Private Sub TextBox1_Change()
If Not IsNumeric(TextBox1) Then
UserForm2.Label1 = "Only Number is Allowed"
UserForm2.Show
'At this point TextBox1 has lost focus,
'to set the focus again you have to setfocus on something else
'and then again set focus on textbox1 (a way to reinitialize it).
'I have added a hidden textbox2 and will set focus on it
TextBox2.Visible = True
TextBox2.SetFocus
TextBox2.Visible = False
TextBox1.SetFocus
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End If
End Sub
该屏幕截图仅是测试,您可以根据自己的应用进行格式化等操作.
The screenshot is only a test, you can do the formatting etc according to your application.
这篇关于在无模式表单上输入文本框的验证消息会中断文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!