N
Nikolay Petrov
I have asked a question before some time, and a guy responded to it. His
answer raised new questions and I replied them back, but still no answer. I
need them urgently so I am to post the whole thing again. I've found that
nobody looks at the older threads ;-(
My Question was:
I have a Combo box, binded to a dataset
With cmbCompany
.DataSource = dsSpecContact
.DisplayMember = "Companies.CompanyName"
.ValueMember = "Companies.CompanyID"
End With
What I want is to change the SelectedIndex of this Combo Box by using the
ValueMember or DisplayMemver values. I mean the "Companies.CompanyID" or
"Companies.CompanyName".
Some how I have to iterate through all the item in Combo Box and find which
item has ValueMember = "Companies.CompanyID" or DisplayMember =
"Companies.CompanyName" and select it.
And the answer:
You could use a hashtable to store the index-ID mappings. something like:
For Each oCompany as Company In Companies
oHash.Add(oCompany.CompanyID, cmbCompany.Items.IndexOf(oCompany))
Next oCompany
When you need the index for a given CompanyID, you just do:
Dim index As Integer = CType(oHash.Item(CompanyID), Integer)
This would work fine for a fixed combobox where you're not going to be
adding-removing items at runtime. If you are going to be adding-removing
items, you'll need to update the hashtable to reflect these changes.
Also, for a combobox with not a lot of items (probably 10 to 15 items), it
wouldn't be too bad to loop through the combobox to retrieve the index since
populating and maintaining the hashtable does add some overhead anyway.
And my two new questions:
1.
I don't understand
===
For Each oCompany as Company In Companies
oHash.Add(oCompany.CompanyID, cmbCompany.Items.IndexOf(oCompany))
Next oCompany
===
Could someone declare items oCompany, Company, Companies?
2.
How to loop through the Combo Box items?
I know only the following method:
===
Dim itm as object
for each itm in ComboBox1.items
....
....
next
===
But this method does not provide me a way to access items properties,
because they are defined as Object
answer raised new questions and I replied them back, but still no answer. I
need them urgently so I am to post the whole thing again. I've found that
nobody looks at the older threads ;-(
My Question was:
I have a Combo box, binded to a dataset
With cmbCompany
.DataSource = dsSpecContact
.DisplayMember = "Companies.CompanyName"
.ValueMember = "Companies.CompanyID"
End With
What I want is to change the SelectedIndex of this Combo Box by using the
ValueMember or DisplayMemver values. I mean the "Companies.CompanyID" or
"Companies.CompanyName".
Some how I have to iterate through all the item in Combo Box and find which
item has ValueMember = "Companies.CompanyID" or DisplayMember =
"Companies.CompanyName" and select it.
And the answer:
You could use a hashtable to store the index-ID mappings. something like:
For Each oCompany as Company In Companies
oHash.Add(oCompany.CompanyID, cmbCompany.Items.IndexOf(oCompany))
Next oCompany
When you need the index for a given CompanyID, you just do:
Dim index As Integer = CType(oHash.Item(CompanyID), Integer)
This would work fine for a fixed combobox where you're not going to be
adding-removing items at runtime. If you are going to be adding-removing
items, you'll need to update the hashtable to reflect these changes.
Also, for a combobox with not a lot of items (probably 10 to 15 items), it
wouldn't be too bad to loop through the combobox to retrieve the index since
populating and maintaining the hashtable does add some overhead anyway.
And my two new questions:
1.
I don't understand
===
For Each oCompany as Company In Companies
oHash.Add(oCompany.CompanyID, cmbCompany.Items.IndexOf(oCompany))
Next oCompany
===
Could someone declare items oCompany, Company, Companies?
2.
How to loop through the Combo Box items?
I know only the following method:
===
Dim itm as object
for each itm in ComboBox1.items
....
....
next
===
But this method does not provide me a way to access items properties,
because they are defined as Object