Combobox populate textbox?

Tacec

Member
Joined
Jun 14, 2005
Messages
18
Location
Renton, WA
Programming Experience
1-3
Hello,

I know this is a fairly simple process, but i can't seem to find anything to help me. I have a combobox with a list of values (non bound). I have a textbox. I want the value when selectedvaluechanged in the combobox to populate the textbox and keep appending to the textbox whenever another item is selected in the combobox. Each item listed in the textbox needs to be separated by commas.

Fairly straightforward, right? I can't find any code for vb, only c#. The basic for what i have is just combobox.text = textbox.text. But it's the append part that's got me.

I know there must be a foreach loop in there or something but I'm pretty new to vb.net so i'm struggling here. Any help would be greatly appreciated!

Thanks
 
Last edited:
Oops, thanks, you're right. I switched those two around. Here's what i have so far and it doesn't append anything, it just keeps changing the text in the textbox to what i select in the combobox:

VB.NET:
Private Sub AppendTextBox1Text()
        If TextBox1.Text = "" Then
            Return
        Else
            TextBox1.AppendText(UiComboBox1.SelectedValue)
        End If
    End Sub

    Private Sub UiComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UiComboBox1.SelectedIndexChanged
        AppendTextBox1Text()
        TextBox1.Text = UiComboBox1.Text

    End Sub

This doesn't even address the commas i need to separate the text.

Help?
 
You append something in the AppendTextBox1Text method but then you blow that away immediately after by setting the Text property explicitly. Do NOT set the Text property at all. Call AppendText ONLY.
 
Also, you will have to think a bit harder how that's going to work because what if the user uses the keyboard to scroll through the items in the ComboBox? The way you're trying to do it will add every item to the TextBox.
 
Thank you for your help.

Unfortunately, if i just call the AppendText, nothing happens at all. If i have the code like this, it just lists the selection in the textbox twice. If another combobox item is selected, it just replaces the duplicate item in the textbox with another duplicate:

VB.NET:
Private Sub UiComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UiComboBox1.SelectedIndexChanged
        TextBox1.Text = UiComboBox1.Text
        AppendTextBox1Text()
    End Sub

What the heck am i missing here? For some reason I thought this might be a fairly simple process to do, but i seem to be failing miserably!
 
Ok, after dinking around with it a little i found quite a simple answer:

VB.NET:
 Private Sub UiComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UiComboBox1.SelectedIndexChanged
        TextBox1.AppendText(UiComboBox1.SelectedValue)
    End Sub

I feel a tad bit dense at this point, (overthinking this perhaps?). Thanks for your patience.

As for the comma, i added this:

VB.NET:
Private Sub UiComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UiComboBox1.SelectedIndexChanged
        TextBox1.AppendText(UiComboBox1.SelectedValue + ",")
    End Sub

But obviously I won't need a comma if it's the last item in the textbox. How would i check for that??
 
It's not that you don't need the comma if it's the last item in the TextBox. It's that you don't need the comma if it's the FIRST item in the TextBox. If there's only one item then you don't need any comma.
VB.NET:
Dim text As String = CStr(UiComboBox1.SelectedValue)

If Me.TextBox1.TextLength > 0 Then
    text = ", " & text
End If

Me.TextBox1.AppendText(text)
 
Back
Top