?
Solved

displaying array values on combobox selectedindexchanged event?

Posted on 2006-11-29
6
Medium Priority
?
290 Views
Last Modified: 2010-05-18
I have a combobox populated by an array datasource, and a label.

Me.ComboBox1.DataSource = myPrintersArray
Me.ComboBox1.DisplayMember = "DeviceID"
Me.ComboBox1.ValueMember = "DeviceID"

myPrintersArray holds the "DeviceID" and "Location" of several printers

On the selectedindexchanged event I would like to display the "Location" of the selected Printer.

Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
      Me.Label1.Text =
End Sub

If I change .ValueMember to ="Location", this is easy enough. But can it be done without passing the Location into the Combobox itself? I ask because I may have to expand the array to include the "Name" of the printers. So .DisplayMember would be "Name", .ValueMember would be "DeviceID", and I could not specify "Location" anywhere in the Combobox.

Thanks!
0
Comment
Question by:jaw0807
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
Hillwaaa earned 200 total points
ID: 18041224
Hi jaw0807,

I'd suggest putting your printer info into a HashTable, with key = "DeviceID" and value = "Location".  That way you can do the following:

Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
      Me.Label1.Text = myPrintersHashtable.Item(ComboBox1.Text)
End Sub

Cheers,
Hillwaaa
0
 

Author Comment

by:jaw0807
ID: 18041492
Thank you for the reply, Hillwaaa.

I haven't had much experience with hashtables and can't seem to get this to work. I'm creating the printer collection with a for each loop.

Here's the code snippets:

Public myPrintersHashTable As New HashTable
...
For Each queryObj As ManagementObject In searcher.Get()
     Dim deviceID = queryObj("DeviceID")
     Dim deviceLoc = queryObj("Location")              
     
     myPrintersHashTable.Add(deviceID, deviceLoc)
Next

Me.ComboBox1.DataSource = myPrintersHashTable
Me.ComboBox1.DisplayMember = ?
Me.ComboBox1.ValueMember = ?

I had done something similar with a collection (Public myPrinterCollection as New Collection), but opted for an array because if I have to add the third property, a collection wouldn't work. Hashtable will not work either if I have to add additional printer properties, I fear. This is why I originally used an array. If I need to add additional printer properties, I need multiple dimensions.
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18041528
Hey - sorry, I meant for you to populate the hashtable exactly as you have done, but leave your array setup the same - if your combobox was working with the array, you could then use the hashtable as a quick lookup of the location for display in your text box.

You could also use multiple array dimensions, but this may cause problems for your combobox setup.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 200 total points
ID: 18041808
If you make an array of the OBJECTS concerned, you can then bind the combo to the properties of those objects.  You can only use two of the properties - one for DisplayMember and one for ValueMember.  But if you need to locate a third property you should (I assume there will not be masses of entries in the array) be able just to iterate through the array to find the Object that you want with reference to one of the values you have.

Something like

Public myArray(whatever) As ManagementObject
...
Dim i AS Integer = 0
For Each queryObj As ManagementObject In searcher.Get()
     myArray(i) = queryObj
     i += 1
     myPrintersHashTable.Add(deviceID, deviceLoc)
Next

Me.ComboBox1.DataSource = myArray
Me.ComboBox1.DisplayMember = "DeviceID"
Me.ComboBox1.ValueMember = "Location"

That would assume that your ManagementObject is a class, with "DeviceID", "Location", etc. exposed as properties.

But if that is not the case (and the references make me suspect that it may not be) I wonder what is wrong with a datatable?  And bog-standard binding to that?  Then, if you need to make reference to a field that you cannot fit in the two slots - DisplayMember and ValueMember - that are available in the combo, you have a direct reference to the row concerned via the binding manager's .Current property.

Roger
0
 
LVL 34

Expert Comment

by:Sancler
ID: 18041815
Oops, I see I forgot to delete this line

     myPrintersHashTable.Add(deviceID, deviceLoc)

from my example code.

Roger
0
 

Author Comment

by:jaw0807
ID: 18056144
Thank you guys. Both your ideas worked well. I'll split the points and accept both. I ended up opting for Hillwaaa's approach.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question