我目前的工作项目面临一些问题。
我想在多页用户表单上检查2组文本框(4个带标签" A" 4带标签" B")实时检查,如果其中一组是非空(每个文本框必须包含一个值)应该启用一个按钮。
这是我到目前为止所做的:
主要代码:
Dim TextBoxes() As New ChangeCheck
Private Sub UserForm_Initialize()
Main.ZeitA_CommandButton1.Enabled = False
Dim Ctl As MSForms.Control
Dim i As Long
i = 1
For Each Ctl In Me.Controls
If TypeName(Ctl) = "TextBox" Then
ReDim Preserve TextBoxes(1 To i)
Set TextBoxes(i).TextGroup = Ctl
i = i + 1
End If
Next Ctl
End Sub
课程模块" ChangeCheck"
Option Explicit
Public WithEvents TextGroup As MSForms.Textbox
Sub TextGroup_Change()
If TextGroup.Tag = "A" Then
MsgBox "Hallo"
End If
End Sub
因此,如果我更改带有标签" A"的文本框我得到一个MsgBox,但是如何检查是否所有带有标签" A"不是空的?如果我使用
If TextGroup.Tag = "A" And TextGroup.Value <> "" Then
每个文本框都会激活或停用按钮
提前谢谢你 Moritz的
答案 0 :(得分:1)
下面有一种可能的解决方案。
假设有一个名为UserForm1
的表单,其中包含8个TextBox控件(4个带有标记“A”,4个带有标记“B”,如上所述)和2个名为CommandButton1
的CommandButton控件用于组“A”和CommandButton2
表示“B”。将此代码放入UserForm1
部分(一些变量名称替换为更具指导性的IMO,我也使用集合代替数组,因此不需要计数器变量):
Option Explicit
Private Sub UserForm_Initialize()
Dim Ctl As MSForms.Control
Dim ChangeCheckInstance As ChangeCheck
Set Form = Me
Me.CommandButton1.Enabled = False
Me.CommandButton2.Enabled = False
For Each Ctl In Me.Controls
If TypeName(Ctl) = "TextBox" Then
Set ChangeCheckInstance = New ChangeCheck
Set ChangeCheckInstance.TextBoxCtl = Ctl
ChangeChecks.Add ChangeCheckInstance
End If
Next Ctl
End Sub
项目必须在模块部分中包含Module1
,代码如下:
Option Explicit
Public ChangeChecks As New Collection
Public Form As Object
Public Sub CheckTextBoxes(CallerTextBox As MSForms.TextBox)
Dim ChangeCheckInstance As ChangeCheck
Dim NotEmpty As Boolean
NotEmpty = True
For Each ChangeCheckInstance In ChangeChecks
With ChangeCheckInstance.TextBoxCtl
If (.Text = "") And (.Tag = CallerTextBox.Tag) Then
NotEmpty = False
Exit For
End If
End With
Next
Select Case CallerTextBox.Tag
Case "A"
Form.CommandButton1.Enabled = NotEmpty
Case "B"
Form.CommandButton2.Enabled = NotEmpty
End Select
End Sub
您在类模块部分中的ChangeCheck
类定义:
Option Explicit
Public WithEvents TextBoxCtl As MSForms.TextBox
Sub TextBoxCtl_Change()
CheckTextBoxes TextBoxCtl
End Sub
正如您所看到的,所有检查操作都在Public Sub CheckTextBoxes
中实现,它从已更改的类实例传递的参数CallerTextBox
中获取必要的数据,并使用所有复选框获取全局变量:ChangeChecks
集合, Form
引用UserForm1
。
修改强>
如果你不想拥有全局变量(这不是你所知道的最佳实践),另一种方法是在类中封装check方法和必要的数据。因此,您无需在模块部分中保留Module1
,表单部分中的UserForm1
代码将如下所示:
Option Explicit
Private Sub UserForm_Initialize()
Dim Ctl As MSForms.Control
Dim ChangeChecks As New Collection
Dim ChangeCheckInstance As ChangeCheck
Me.CommandButton1.Enabled = False
Me.CommandButton2.Enabled = False
For Each Ctl In Me.Controls
If TypeName(Ctl) = "TextBox" Then
Set ChangeCheckInstance = New ChangeCheck
Set ChangeCheckInstance.TextBoxCtl = Ctl
Set ChangeCheckInstance.ChangeChecks = ChangeChecks
Set ChangeCheckInstance.Form = Me
ChangeChecks.Add ChangeCheckInstance
End If
Next Ctl
End Sub
,类模块部分中的ChangeCheck
类定义如下:
Option Explicit
Public WithEvents TextBoxCtl As MSForms.TextBox
Public ChangeChecks As Collection
Public Form As Object
Sub TextBoxCtl_Change()
Dim ChangeCheckInstance As ChangeCheck
Dim NotEmpty As Boolean
NotEmpty = True
For Each ChangeCheckInstance In ChangeChecks
With ChangeCheckInstance.TextBoxCtl
If (.Text = "") And (.Tag = TextBoxCtl.Tag) Then
NotEmpty = False
Exit For
End If
End With
Next
Select Case TextBoxCtl.Tag
Case "A"
Form.CommandButton1.Enabled = NotEmpty
Case "B"
Form.CommandButton2.Enabled = NotEmpty
End Select
End Sub