textbox将根据选中的复选框显示字符串

时间:2017-03-14 17:45:53

标签: excel vba

我有一个带有3个复选框和1个文本框的UserForm ..我想在文本框中显示一个字符串,具体取决于勾选的复选框。

chkProcessor是tick =处理器是计算机的大脑......

chkRam是tick = RAM棒通常我们称之为内存

chkCmos是tick =它就像一个硬币型电池..

如果勾选2个或更多复选框,则它将显示各自的字符串。

这是表格

enter image description here

这是我的代码:

Private Sub chkCmos_Click()

If chkCmos.Value = True Then
txtVerbiage.Text = txtVerbiage.Text & "It's like a coin type battery.." & Chr(10)

Else

' I WANT THE ABOVE TEXT TO BE NOT DISPLAYED THEN

End If
End Sub

Private Sub chkProcessor_Click()

If chkProcessor.Value = True Then

    txtVerbiage.Text = txtVerbiage.Text & "Processor is the brain of computer..." & Chr(10)

    'I WANT TO HAVE A LINE BREAK HERE.

    txtVerbiage.Text = txtVerbiage.Text & "Normally sets below the heat sink..." & Chr(10)

Else

' I WANT THE ABOVE TEXT TO BE NOT DISPLAYED THEN


End If

End Sub

Private Sub chkRam_Click()

If chkRam.Value = True Then

txtVerbiage.Text = txtVerbiage.Text & "RAM stick usually we call it memory" & Chr(10)


Else

' I WANT THE ABOVE TEXT TO BE NOT DISPLAYED THEN


End If

End Sub



Private Sub cmdErase_Click()
txtComment.Text = ""
txtVerbiage.Text = ""
chkProcessor.Value = False
chkRam.Value = False
chkCmos.Value = False
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

Private Sub chkProcessor_Click()
    DisplayManager
End Sub

Private Sub chkRAM_Click()
    DisplayManager
End Sub

Private Sub chkCmos_Click()
    DisplayManager
End Sub

Sub DisplayManager()
    Dim cb As MSForms.Control, txt As String

    txt = vbNullString
    Me.txtVerbiage.Value = txt

    For Each ctrl In UserForm1.Controls
        If TypeName(ctrl) = "CheckBox" Then
            If ctrl.Value Then
                txt = txt & GetMessage(ctrl.Name) & vbCrLf
            End If
        End If
    Next ctrl

    Me.txtVerbiage.Value = txt
End Sub

Function GetMessage(cbName As String) As String
    Dim str As String        
        If cbName = "chkProcessor" Then
            str = "Processor is the brain of computer..."
        ElseIf cbName = "chkRam" Then
            str = "RAM stick usually we call it memory"
        ElseIf cbName = "chkCmos" Then
            str = "It's like a coin type battery..."
        End If    
    GetMessage = str
End Function
  • 对于txtVerbiageMultiline设置为True(在VB编辑器属性中)
  • 每个chkXXX调用DisplayManager来管理文字显示
  • 文本将按Userform
  • 上的chkXXX的顺序显示