首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >启用文本框的动态复选框

启用文本框的动态复选框
EN

Stack Overflow用户
提问于 2019-11-20 16:06:47
回答 1查看 180关注 0票数 2

我有一个关于动态控件的问题。我创建了一个动态用户表单,其单帧依赖于一个数字(在我的示例中,rngprt依赖于用户输入)。到现在为止都没有问题。现在,我想在单击相对复选框时启用特定框架内的文本框(以便更改其值)。我使用了一个类模块(Classe1),但在我的代码中,我只成功地启用了n帧中的最后一个文本框(例如,如果我有3个框架,其中有3个文本框和3个复选框,那么只有第三个复选框可以启用第三个文本框,第一个和第二个不起作用)。

类模块: Classe1

代码语言:javascript
复制
Option Explicit
Public WithEvents cmbEvent1 As MSForms.CommandButton
Public WithEvents txbEvent1 As MSForms.TextBox
Public WithEvents frmEvent1 As MSForms.Frame
Public WithEvents ckbEvent1 As MSForms.CheckBox

Private Sub cmbEvent1_Click()
UserForm3.Hide
End Sub

Private Sub frmEvent1_Click()
End Sub

Public Sub txbEvent1_Change()
End Sub

Private Sub ckbEvent1_Click()
If UserForm3.Controls("CK" & xx).Value = True Then
UserForm3.Controls("TB" & xx).Enabled = True
End If
End Sub

模块: UserForm3

代码语言:javascript
复制
Option Explicit
Dim cmdB As New Classe1
Dim txtB As New Classe1
Dim chkB As New Classe1
Dim frm As New Classe1
Dim chkBColl As New Collection

Private Sub UserForm_Initialize()
Dim x As Long
Dim c As Variant
Dim cmdB1 As MSForms.CommandButton
Dim frm1 As MSForms.Frame
Dim txtB1 As MSForms.TextBox
Dim chkB1 As MSForms.CheckBox

Set cmdB1 = UserForm3.Controls.Add("Forms.CommandButton.1")
       With cmdB1
            .Name = "OKButton"
            .Caption = "OK"
            .Top = 40 * rngprt
            .Left = 120
            .Width = 40
            .Height = 25
       End With
       Set cmdB.cmbEvent1 = cmdB1

For x = cel.Row To cel.Row + rngprt - 1 '**rngprt is a number from a Module1**

xx = x - cel.Row + 1  '**for progessive name of controls Dim xx as long into the Module1**

Set frm1 = UserForm3.Controls.Add("Forms.Frame.1")
            frm1.Top = 40 * (xx - 1)
            frm1.Left = 10
            frm1.Width = 300
            frm1.Height = 35
            frm1.Name = "FR" & xx

With frm1.Controls

Set txtB1 = .Add("Forms.TextBox.1")
       With txtB1
            .Name = "TB" & xx
            .Top = 10
            .Left = 160
            .Width = 30
            .Height = 15
            .TextAlign = fmTextAlignRight
            .Enabled = False
            .Value=50
       End With
       Set txtB.txbEvent1 = txtB1

     Set chkB1 = .Add("Forms.CheckBox.1")
       With chkB1
            .Name = "CK" & xx
            .Caption = "Part"
            .Top = 10
            .Left = 245
            .Width = 45
            .Height = 15
       End With
       Set chkB.ckbEvent1 = chkB1
       'Here I added the code below

End With
       Set frm.frmEvent1 = frm1
Next x

End Sub

我还尝试在chkB1设置下面添加此代码,但什么都没有。

代码语言:javascript
复制
       Set chkB = New Classe1
       Set chkB.ckbEvent1 = Me.Controls(chkB.txbEvent1)
       chkBColl.Add chkB

任何人都有想法。提前感谢大家的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-20 23:19:17

您的代码将无法工作。

您需要创建一个classe1数组。检查此更改:

Classe1

代码语言:javascript
复制
Option Explicit
Public WithEvents cmbEvent1 As MSForms.CommandButton
Public WithEvents txbEvent1 As MSForms.TextBox
Public WithEvents frmEvent1 As MSForms.Frame
Public WithEvents ckbEvent1 As MSForms.CheckBox

Public xx As Integer

Private Sub cmbEvent1_Click()
UserForm3.Hide
End Sub

Private Sub frmEvent1_Click()
End Sub

Public Sub txbEvent1_Change()
End Sub

Private Sub ckbEvent1_Click()
If UserForm3.Controls("CK" & xx).value = True Then
UserForm3.Controls("TB" & xx).Enabled = True
End If
End Sub

UserForm3

代码语言:javascript
复制
Dim cmdB() As New Classe1
Dim txtB() As Classe1
Dim chkB() As Classe1
Dim frm() As New Classe1


Dim chkBColl As New Collection

Private Sub UserForm_Initialize()
Dim x As Long
Dim c As Variant
Dim cmdB1 As MSForms.CommandButton
Dim frm1 As MSForms.Frame
Dim txtB1 As MSForms.TextBox
Dim chkB1 As MSForms.CheckBox
ReDim Preserve cmdB(1)
Set cmdB1 = UserForm3.Controls.Add("Forms.CommandButton.1")
       With cmdB1
            .Name = "OKButton"
            .Caption = "OK"
            .Top = 40 * rngprt
            .Left = 120
            .Width = 40
            .Height = 25
       End With
       Set cmdB(0).cmbEvent1 = cmdB1

For x = cel.Row To cel.Row + rngprt - 1 '**rngprt is a number from a Module1**

xx = x - cel.Row + 1  '**for progessive name of controls Dim xx as long into the Module1**

    ReDim Preserve txtB(xx)
Set txtB(xx) = New Classe1
Set frm1 = UserForm3.Controls.Add("Forms.Frame.1")
            frm1.Top = 40 * (xx - 1)
            frm1.Left = 10
            frm1.Width = 300
            frm1.Height = 35
            frm1.Name = "FR" & xx

With frm1.Controls

Set txtB1 = .Add("Forms.TextBox.1")
       With txtB1
            .Name = "TB" & xx
            .Top = 10
            .Left = 160
            .Width = 30
            .Height = 15
            .TextAlign = fmTextAlignRight
            .Enabled = False
            .Value=50
       End With
       txtB(xx).xx = xx
       Set txtB(xx).txbEvent1 = txtB1

ReDim Preserve chkB(xx + 1)
Set chkB(xx) = New Classe1
     Set chkB1 = .Add("Forms.CheckBox.1")
       With chkB1
            .Name = "CK" & xx
            .Caption = "Part"
            .Top = 10
            .Left = 245
            .Width = 45
            .Height = 15
       End With
       Set chkB(xx).ckbEvent1 = chkB1
       chkB(xx).xx = xx
       'Here I added the code below

End With
ReDim Preserve frm(xx)
       Set frm(xx).frmEvent1 = frm1
Next x

End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58949445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档