Solved

displaying array values on combobox selectedindexchanged event?

Posted on 2006-11-29
6
269 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 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
Independent Software Vendors: 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net and sql server 4 54
VB.NET 2008 - SQL Timeout 9 51
Access/Visual Basic Question 3 43
Broken .resx file generating errors 18 26
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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