Select Case

ddady

Member
Joined
Sep 19, 2006
Messages
20
Programming Experience
Beginner
Hi again,

I'm facing a tough issue.

I have 2 combobox with a couple of items in each, now, i need the best and shortest way to define calculations between each and every item. Just for an example:

combobox1
1
2
3

combobox2
1
2
3

i need for item 1 in combobox1 to have some sort of calculation with all 3 items in combobox2 such as (1+2, 1+2, 1+3) and so on (2+1, 2+2, 2+3)....

Now is the select case statment should work it? and how? or there is a better way?

Hope i have explained myself good enough :p
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Try this.....
VB.NET:
Dim I as Integer = 0 
For I As Integer = 0 To Me.ComboBox2.Items.Count -1
 
I = Convert.ToInt32(Me.Combobox1.Items(0)) + Convert.ToInt32(Me.Combobox2.Items(I))
 
MessageBox.Show(The Value Of  ComboBox1 Item 0 Plus ComboBox2 Item " + Convert.ToString(ComboBox2.Items(i)) 
+ " = " + i.tostring")
 
Next

Assuming if coded this correctly, it will loop through all the items in combobox2 convert them to an integer and add that number to the first item in combobox1. But i've just knocked this up without testing it and it will be very embarressing if i've cocked it up:)

Going on your code above it should popup three message boxes, one after the other, and display how clever at maths your computer is!:) If you want to loop thorugh all the items in both comboboxes then you can add a second loop inside the first loop...


VB.NET:
For I as Integer = 0 to Me.Combobox1.Item.Count-1
For A as Integer = 0 To Me.Combobox2.Items.Count-1
 
 
'Add Math Calculations Here.....
 
 
next
next

So my little snippet above will start a loop through the items in combobox1's item collection, but before it goes to the 'next' statement it will run a loop through all the items in combbox2's collection. So therefore you will be able to comapre all the items in combbox2's collection against the first item in combobox1's collection, then it will process the second item in combobox1's collection and you can add that to all the items in combobox2's collection. Confused Yet?
 

ddady

Member
Joined
Sep 19, 2006
Messages
20
Programming Experience
Beginner
things that makes you go hmmmm....

Well, i got confused.

I guess it was my fault, i have tried to simplify my question but i only got it more confused.

In general i'm trying to do a length convertor from metric to inch and
vice versa. In combobox2 i have the list of lengths (cm,mm,inch,yard,etc..)
from it i choose what kind of length i want to convert from.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Length_Button_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE]
[SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] LengthBt.Click[/SIZE]
 
[SIZE=2]ComboBox2.Text = ([/SIZE][SIZE=2][COLOR=#800000]"choose"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Clear()[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"mm"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"cm"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"m"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"km"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"inch'"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"foot"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"yard"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ComboBox2.Items.Add([/SIZE][SIZE=2][COLOR=#800000]"mile"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

In combobox1 the list of the rest of the items from combobox2 are displayed in order for
me to choose to what length to convert to.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ComboBox2_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ComboBox2.SelectedIndexChanged[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] val [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'index value of item[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] num [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] count [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'number of items in each list[/COLOR][/SIZE]
[SIZE=2]val = ComboBox2.SelectedIndex[/SIZE]
[SIZE=2]count = ComboBox2.Items.Count[/SIZE]
[SIZE=2]ComboBox1.Items.Clear()[/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] num = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] count - 1[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] num <> val [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]ComboBox1.Items.Add(ComboBox2.Items.Item(num))[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

Now, for each convertion there is a different calculation for an example [i can choose from combobox2 "mm" and to convert it to "inch" from combobox1 and so on. the conversion will happen and the result will appear once i press the convert button.

What i needed was the best and shortest way to make it happen.
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Try this..

VB.NET:
For I as integer = 0 to me.combobox1.items.count -
 
If Not me.combobox1.Items(i).equals(me.combobox1.SelectedItem) Then
Me.combbox2.Items.Add(Me.Combobox1.Items(I))
 
Next
 

ddady

Member
Joined
Sep 19, 2006
Messages
20
Programming Experience
Beginner
wouldn't that give the same result as my code for filling the second combobox with the rest of the items that were left from the first one?

And what's the "me" stands for?
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
"Me" stands for the current instance of the class the method or property is located in

for example to change the form's WindowState from a button click on that form you would (in the button's click event" use "Me.WindowState = " because "Me" refers to that class (which is a form in this case)
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Me -> as in the class your are coding for.

And err yes it would give you the same result, it was that bit i thought you were having trouble with? I guess not.. So which bit are you having problems with? The actual conversion? or how to get the values from the comboboxes and compare them in order to use the correct conversion function? if it's the latter then, yes, you could use a Select Case statement in this instance.
 
Top Bottom