VBA 用户表单文本框默认值并突出显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21128984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Userform textbox default value and highlighted
提问by blahblahblah
In my UserForm, I want to set a default value for my TextBox, that will highlight when focused upon.
在我的用户窗体中,我想为我的文本框设置一个默认值,它会在聚焦时突出显示。
Private Sub UserForm_Initialize()
NameTextBox.Value = "Your Name Here"
NameTextBox.SetFocus
End Sub
When this code runs, the cursor should set at the end of the default text, i.e. after "...Here". I want "Your Name Here" to be highlighted so that when the form is generated, the user can start replacing that default/placeholder text.
当此代码运行时,光标应设置在默认文本的末尾,即“...Here”之后。我希望突出显示“您的姓名”,以便在生成表单时,用户可以开始替换该默认/占位符文本。
Can you help me write the code to set default values that are editable?
你能帮我写代码来设置可编辑的默认值吗?
回答by Doug Glancy
This will select all the text in the TextBox:
这将选择 TextBox 中的所有文本:
Private Sub UserForm_Initialize()
With Me.NameTextBox
.Value = "Your Name Here"
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
回答by MBGtips
Set the EnterFieldBehavior
property to frmEnterFieldBehaviorSelectAll
so that when the focus goes to that field, the whole field is selected.
将该EnterFieldBehavior
属性设置为frmEnterFieldBehaviorSelectAll
当焦点移到该字段时,整个字段都会被选中。
回答by nabinchha
how about this line of code when your form loads?
当您的表单加载时,这行代码怎么样?
NameTextBox.SelectAll()