如何在填充上一个文本框之前禁用文本框?
How to disable text box until previous textbox has been populated?
我有一个用户表单,它由一个组合框和多个文本框组成。
我如何禁用每个文本框并将其灰显,以防止任何用户输入,除非其上方的文本框已被填充?
我尝试了这两种技术。
Me.CSockett.Enabled = Not IsNull(Me.CSocketl)
'If CSocketl.Value = "" Then
' Me.CSockett.Enabled = False
' Else
' Me.CSockett.Enabled = True
'End If
我创建了一个新的 UserForm
,其中有 2 个 TextBox
都使用了默认名称。您可以将此概念应用于您需要的每个文本框。
Private Sub TextBox1_Change()
If Not Me.TextBox1.Text = "" Then
Me.TextBox2.Enabled = True
Me.TextBox2.BackColor = &H80000005
Else
Me.TextBox2.Enabled = False
Me.TextBox2.BackColor = &H80000016
End If
End Sub
Private Sub UserForm_Initialize()
Me.TextBox2.Enabled = False
Me.TextBox2.BackColor = &H80000016
End Sub
Initialize
代码禁用第二个文本框并设置背景颜色(见下文)以在表单首次打开时直观地显示它已被禁用。
Change
代码会评估 TextBox.Text
属性 是否应启用下一个文本框。
注意: 通过一些搜索,您会发现除 ...Text = ""
.
之外的一百万种其他方法来检查空文本框
首次打开时的用户窗体(Initialize
代码):
在文本框 1 中输入一个字符后:
.Enabled
属性 如果设置为 False
将不允许用户单击或跳转到 Control
(在这种情况下,TextBox
).我添加了 BackColor
更改,让视觉助手向用户显示它是 'greyed out' - 否则它看起来与启用的 textbox
相同,可能会让用户感到沮丧。
当然,启用后颜色会设置回默认值。
为了使其更加灵活(这意味着您可以将它用于很多文本框)我创建了三个 类 来处理文本框事件并定义了一个自己的事件来启用响应。禁用文本框。
Reading 在 MS
上的这个主题
Class cTextboxes
Option Explicit
Private mcolTextboxes As Collection
Private Sub Class_Initialize()
Set mcolTextboxes = New Collection
End Sub
Public Sub Add(ByRef nTextbox As MSForms.Textbox, index As Long)
Dim mTextbox As cTextbox
Set mTextbox = New cTextbox
mTextbox.index = index
Set mTextbox.Parent = Me
Set mTextbox.Textbox = nTextbox
mcolTextboxes.Add mTextbox
End Sub
Public Function InitBoxes()
Dim i As Long
For i = 2 To mcolTextboxes.Count
mcolTextboxes.Item(i).Enabled = False
mcolTextboxes.Item(i).Backcolor = &H80000016
Next i
End Function
Public Function enableBox(index As Long)
If index <= mcolTextboxes.Count Then
mcolTextboxes.Item(index).Enabled = True
mcolTextboxes.Item(index).Backcolor = &H80000005
End If
End Function
Public Function disableBox(index As Long)
If index <= mcolTextboxes.Count Then
mcolTextboxes.Item(index).Enabled = False
mcolTextboxes.Item(index).Backcolor = &H80000016
End If
End Function
Class cTextbox
Option Explicit
Private WithEvents mTextbox As MSForms.Textbox
Private WithEvents mTextChange As cTextboxChange
Dim mParent As Object
Dim mIndex As Long
Public Property Set Textbox(tb As MSForms.Textbox)
Set mTextbox = tb
Set mTextChange = New cTextboxChange
End Property
Property Let index(nIndex As Long)
mIndex = nIndex
End Property
Property Get index() As Long
index = mIndex
End Property
Public Property Set Parent(nParent As Object)
Set mParent = nParent
End Property
Public Property Get Parent()
Set Parent = mParent
End Property
Property Let Enabled(nEnabled As Boolean)
mTextbox.Enabled = nEnabled
End Property
Property Let Backcolor(color As Long)
mTextbox.Backcolor = color
End Property
Private Sub mTextBox_Change()
mTextChange.ChangeIt
End Sub
Private Sub mTextChange_EnableBox()
If mTextbox.TextLength > 0 Then
mParent.enableBox index + 1
Else
mParent.disableBox index + 1
End If
End Sub
Class cTextboxChange
Option Explicit
Public Event enableBox()
Public Sub ChangeIt()
RaiseEvent enableBox
End Sub
用户窗体本身包含四个文本框示例,并包含以下代码
Option Explicit
Dim colTextboxes As cTextboxes
Private Sub UserForm_Initialize()
Set colTextboxes = New cTextboxes
colTextboxes.Add Me.TextBox1, 1
colTextboxes.Add Me.TextBox2, 2
colTextboxes.Add Me.TextBox3, 3
colTextboxes.Add Me.TextBox4, 4
colTextboxes.InitBoxes
End Sub
我有一个用户表单,它由一个组合框和多个文本框组成。
我如何禁用每个文本框并将其灰显,以防止任何用户输入,除非其上方的文本框已被填充?
我尝试了这两种技术。
Me.CSockett.Enabled = Not IsNull(Me.CSocketl)
'If CSocketl.Value = "" Then
' Me.CSockett.Enabled = False
' Else
' Me.CSockett.Enabled = True
'End If
我创建了一个新的 UserForm
,其中有 2 个 TextBox
都使用了默认名称。您可以将此概念应用于您需要的每个文本框。
Private Sub TextBox1_Change()
If Not Me.TextBox1.Text = "" Then
Me.TextBox2.Enabled = True
Me.TextBox2.BackColor = &H80000005
Else
Me.TextBox2.Enabled = False
Me.TextBox2.BackColor = &H80000016
End If
End Sub
Private Sub UserForm_Initialize()
Me.TextBox2.Enabled = False
Me.TextBox2.BackColor = &H80000016
End Sub
Initialize
代码禁用第二个文本框并设置背景颜色(见下文)以在表单首次打开时直观地显示它已被禁用。
Change
代码会评估 TextBox.Text
属性 是否应启用下一个文本框。
注意: 通过一些搜索,您会发现除 ...Text = ""
.
首次打开时的用户窗体(Initialize
代码):
在文本框 1 中输入一个字符后:
.Enabled
属性 如果设置为 False
将不允许用户单击或跳转到 Control
(在这种情况下,TextBox
).我添加了 BackColor
更改,让视觉助手向用户显示它是 'greyed out' - 否则它看起来与启用的 textbox
相同,可能会让用户感到沮丧。
当然,启用后颜色会设置回默认值。
为了使其更加灵活(这意味着您可以将它用于很多文本框)我创建了三个 类 来处理文本框事件并定义了一个自己的事件来启用响应。禁用文本框。
Reading 在 MS
上的这个主题Class cTextboxes
Option Explicit
Private mcolTextboxes As Collection
Private Sub Class_Initialize()
Set mcolTextboxes = New Collection
End Sub
Public Sub Add(ByRef nTextbox As MSForms.Textbox, index As Long)
Dim mTextbox As cTextbox
Set mTextbox = New cTextbox
mTextbox.index = index
Set mTextbox.Parent = Me
Set mTextbox.Textbox = nTextbox
mcolTextboxes.Add mTextbox
End Sub
Public Function InitBoxes()
Dim i As Long
For i = 2 To mcolTextboxes.Count
mcolTextboxes.Item(i).Enabled = False
mcolTextboxes.Item(i).Backcolor = &H80000016
Next i
End Function
Public Function enableBox(index As Long)
If index <= mcolTextboxes.Count Then
mcolTextboxes.Item(index).Enabled = True
mcolTextboxes.Item(index).Backcolor = &H80000005
End If
End Function
Public Function disableBox(index As Long)
If index <= mcolTextboxes.Count Then
mcolTextboxes.Item(index).Enabled = False
mcolTextboxes.Item(index).Backcolor = &H80000016
End If
End Function
Class cTextbox
Option Explicit
Private WithEvents mTextbox As MSForms.Textbox
Private WithEvents mTextChange As cTextboxChange
Dim mParent As Object
Dim mIndex As Long
Public Property Set Textbox(tb As MSForms.Textbox)
Set mTextbox = tb
Set mTextChange = New cTextboxChange
End Property
Property Let index(nIndex As Long)
mIndex = nIndex
End Property
Property Get index() As Long
index = mIndex
End Property
Public Property Set Parent(nParent As Object)
Set mParent = nParent
End Property
Public Property Get Parent()
Set Parent = mParent
End Property
Property Let Enabled(nEnabled As Boolean)
mTextbox.Enabled = nEnabled
End Property
Property Let Backcolor(color As Long)
mTextbox.Backcolor = color
End Property
Private Sub mTextBox_Change()
mTextChange.ChangeIt
End Sub
Private Sub mTextChange_EnableBox()
If mTextbox.TextLength > 0 Then
mParent.enableBox index + 1
Else
mParent.disableBox index + 1
End If
End Sub
Class cTextboxChange
Option Explicit
Public Event enableBox()
Public Sub ChangeIt()
RaiseEvent enableBox
End Sub
用户窗体本身包含四个文本框示例,并包含以下代码
Option Explicit
Dim colTextboxes As cTextboxes
Private Sub UserForm_Initialize()
Set colTextboxes = New cTextboxes
colTextboxes.Add Me.TextBox1, 1
colTextboxes.Add Me.TextBox2, 2
colTextboxes.Add Me.TextBox3, 3
colTextboxes.Add Me.TextBox4, 4
colTextboxes.InitBoxes
End Sub