【问题标题】:How to disable text box until previous textbox has been populated?如何在填充上一个文本框之前禁用文本框?
【发布时间】:2020-08-09 03:55:11
【问题描述】:

我有一个用户表单,它由一个组合框和多个文本框组成。

如何禁用每个文本框并将其灰显,以防止任何用户输入,除非其上方的文本框已被填充?

我尝试了这两种技术。

Me.CSockett.Enabled = Not IsNull(Me.CSocketl)

'If CSocketl.Value = "" Then
'    Me.CSockett.Enabled = False
'    Else
'    Me.CSockett.Enabled = True
'End If

【问题讨论】:

    标签: vba textbox disabled-input


    【解决方案1】:

    为了使其更灵活(这意味着您可以将它用于很多文本框),我创建了三个处理文本框事件的类,并定义了一个自己的事件来启用相应的。禁用文本框。

    Reading 在 MS 的这个主题

    班级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
    

    班级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
    

    班级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
    

    【讨论】:

      【解决方案2】:

      我创建了一个新的 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 代码):

      在 textbox1 中输入一个字符后:

      .Enabled 属性如果设置为False,将不允许用户单击或切换到Control(在本例中为TextBox)。我添加了 BackColor 更改,以让视觉助手向用户显示它“变灰” - 否则它看起来与启用的 textbox 相同,并且可能会让用户感到沮丧。

      自然,一旦启用,颜色就会恢复为默认值。

      【讨论】:

      • 不错,彻底的答案!
      猜你喜欢
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      相关资源
      最近更新 更多