Combo Box

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
 
C

Cor Ligthert

"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 ;-(

Maybe the last sentence is in another context. However when it is about
people who answer you, than this is absolute not true. Most regulars in this
newsgroup look at the older threads when they are involved So nobody is a
little bit overdone. However maybe is what you mean something else and than
probably I agree with you.

I look now at your question and the answer. In my opinion was a better
answer to tell you that there is a combobox.findexact and even better
combobox.findstringexact and that you can use for the rest the normal set
selectedvalue

http://msdn.microsoft.com/library/d...owsformscomboboxclassfindstringexacttopic.asp

It is much easier, see this sample beneath I made for you about this, the
start is making the table.

I hope this helps you?

Cor

\\\needs one combobox and 2 buttons on a form.
Private dt As New DataTable
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("CompanyName")
dt.Columns.Add("CompanyId")
For i As Integer = 0 To 5
Dim dr As DataRow = dt.NewRow
dr(0) = ChrW(i + 65)
dr(1) = i + 1
dt.Rows.Add(dr)
Next
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "CompanyName"
ComboBox1.ValueMember = "CompanyId"
End Sub

Private Sub Button1_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.SelectedValue = 3
If ComboBox1.SelectedIndex <> -1 Then
MessageBox.Show(ComboBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim myindex As Integer = ComboBox1.FindStringExact("D")
If myindex <> -1 Then
ComboBox1.SelectedIndex = myindex
MessageBox.Show(ComboBox1.SelectedValue.ToString)
End If
End Sub
End Class
///
 
N

Nikolay Petrov

That works fine, thanks Cor


Cor Ligthert said:
"Nikolay Petrov"
answer.

Maybe the last sentence is in another context. However when it is about
people who answer you, than this is absolute not true. Most regulars in this
newsgroup look at the older threads when they are involved So nobody is a
little bit overdone. However maybe is what you mean something else and than
probably I agree with you.

I look now at your question and the answer. In my opinion was a better
answer to tell you that there is a combobox.findexact and even better
combobox.findstringexact and that you can use for the rest the normal set
selectedvalue

http://msdn.microsoft.com/library/d...owsformscomboboxclassfindstringexacttopic.asp

It is much easier, see this sample beneath I made for you about this, the
start is making the table.

I hope this helps you?

Cor

\\\needs one combobox and 2 buttons on a form.
Private dt As New DataTable
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("CompanyName")
dt.Columns.Add("CompanyId")
For i As Integer = 0 To 5
Dim dr As DataRow = dt.NewRow
dr(0) = ChrW(i + 65)
dr(1) = i + 1
dt.Rows.Add(dr)
Next
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "CompanyName"
ComboBox1.ValueMember = "CompanyId"
End Sub

Private Sub Button1_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.SelectedValue = 3
If ComboBox1.SelectedIndex <> -1 Then
MessageBox.Show(ComboBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim myindex As Integer = ComboBox1.FindStringExact("D")
If myindex <> -1 Then
ComboBox1.SelectedIndex = myindex
MessageBox.Show(ComboBox1.SelectedValue.ToString)
End If
End Sub
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top