Solved

displaying array values on combobox selectedindexchanged event?

Posted on 2006-11-29
6
266 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 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

15 Experts available now in Live!

Get 1:1 Help Now