Solved

displaying array values on combobox selectedindexchanged event?

Posted on 2006-11-29
6
263 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 50 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 50 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now