Need help on combo box & label!!!

alexkoh

New member
Joined
Mar 19, 2005
Messages
3
Programming Experience
Beginner
hi everyone.

currently im doing a page for displaying some value.

i have a combo box and a label. the combo box have a few choices and whenever user select them. i wish to display some value in the label. i had tried some way n stil unable to solve it.

the code below is for my combo box. jus wondering if anyone could help mi with it? thanks alot. =)

VB.NET:
[font='Courier New']  [color=blue]Private[/color] [color=blue]Sub[/color] ComboBox1_MouseUp([color=blue]ByVal[/color] sender [color=blue]As[/color] [color=blue]Object[/color], [color=blue]ByVal[/color] e [color=blue]As[/color] System.Windows.Forms.MouseEventArgs) [color=blue]Handles[/color] ComboBox1.MouseUp[/font]

[font='Courier New']		[color=blue]If[/color] ComboBox1.Text = "Mon" [color=blue]Or[/color] "Tue" [color=blue]Or[/color] "Wed" [color=blue]Or[/color] "Thur" [color=blue]Then[/color][/font]

[font='Courier New']			price.Text = "6.5"[/font]

[font='Courier New']		[color=blue]Else[/color][/font]

[font='Courier New']			[color=green]'If ComboBox1.Text = "Fri" Or "Sat" Or "Sun" Then[/color][/font]

[font='Courier New']			price.Text = "8"[/font]

[font='Courier New']		[color=blue]End[/color] [color=blue]If[/color][/font]

[font='Courier New']	[color=blue]End[/color] [color=blue]Sub[/color][/font]
 

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
Here is the answer you want.

DoubleClick the combobox in the IDE and choose you should be in the selected Index Changed event sub routine. If not, goto the top right adn select that event in the dropbox up top. Then enter the following code.

If ComboBox1.SelectedItem = "Mon" OrElse ComboBox1.SelectedItem = "Tue" OrElse ComboBox1.SelectedItem = "Wed" OrElse ComboBox1.SelectedItem = "Thur" Then

price.Text = "6.5"

Else

price.Text = "8"

End If

End Sub


I also use OrElse as opposed to Or for short circtuit evaluation but that's not necessary here. I would also look into making an Enumuration for the days of the week to make the code more concise and clean. It will also help you if you need to reuse code rather that typing all the If Or statements. A simple Select Case would work.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
There's already an enumeration for the days of the week:
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ComboBox1.DataSource = System.Enum.GetNames(GetType(DayOfWeek))
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Select Case Me.ComboBox1.SelectedIndex
            Case 1 To 4
                'Display the price in the local currency format.
                price.Text = (6.5).ToString("c")
            Case Else
                price.Text = (8).ToString("c")
        End Select
    End Sub
 
Top Bottom