2017-06-05 171 views
0

需要幫助編寫代碼 - 我已經開發了一個用戶窗體。Combobox在另一個組合框內

我需要一個組合框來顯示一個字母的主要部分: 「根據我們的決心,他/她有權要求退款」,「基於我們的決心,他/她不享有退款」

,但我也需要一個組合框中選擇性別: 「他」 「SHE」

到目前爲止我有:

Private Sub Userform_Initialize() 
With ComboBoxDecision 
.AddItem "based on our determination HE/SHE is entitled to a refund" 
.AddItem "based on our determination HE/SHE is not entitled to a refund" 
End With 
With ComboBoxGender 
.AddItem "HE" 
.AddItem "SHE" 
End With 
lbl_exit: 
Exit sub 
End Sub 

Private Sub CommandButtonOk_Click() 
Application.ScreenUpdating = False 
With ActiveDocument 
    .Bookmarks("Decision").Range.Text = ComboBoxDecision.Value 
    End With 
Application.ScreenUpdating = True 
Unload Me 
End Sub 

有沒有辦法做到:

.AddItem "based on our determination "Whatever option was selected in ComboBoxGender" is entitled to a refund" 
.AddItem "based on our determination "Whatever option was selected in ComboBoxGender" is not entitled to a refund" 

樂意提供更多信息。

回答

1

最簡單的方法可能是每當'Gender'組合框發生變化時用適當的文本填充'Decision'組合框。你會通過捕獲ComboBoxGender_Change事件,像這樣做:

Private Sub ComboBoxGender_Change() 
    Dim gndr As String 

    gndr = Me.ComboBoxGender.Text 

    With Me.ComboBoxDecision 
     .Clear 
     .AddItem "based on our determination " & _ 
       gndr & " is entitled to a refund" 
     .AddItem "based on our determination " & _ 
       gndr & " is not entitled to a refund" 
    End With 

End Sub 

Private Sub UserForm_Initialize() 
    With Me.ComboBoxGender 
     .AddItem "HE" 
     .AddItem "SHE" 
     .ListIndex = 0 
    End With 
End Sub 
1

可以存儲在同一個組合框的不同列中多個選擇。根據性別的選擇,然後隱藏一列或另一列。代碼如下所示。

Option Explicit 
Option Base 0 

Private Sub UserForm_Initialize() 

    Dim Arr(1, 1) As String 

    ' Column, item: 
    Arr(1, 0) = "Based on our determination she is not entitled to a refund" 
    Arr(0, 0) = Replace(Arr(1, 0), "not", "") 
    Arr(1, 1) = Replace(Arr(1, 0), "she", "he") 
    Arr(0, 1) = Replace(Arr(1, 1), "not", "") 
    With CbxDecision 
     .List = Arr 
     .ColumnCount = 2 
     .ListIndex = 0 
    End With 

    With CbxGender 
     .List = Split("He She") 
     .ListIndex = 0 
    End With 
End Sub 


Private Sub CbxGender_Change() 
    ' set the list width for 1st and 2nd column 
    ' one of them must always be 0 (= hidden) 
    CbxDecision.ColumnWidths = Split("0pt;90pt|90pt;0pt", "|")(CbxGender.ListIndex) 
End Sub 
相關問題