Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable
Set ComboBox choice as variable Set ComboBox choice as variable
Set ComboBox choice as variable
Go Back  Xtreme Visual Basic Talk > > > Set ComboBox choice as variable


Reply
 
Thread Tools Display Modes
  #1  
Old 02-17-2009, 01:50 PM
thisisdallas thisisdallas is offline
Newcomer
 
Join Date: Feb 2009
Posts: 2
Default Set ComboBox choice as variable


Ok so im just starting to learn visual basic and i need some help. I made a combo box and i need to know how to set one variable for each choice in the combo box. for example, my choices are poor, average, good, and superior. I want to set the "poor" variable as the poor option in the combo box and the "average" variable as the average option in the combo box and so on and so forth. So if someone selects the poor option in the combo box it sets that as the value for the poor varialbe. Then I want to use an if statement to display a messagebox. i want a different messagebox to pop up for each different choice in my combobox. Like if the user selects "good" from the combo box and then clicks the results button i want a box to pop up saying "specific text for the good variable" if the user picks "average" and then clicks the button then a want a box to pup up saying "specific text for the average variable". so far i have this

dim good as string
dim average as string

good = combobox.text
average = combobox.text

if good = combobox.text then
messagebox.show("specific text")

if average = combobox.text then
messsagebox.show("different specific text")


the result is no matter what choice i pick two boxes pop up, one saying you are good and one saying you are average. i understand its because i set both variables as the combobox.text but i dont know how to fix this haha.

thanks for any help!
Reply With Quote
  #2  
Old 02-17-2009, 03:14 PM
user name user name is offline
Junior Contributor
 
Join Date: Sep 2004
Posts: 264
Default

I would use the index of the selected item. The items in the combobox start from index zero onwards, so the first item which as the text "Poor" would be index zero, the second item as the text "Average" would have an index of 1, and so on. If nothing is selected then the index would be -1.

Knowing that, you can check the index selected and act on that.
Code:
Select Case ComboBox1.SelectedIndex
            Case -1
                MessageBox.Show("Nothing Selected")
            Case 0
                MessageBox.Show("Poor")
            Case 1
                MessageBox.Show("Average")
            Case 2
                MessageBox.Show("Good")
            Case 3
                MessageBox.Show("Superior")
        End Select
Welcome to the forum
Reply With Quote
  #3  
Old 02-17-2009, 04:01 PM
thisisdallas thisisdallas is offline
Newcomer
 
Join Date: Feb 2009
Posts: 2
Default

Great! I didnt know you could do that, thanks. but now what if i need to display a messagebox that depended on two variables. like the variable gpa being greater than 3.25 and the user choosing "Good"? could i do something like

if gpa >= 3.25 AndAlso ComboBox1.SelectedIndex Case 2 Then
messagebox.show("text")
Reply With Quote
  #4  
Old 02-17-2009, 04:56 PM
AtmaWeapon's Avatar
AtmaWeaponSet ComboBox choice as variable AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

The part you're missing about If statements is that there is such a thing as If..Else and If..ElseIf. Select..Case is only useful for comparing one condition.

user_name could have written this:
Code:
If ComboBox1.SelectedIndex = -1 Then
    MessageBox.Show("Nothing Selected")
ElseIf ComboBox1.SelectedIndex = 0
    MessageBox.Show("Poor")
ElseIf...
This is usually tedious compared to a Select..Case statement, but if you want to compare multiple conditions you're stuck using If..ElseIf.

Still, there might be some simplification here. What's the relationship between gpa and ComboBox1.SelectedIndex? Things work much better if you describe what you want, then write the code that does that instead of just writing code. If, for example, you only want to show a message when the GPA is above 3.25, the hard way is like this:
Code:
If gpa >= 3.25 AndAlso ComboBox1.SelectedIndex = -1 Then
    MessageBox.Show("Nothing")
Else If gpa >= 3.25...
The easy way is like this:
Code:
If gpa >= 3.25 Then
    Select Case ComboBox1.SelectedIndex
        Case -1
            MessageBox.Show("Nothing")
...
So make sure you aren't making your job hard when constructing these conditional statements!
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 02-17-2009, 07:23 PM
hawkvalley1's Avatar
hawkvalley1 hawkvalley1 is offline
Centurion
 
Join Date: May 2008
Location: Denver, CO USA
Posts: 190
Default

How about:
In the ComboBox_SelectedIndexChanged event...
Dim num as Double = Format(Cdbl(txtGPA.text), "#.##")
Dim poor as string = "Poor"
Dim good as string = "Good"
Dim exceptional as string = "Exceptional"
Select Case num
Case 0.00 to 1.00
If cb1.selectedIndex = 0 then
MsgBox("Your score is " & poor)
ElseIf cb1.selectedIndex = 1 then
MsgBox(" Your score is " & good)...
Case 1.01 to 2.00
MsgBox("something") '... as above
Case 2.01 to 3.00 ' as above
Etc....
This would give you the MsgBox for whichever number was typed into the textbox and which combobox was selected.
Also you could use the Keypress event if you need to keep the txtGPA.text a number and not letters, I can give example if this would work...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable Set ComboBox choice as variable
Set ComboBox choice as variable
Set ComboBox choice as variable
 
Set ComboBox choice as variable
Set ComboBox choice as variable
 
-->