Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

displaying array values on combobox selectedindexchanged event?

Posted on 2006-11-29
6
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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