Searching control of objects

  • Thread starter Thread starter Randy
  • Start date Start date
R

Randy

I'm looking for an easy way to find the value member of an object in a
control. I have an ArrayList of a simple class with two properties. One
property is "Id" and the other is "CompName" (both stings). On my form I
have a CheckListBox and I bind the ArrayList of Company's like so.



Clb.DisplayMember = "CompName"

Clb.ValueMember = "Id"

Clb.DataSource = alCompany



I have a second ArrayList with stings that match the Id of some of the
companies that I want to set the check state. But with the folling code I
always get a -1 index. Is there another way except looping through the
entire CheckListBox for every Id in the Second ArrayList? The Obj.ToString
is holding a valid Id, but its not finding it.



Dim obj as Object

Dim intLoop as Integer



For each obj in alCheckTheseId

intLoop = clb.ValueMemeber.IndexOf(Obj.ToString)



if intLoop > -1 Then

clb.SetItemChecked(intLoop, True)

end if

Next





MANY THX,

-Randy
 
When you step through the code, what value is obj.ToString giving you? It's
probably going to be something like this:
System.Windows.Forms.Button, Text: Button1
You may want to cast the object to a specific type and interrogate a
specific property
Also, it looks like you are trying to get the ValueMember which if I'm
reading this correctly is always going to be ID for that specific listbox.
So that code is going to find the Index of the Literal "ID" which will be
the same for each item in the listbox...ValueMember doesn't change for each
itme.

What ultimately are you trying to do with the two arraylists? If you are
trying to match items and values, there's definitely more straightforward
approaches. Moreoever, you may want to make a class and use reflection to
grab the properties depending on what the ultimate goal is.

HTH,

Bill

intLoop = clb.ValueMemeber.IndexOf(Obj.ToString)
 
obj.ToString value is something like "1085" {string}. The ultimate goal is
to fill the CheckListBox with all possible companies and allow the user to
save any checked companies to later load. The first ArrayList holds all the
Company objects and the second comes from the database and holds just the
Id's of the companies that were checked. I thought ValueMember was a
companion to DisplayMember when objects are added to a control. The same as
VB6 when you have...

' User sees Comany A in combobox, but has a value of 23
Combo1.AddItem "Company A"
Combo1.ItemData(combo1.NewIndex) = 23

In my first example, alCheckTheseId is the second ArrayList of strings. It
holds the Id if the checked companies. Simply I need to match the value of
a property in a list of objects.

-Randy
 
Back
Top