如何在多选列表框中取消选择后使组合框变为不可见?

huangapple go评论75阅读模式
英文:

How do I get combo boxes to become invisible when the selection is no longer selected in a multiselect list box?

问题

我有一个多选列表框(nameDatabase),当我选择一个选项时,会出现一个组合框,我可以使用它。然而,当我取消选择它时,组合框仍然可见,尽管已取消选择。

这是我目前的代码,用于使它们可见并将选定的项目放入文本框中:

Private Sub nameDatabase_AfterUpdate()

Dim strEng As String
Dim varItem As Variant
    
For Each varItem In Me.nameDatabase.ItemsSelected
    
    strEng = strEng & ", " & Me.nameDatabase.ItemData(varItem)
        
Next
    
If Len(strEng) > 0 Then
    
    strEng = Mid(strEng, 2)
        
End If
    
Me.TestBox = strEng

If nameDatabase.Column(Value) = "A" Then
        
    codeJBox.Visible = True
        
ElseIf nameDatabase.Column(Value) = "B" Then
    
    codeSBox.Visible = True

ElseIf nameDatabase.Column(Value) = "C" Then

    codePBox.Visible = True
    codeABox.Visible = True
   
End If

End Sub

我尝试过在一个文本框中放置所选内容,并且如果,例如,“A”不在其中,它将消失,但是它没有起作用,或者我做错了什么。我有一种感觉,我可能是在寻找使组合框消失的一些简单方法,而且我可能过于复杂化了一切。

英文:

I have a multiselect list box (nameDatabase) that when I select an option, a combo box will appear, and I can use it. However, when I unselect it, the combo box stays visible after it is unselected.

This is what I currently have to make them visible and place the items selected to a text box:

Private Sub nameDatabase_AfterUpdate()

Dim strEng As String
Dim varItem As Variant
    
For Each varItem In Me.nameDatabase.ItemsSelected
    
    strEng = strEng & ", " & Me.nameDatabase.ItemData(varItem)
        
Next
    
If Len(strEng) > 0 Then
    
    strEng = Mid(strEng, 2)
        
End If
    
Me.TestBox = strEng

If nameDatabase.Column(Value) = "A" Then
        
    codeJBox.Visible = True
        
ElseIf nameDatabase.Column(Value) = "B" Then
    
    codeSBox.Visible = True

ElseIf nameDatabase.Column(Value) = "C" Then

    codePBox.Visible = True
    codeABox.Visible = True
   
End If

End Sub

I have tried to have a text box have the selections in it and if, for example, "A" is not in there it will go away though it didn't work, or I did something wrong. I have a feeling it is something simple I'm missing to make the combo box disappear and that I'm overthinking everything.

答案1

得分: 0

你可以最初将它们全部隐藏:

codeJBox.Visible = False
codeSBox.Visible = False
codePBox.Visible = False
codeABox.Visible = False

如果 nameDatabase.Column(Value) = "A" Then
        
    codeJBox.Visible = True
        
ElseIf nameDatabase.Column(Value) = "B" Then
    
    codeSBox.Visible = True

ElseIf nameDatabase.Column(Value) = "C" Then

    codePBox.Visible = True
    codeABox.Visible = True
   
End If
英文:

You could initially hide them all:

codeJBox.Visible = False
codeSBox.Visible = False
codePBox.Visible = False
codeABox.Visible = False

If nameDatabase.Column(Value) = "A" Then
        
    codeJBox.Visible = True
        
ElseIf nameDatabase.Column(Value) = "B" Then
    
    codeSBox.Visible = True

ElseIf nameDatabase.Column(Value) = "C" Then

    codePBox.Visible = True
    codeABox.Visible = True
   
End If

答案2

得分: 0

以下是翻译好的部分:

这应该适用于您的用例(如果没有,请参见编辑):

Private Sub nameDatabase_Change()
    Dim Value As Long
    'Value = 1 '用于测试
    If nameDatabase.Text <> "" Then
        codeJBox.Visible = nameDatabase.Column(Value) = "A"
        codeSBox.Visible = nameDatabase.Column(Value) = "B"
        codePBox.Visible = nameDatabase.Column(Value) = "C"
        codeABox.Visible = nameDatabase.Column(Value) = "C"
    Else
        codeJBox.Visible = False
        codeSBox.Visible = False
        codePBox.Visible = False
        codeABox.Visible = False
    End If
End Sub

如果没有选择任何内容,它会将这4个项目保持不可见,而在输入内容时,只会使您需要的项目可见。如果有不清楚的地方,请告诉我。

编辑:
看来我误解了您的问题,以为这是普通的列表框/组合框,而实际上是Access中的一个。因此,我无法进一步测试,但您可以尝试以下代码,它仍然应该有效。如果不行,请告诉我。

Private Sub nameDatabase_AfterUpdate()

    Dim strEng As String
    Dim varItem As Variant
        
    For Each varItem In Me.nameDatabase.ItemsSelected
        If Len(strEng) = 0 Then
            strEng = Me.nameDatabase.ItemData(varItem)
        Else
            strEng = strEng & ", " & Me.nameDatabase.ItemData(varItem)
        End If
    Next varItem
        
    Me.TestBox = strEng
    
    codeJBox.Visible = InStr(strEng, "A") > 0
    codeSBox.Visible = InStr(strEng, "B") > 0
    codePBox.Visible = InStr(strEng, "C") > 0
    codeABox.Visible = InStr(strEng, "C") > 0
End Sub
英文:

This should work for your use-case (it didn't, see edit):

Private Sub nameDatabase_Change()
    Dim Value As Long
    &#39;Value = 1 &#39;was for testing
    If nameDatabase.Text &lt;&gt; &quot;&quot; Then
        codeJBox.Visible = nameDatabase.Column(Value) = &quot;A&quot;
        codeSBox.Visible = nameDatabase.Column(Value) = &quot;B&quot;
        codePBox.Visible = nameDatabase.Column(Value) = &quot;C&quot;
        codeABox.Visible = nameDatabase.Column(Value) = &quot;C&quot;
    Else
        codeJBox.Visible = False
        codeSBox.Visible = False
        codePBox.Visible = False
        codeABox.Visible = False
    End If
End Sub

It turns the 4 invisible if nothing is selected and when something is typed only makes the ones you need visible. Let me know if something is unclear.

EDIT:
It appears I mistook your question for a normal listbox/combobox, not one from access. As such I can't test this further but you can try the following code which should still work. Let me know if it doesn't.

Private Sub nameDatabase_AfterUpdate()

    Dim strEng As String
    Dim varItem As Variant
        
    For Each varItem In Me.nameDatabase.ItemsSelected
        If Len(strEng) = 0 Then
            strEng = Me.nameDatabase.ItemData(varItem)
        Else
            strEng = strEng &amp; &quot;, &quot; &amp; Me.nameDatabase.ItemData(varItem)
        End If
    Next varItem
        
    Me.TestBox = strEng
    
    codeJBox.Visible = InStr(strEng, &quot;A&quot;) &gt; 0
    codeSBox.Visible = InStr(strEng, &quot;B&quot;) &gt; 0
    codePBox.Visible = InStr(strEng, &quot;C&quot;) &gt; 0
    codeABox.Visible = InStr(strEng, &quot;C&quot;) &gt; 0
End Sub

答案3

得分: 0

很难确定以下内容是否能满足您的要求,而不看到 Sub 所处的更广泛上下文,特别是对于 Value,然而,您可以尝试以下方法,它不仅会在选择适当时使 ComboBox 可见,还会在选择不适当时隐藏它们:

Private Sub nameDatabase_AfterUpdate()

Dim strEng As String
Dim varItem As Variant
    
For Each varItem In Me.nameDatabase.ItemsSelected
    
    strEng = strEng & ", " & Me.nameDatabase.ItemData(varItem)
        
Next
    
If Len(strEng) > 0 Then
    
    strEng = Mid(strEng, 2)
        
End If
    
Me.TestBox = strEng

codeJBox.Visible = Me.nameDatabase.Column("Value") = "A"
codeSBox.Visible = Me.nameDatabase.Column("Value") = "B"
codePBox.Visible = Me.nameDatabase.Column("Value") = "C"
codeABox.Visible = Me.nameDatabase.Column("Value") = "C"

End Sub

... 我必须提到,如果没有更广泛的代码基础来测试这段代码,我无法测试它的运行情况。

英文:

It's still difficult to tell whether the following will do what you want without seeing the wider context the Sub is in, especially for Value, however, you could try this which doesn't just make the ComboBoxes visible if the selection is appropriate, but also hides them if the selection is not appropriate

Private Sub nameDatabase_AfterUpdate()

Dim strEng As String
Dim varItem As Variant
    
For Each varItem In Me.nameDatabase.ItemsSelected
    
    strEng = strEng &amp; &quot;, &quot; &amp; Me.nameDatabase.ItemData(varItem)
        
Next
    
If Len(strEng) &gt; 0 Then
    
    strEng = Mid(strEng, 2)
        
End If
    
Me.TestBox = strEng

codeJBox.Visible = nameDatabase.Column(Value) = &quot;A&quot;
codeSBox.Visible = nameDatabase.Column(Value) = &quot;B&quot;
codePBox.Visible = nameDatabase.Column(Value) = &quot;C&quot;
codeABox.Visible = nameDatabase.Column(Value) = &quot;C&quot;

End Sub

... I have to mention that I have not been able to test this code without having the wider code-base to work it within.

huangapple
  • 本文由 发表于 2023年7月27日 19:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定