You already have an answer, but I think the code can be improved upon a little. As a rule of thumb, if I'm going to be doing a check against a single variable for more than a couple items in a list, I use Select Case
rather than an ongoing list of ElseIf
statements. The number one reason being that if you change something on the first one, you'll have to remember to change it on all the others. CodeGray has solved some of this issue by declaring the value as a local variable, but the readability still stands to gain, so try this instead:
Select Case C_job.SelectedItem.ToString
Case "Internet"
t_amount.Text = "20"
Case "Games"
t_amount.Text = "10"
Case "Print (short)"
t_amount.Text = "1"
Case "Print (long)"
t_amount.Text = "2"
End Select
Also, i just wanted to check that you were using the Display Member
and the Value Member
of the ComboBox correctly. You can expose two properties with native support in combo boxes, one a value to display to the user, and a second a backing property. When you're executing code, you'll have access to both. The plus side is you can store this information in a more declarative way and then react accordingly.
When the ComboBox has a selection, that information is stored in the SelectedItem
property which stores the original object used to populate that drop down item. If you've defined a DisplayMember
and ValueMember
for combo box items, then the SelectedText
and SelectedValue
will expose just those properties for the selected object.
Here's the Setup
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dataSource As New DataTable
With dataSource.Columns
.Add("Display", GetType(String))
.Add("Value", GetType(Integer))
End With
With dataSource.Rows
.Add("Internet", 20)
.Add("Games", 10)
.Add("Print (short)", 1)
.Add("Print (long)", 2)
End With
With C_job
.DisplayMember = "Display"
.ValueMember = "Value"
.DataSource = dataSource
End With
End Sub
The plus side, is now here's all the code you need to handle the event
Private Sub C_job_SelectedIndexChanged(sender As Object, e As EventArgs) Handles C_job.SelectedIndexChanged
t_amount.Text = C_job.SelectedValue
End Sub
If you wanted to, this will also make binding this property very easy since you've coded all your logic upfront.