Question textbox.text = (combobox selection 1)

gho5t

Member
Joined
Aug 26, 2013
Messages
13
Programming Experience
1-3
Hello everyone,

I am trying to have a text box = what the combo box selection is,
Has anyone got a solution to achieve this?
I can't really find the correct syntax to achieve so.

Thank-you in advanced :p

Private Sub systemcmbo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles systemcmbo.SelectedIndexChanged


If systemcmbo.SelectedItem = 1 Then


End If


End Sub
 
There's no If statement required. You simply get the item selected and display it in the TextBox. The question is, do you want to display the same text in the TextBox as is displayed in the ComboBox or do you want to display some other aspect of the selected item, e.g. you have bound a list of Person objects to the ComboBox and it displays the Name properties and you want to display the corresponding DateOfBirth properties in the TextBox? If it's the former, you simply assign the Text of the ComboBox to the Text of the TextBox. If it's the latter then we'll need more details to give you an exact solution but it would look something like this:
Dim item = DirectCast(ComboBox1.SelectedItem, SomeType)

If item IsNot Nothing Then
    TextBox1.Text = item.SomeProperty.ToString()
End If
 
There's no If statement required. You simply get the item selected and display it in the TextBox. The question is, do you want to display the same text in the TextBox as is displayed in the ComboBox or do you want to display some other aspect of the selected item, e.g. you have bound a list of Person objects to the ComboBox and it displays the Name properties and you want to display the corresponding DateOfBirth properties in the TextBox? If it's the former, you simply assign the Text of the ComboBox to the Text of the TextBox. If it's the latter then we'll need more details to give you an exact solution but it would look something like this:
Dim item = DirectCast(ComboBox1.SelectedItem, SomeType)

If item IsNot Nothing Then
    TextBox1.Text = item.SomeProperty.ToString()
End If

I'm trying to learn the concept behind applying a variable to a combo box selection.

In an overall view of things I am eventually looking to have a system similar to the following:

(Please excuse my concepts being incorrect)
But I am looking to have a combo box selection load a set of different boxes.


So if selection1 in the combobox is selected then display "cubes.text", "qty1.text", "plastics1.text", "qty2.text"

or if selection2 in the combobox is selected then display "plastics2.text", "qty3.text", plastics3.text", "qty4.text"

Is this possible?
I believe i understand the concept behind what i wish to achieve just a matter of actually implementing it.

What class would I use?
 
I'm not 100% sure what you mean but I think that it's just a simple matter of data-binding. Try this:

1. Create a new Windows Forms Application project.
2. Add a ComboBox and a TextBox to the form.
3. Open the code windows and paste in this code:
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim people = {New Person With {.Name = "Peter", .DateOfBirth = #1/1/1981#},
                      New Person With {.Name = "Paul", .DateOfBirth = #2/2/1982#},
                      New Person With {.Name = "Mary", .DateOfBirth = #3/3/1983#}}

        With Me.ComboBox1
            .DisplayMember = "Name"
            .ValueMember = "DateOfBirth"
            .DataSource = people
        End With

        Me.TextBox1.DataBindings.Add("Text", people, "DateOfBirth")
    End Sub

End Class


Public Class Person
    Public Property Name As String
    Public Property DateOfBirth As Date
End Class
4. Run the project and select different values in the ComboBox.

Notice how selecting a Name in the ComboBox automatically displays the corresponding DateOfBirth in the TextBox? That's part of what data-binding can do for you.
 
In future, please simply attach an image to your post, so we can see it without having to leave the current page.

In that case, what you're actually trying to do is hide and show controls, e.g.
Select Case Me.ComboBox1.SelectedIndex
    Case 0
        Me.Label1.Show()
        Me.Label2.Hide()
    Case 1
        Me.Label1.Visible = False
        Me.Label2.Visible = True
End Select
 
Back
Top