vb.net combox headache

mike7510uk

Member
Joined
Apr 29, 2005
Messages
6
Programming Experience
Beginner
hi all...can anyone please help me?

I have 2 combo boxes on my form of which i have entered the relevant data into the "collections" part of its properties. When i select an item on combo box 1 i want it to change what can be selected in combo box 2.

any ideas????
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Clear and Add or bind items to combo 2 in the selectedindex change event of combo1.
 

mike7510uk

Member
Joined
Apr 29, 2005
Messages
6
Programming Experience
Beginner
i am very new to vb.net and didnt quite understand what you meant
cant i use "if" statements? e.g "if cbo1 = sometext then cbo2 = the required text"???
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Well you could but those if statments will need to be somewhere...
It would also be easier if you used a select case instead of a bunch of if's.
Also you want to change the items in the combo not the text, so you'll need to clear it and add new ones.

Here's an eg of a select case:
VB.NET:
select case cbo1.selecteditem
	case "item1"
...add items...
	case "item2"
...add different items....
end select
 

mike7510uk

Member
Joined
Apr 29, 2005
Messages
6
Programming Experience
Beginner
nope i still not getting it.
Here is an example of what it is i want it to do:


combobox 1 has 3 items in it (which were entered into the "collection" area of the combobox properties)
when the user selects an item i want combobox 2 (which has 4 items in it entered the same way as above) to display the top 2 choices if the first option was selected from cbo1. If the 2nd of 3rd option was select from cbo1 then cbo2 must display only the bottom 2 options.

This is why i thought "if" statements were the way to go
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,328
Location
Sydney, Australia
Programming Experience
10+
You cannot hide and show items in a ComboBox. You must add and remove them as necessary, so you must have them stored somewhere else, like an array. The best option is to use the SelectedIndexChanged event handler of cbo1 to Clear cbo2 and then re-add only the items you need from the array. You can use TPM's code to re-add the required items.
VB.NET:
Dim cbo2Items As String() = {"item1", "item2", "item3", "item4"}

Private Sub cbo1_SelectedIndexChanged(...) Handles cbo1.Click
	cbo2.Items.Clear()

	Select Case cbo1.SelectedIndex
		Case 0
			cbo2.Items.Add(cbo2Items(0))
			cbo2.Items.Add(cbo2Items(1))
		Case 1, 2
 			cbo2.Items.Add(cbo2Items(2))
 			cbo2.Items.Add(cbo2Items(3))
	End Select
End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,328
Location
Sydney, Australia
Programming Experience
10+
What do you mean by "clear cbo2"? If the DropDownStyle of a ComboBox is set to DropDown, calling Items.Clear() removes the items from the drop down list but does not clear the text in the field. If it is set to DropDownList, i.e. you cannot type something that is not in the list into the field, calling Items.Clear() will clear the text too. Try stepping through the code and check the ComboBox Text and Items.Count properties before and after the call to Items.Clear().

You can use SelectedIndex with the index of each item or SelectedItem with the text of each item. It amounts to the same thing.
 

ricio2000

Member
Joined
May 2, 2005
Messages
18
Location
Hialeah, FL
Programming Experience
1-3
I had this same issue yesterda, but nobody really helped me, but I will help you. If you are familiar with ADO.net specifically an SqlDataAdapter and a DataSet. I did this using the DataSource property of the ComboBox class. I have a companytype comboBox and a company ComboBox. Certain companies are filled based on the companytype, that is comboBox containg customer information is auto populated each time something different is selected from the customertype combobox. You basically perform the updating in the selected index change event. You can clear the combobox everytime the user changes the first combobox. I was pulling the data from a dataset. So i needed to clear the dataset and clear the combobox in the selectedindexchange event. So that the information was duplicated because i would call fill each time. I've been exposed to vb.net programmers and I'm been doing alot of code. Stick with it. This is alot of practice and heavy detail reading.
 
Top Bottom