Question Having ListBox show Predetermined Value

ent_ttu

Member
Joined
Jun 17, 2008
Messages
14
Programming Experience
1-3
I've created a custom class that inherits ListBox. I'm successfully able to create the box and have it appear on the page I need (thanks to help from this forum), but now I can't get it to select a specific item as the default.

I've created an array that stores the values, so I can keep both the primary key and the information I want the user to see. I konw the data is being passed, because the box appears with the names in it, and I can get a pop-up window with the primary key.

I've tried everything I can find online, but none of it's working. Here's the code snippets:
VB.NET:
Expand Collapse Copy
Dim lstRespParty As New ListBox

Public Sub New(ByVal responsibleID As String, ByVal ParentForm As Form)
       Dim people As New ArrayList()
       'There's a sql query here to get people from table, but I didn't post it

        Do While inc < MaxRows
            resp = New responsibleParty(ds.Tables("ppl").Rows(inc).Item("person_id"))
            people.Add(resp)
            inc = inc + 1
        Loop

        lstRespParty.DataSource = people

        'when I run the code, the integer appears in this msgbox, so I know it's being passed
        MsgBox("responsible id: " & responsibleID)

        lstRespParty.DisplayMember = "rpLastNameFirst"
        lstRespParty.ValueMember = "personNo"

        'this is where I'm trying to tell it to choose the responsibleID when it displays the list box
        lstRespParty.SelectedValue = responsibleID
        'tried selectedvalue = responsibleID
        'tried selectedvalue = "responsibleID"
        'tried selecteditem = responsibleID
        'tried selecteditem = "responsibleID"
        'tried selectedindex = responsibleID; at least got an error message
        'lstRespParty.SelectedIndex = "responsibleID"; at least got an error message

I'm using VB.NET 2005. Any suggestions would be much appreciated. Thanks in advance!
 
Thanks for the response. At least now I'm headed in the right direction...

The error message reads "Invalid Argument=value of '7' is not valid for selectedIndex", but I'm having trouble finding how to solve it. I'm still weeding thru google results, but not having much luck.

I have confirmed that both responsibleID and personNo are strings, not integers. Is that the problem? Is there a way to view the ValueMember, and make sure it's being assigned correctly?

Thanks again.
 
selected index is an integer in the range 0 to (listbox item count - 1)

If you have 10 items int he list and want to select the SEVENTH you set Selected Index to 6 ( SIX )
 
Thanks again!

Unfortunately, that's the root of my problem; I don't know what position the item I want will be in. That's why I wanted to assign the index as the personID from the table.

If I understand this correctly, I should have a dataset something like this:
ValueMember = 1; DisplayMember = "John Smith"
ValueMember = 3; DisplayMember = "Jane Smith"
ValueMember = 4; DisplayMember = "John Doe"
etc...

So I pass it a '4' and want to tell it to show "John Doe" as selected.

Sorry for the ignorance; I'm having a hard time with VB.NET (this is my first project). There's a chance I'm going about this all wrong, but this is the only way I've found that looks like it might do what I want.

Thanks again for the responses and the patience.
 
And is your ValueMember a STRING or an INTEGER?

If it's a string, you want:

listBox.SelectedValue = responsibleID.ToString()


otherwise:

listBox.SelectedValue = responsibleID
 
They're both strings. Since I won't be using them for numeric calculations, I stored them as strings, not integers.

I changed:
VB.NET:
Expand Collapse Copy
lstRespParty.selectedIndex = responsibleID

to:
VB.NET:
Expand Collapse Copy
lstRespParty.SelectedValue = responsibleID.ToString()

I no longer get the error message, but it still doesn't show the item as selected; it just selects the first item in the listbox, which isn't what I need.

Again, thanks for the help and the patience.
 
I went back and changed both variables to Integer, and then changed the code back to:

lstRespParty.selectedIndex = responsibleID

That brought back the error message of "Invalid Argument=value of '7' is not valid for selectedIndex"
 
Wait a sec.. stop coding blindly and think about what is going on. I mention that .SelectedIndex is for selecting an item based on its position in the list. You said that isnt what you want and now youre trying to use it again.. Dont do it, because it's not what you want.. Coding is not about trying every property at random until error messages go away.

ValueMember is bound to a column containing integers stored as strings. When you alter listbox.SelectedValue, that column will be hunted for the mathcing value and the control repositions to select the chosen item. I promise you this is how it works, see the attached demo project that does exactly and only that.

NOTE that setting .SelectedValue causes ListBox to navigate the list it sits on. If you have anything else that also navigates the list after, such as BindingSource.MoveFirst then the first item in the listbox will become highlighted.
 

Attachments

I do understand that is how it works, which is why it was so unbelievably frustrating that it wasn't working.

At any rate, I've gotten the problem solved. Knowing that line of code was what I was supposed to be using, I began trying the line on the calling form, rather than in the class file that I used to create the list box. I added this to my class code:
VB.NET:
Expand Collapse Copy
    Public Property selectedPerson() As String
        Get
            Return lstRespParty.SelectedValue
        End Get
        Set(ByVal value As String)
            lstRespParty.SelectedValue = value
        End Set
    End Property

Then, from the calling form, I added:
VB.NET:
Expand Collapse Copy
 lstRespParty.selectedPerson = invNo.responsibleID

That is working fine. It must have had something to do with it being a custom class that I was trying to work with.

I appreciate your patience and assistance; I think it's great that you're willing to help beginners like me. Thanks again!
 
Back
Top