How do I... ?

My question is a little hard to word, so I'll just use an example:

Say that I click a combo box and 7 items appear.

The items are:

O Class
B Class
A Class
F Class
G Class
K Class
M Class

Note: These are the 7 main star groups.

There are also 6 labels:

Temperature, Star color, Mass, Radius, and Luminosity, and Hability Zone

What I want is: if I choose O class, a textbox (or something) next to Mass should read 60.

I have not yet learned the Visual Basic, but I should get this one out the way right now.

If you do not understand the question well, please tell me.

Sincerely,

Mirai Naza




Answer this question

How do I... ?

  • Gloria123

    I think you'll find the difference is the first approach is simply reflecting the contents of the combobox in the textbox1 using a simple select case statement. THis is fine as long as you only want to use a single value in a textbox to update a control.

    The difference in the code I have shown is its using and object which stores a number of related Items. You combobox shows one item but when you select it - it uses the other items in the underlying object to set multiple controls with values.

    THis is why the code shown here is different. You'll also note that there is no hardcoded values except to initialize the code. Whereas in the other code there are hardcoded values within the code. Maybe for small apps this may not be a problem but having hardcoded values in can make you code much more difficult to maintain if something changes or you want to use it with different data.


  • IlCapo

    spotty wrote:

    My thought here is you trying to have a listbox which will show you various details when one of the items is selected.

    For this I have created a simple class and use it to populate a listbox and when an item is selected in ther listbox it will display the corresponding details in other controls (in this case labels). This can form the basis of this and you can add other properties or adjust the controls your using.

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '//Initialize values - I have no idea real values
    Me.ListBox1.Items.Clear()
    Me.ListBox1.Items.Add(New StarGroup("0", "60", "Blue", "1"))
    Me.ListBox1.Items.Add(New StarGroup("A", "70", "Reg", "2"))
    Me.ListBox1.Items.Add(New StarGroup("B", "80", "green", "3"))
    Me.ListBox1.Items.Add(New StarGroup("C", "90", "White", "4"))
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    '//Display Selected Item
    Dim x As StarGroup
    If Me.ListBox1.SelectedItem IsNot Nothing Then
    x = Me.ListBox1.SelectedItem

    Label1.Text = x._Name
    Label2.Text = x._Starcolor
    Label3.Text = x._Temperature
    Label4.Text = x._Mass
    Else
    Label1.Text = ""
    Label2.Text = ""
    Label3.Text = ""
    Label4.Text = ""
    End If
    End Sub
    End Class


    Class StarGroup
    Public _Name As String
    Public _Temperature As String
    Public _Starcolor As String
    Public _Mass As String

    Sub New(ByVal Name As String, ByVal Temperature As String, ByVal Starcolor As String, ByVal Mass As String)
    _Name = name
    _Temperature = Temperature
    _Starcolor = Starcolor
    _Mass = Mass
    End Sub

    '//This is what will display in the listbox
    Public Overrides Function ToString() As String
    Return _Name
    End Function
    End Class

    Sorry. Some one got to it first. But, thanks.



  • Richc12345

     Spidermans_DarkSide wrote:

    Hi,

    Presuming you have the items in the order you stated then do something like this.>>

     

     

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    Dim I As Integer

    I = ComboBox1.SelectedIndex

    Select Case I

    Case 0

    ' O class selected.

    Textbox1.text = "60"

    Case 1

    'B class selected.

    TextBox1.Text = "B"

    Case 2

    'A class selected.

    TextBox1.Text = "A"

    Case 3

    'F class selected.

    TextBox1.Text = "F"

    Case 4

    'G class selected.

    TextBox1.Text = "G"

    Case 5

    'K class selected.

    TextBox1.Text = "K"

    Case 6

    'M class selected.

    TextBox1.Text = "M"

    End Select

    End Sub

     

    Regards,

    S_DS

     

     

    Alright.  Thanks.  I'll try that out and get back to you soon.

     

    Hey!  That works perfectly!  Thanks!

     

    My next venture: natural selection thing.  But, that's for another time.



  • fighter92

    My thought here is you trying to have a listbox which will show you various details when one of the items is selected.

    For this I have created a simple class and use it to populate a listbox and when an item is selected in ther listbox it will display the corresponding details in other controls (in this case labels). This can form the basis of this and you can add other properties or adjust the controls your using.

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '//Initialize values - I have no idea real values
    Me.ListBox1.Items.Clear()
    Me.ListBox1.Items.Add(New StarGroup("0", "60", "Blue", "1"))
    Me.ListBox1.Items.Add(New StarGroup("A", "70", "Reg", "2"))
    Me.ListBox1.Items.Add(New StarGroup("B", "80", "green", "3"))
    Me.ListBox1.Items.Add(New StarGroup("C", "90", "White", "4"))
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    '//Display Selected Item
    Dim x As StarGroup
    If Me.ListBox1.SelectedItem IsNot Nothing Then
    x = Me.ListBox1.SelectedItem

    Label1.Text = x._Name
    Label2.Text = x._Starcolor
    Label3.Text = x._Temperature
    Label4.Text = x._Mass
    Else
    Label1.Text = ""
    Label2.Text = ""
    Label3.Text = ""
    Label4.Text = ""
    End If
    End Sub
    End Class


    Class StarGroup
    Public _Name As String
    Public _Temperature As String
    Public _Starcolor As String
    Public _Mass As String

    Sub New(ByVal Name As String, ByVal Temperature As String, ByVal Starcolor As String, ByVal Mass As String)
    _Name = name
    _Temperature = Temperature
    _Starcolor = Starcolor
    _Mass = Mass
    End Sub

    '//This is what will display in the listbox
    Public Overrides Function ToString() As String
    Return _Name
    End Function
    End Class


  • Sergio L.

    Hi,

    Presuming you have the items in the order you stated then do something like this.>>

     

     

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    Dim I As Integer

    I = ComboBox1.SelectedIndex

    Select Case I

    Case 0

    ' O class selected.

    Textbox1.text = "60"

    Case 1

    'B class selected.

    TextBox1.Text = "B"

    Case 2

    'A class selected.

    TextBox1.Text = "A"

    Case 3

    'F class selected.

    TextBox1.Text = "F"

    Case 4

    'G class selected.

    TextBox1.Text = "G"

    Case 5

    'K class selected.

    TextBox1.Text = "K"

    Case 6

    'M class selected.

    TextBox1.Text = "M"

    End Select

    End Sub

     

    Regards,

    S_DS

     



  • How do I... ?