Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto select displayed listbox item?

Status
Not open for further replies.

Ovatvvon

Programmer
Feb 1, 2001
1,514
US
Hello all,

Normally when grabbing a value from a listbox, the item in the listbox would need to be selected first. If my listbox displays only one item at a time however, I would like to grab the displayed value without the user having to click on it.

Is there a way to know which item in the listbox collection is displayed without the user having to select it?


-Ovatvvon :-Q
 
If ListBox1.Items.Count = 1 Then
ListBox1.SelectedItem = ListBox1.Items.Item(0)
End If
 
Place it in:
_BindingContextChanged or
_DataSourceChanged
if you populate it by {binding|datasources} or
After completing the listbox1.items.add method


Hope i'm clear enough...
 

I will try both of these methods later this evening. Thank you for responding, both of you. :)



-Ovatvvon :-Q
 

hehe, sorry, just saw the posts, and didn't look at who was giving them yet. :) Well, if I try it and it works, then I will definately give you a star...but, will attempt to give you two - for both of you. :)



-Ovatvvon :-Q
 
Alas, it didn't work. :-(

I have the form_load adding all items to the listbox (just 0 through 100), then put it directly after that as you said. Didn't work, so I also inserted a msgbox to verify its count would be 1, but it grabbed all 101 items. Here is what it looks like as of now:
Code:
    Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim intIndexCounter, intValueCounter
        intIndexCounter = 0

        For intValueCounter = 100 To 0 Step -1
            Me.lstBox.Items.Insert(intIndexCounter, intValueCounter)
            intIndexCounter += 1
        Next

        MsgBox(lstBox.Items.Count, MsgBoxStyle.OKOnly, "")

        If lstBox.Items.Count = 1 Then
            lstBox.SelectedItem = lstBox.Items.Item(0)
        End If
    End Sub

So, it isn't detecting how many are displayed in the list box, but how many items the listbox contains. Need it to auto-select the one that is being displayed. Thoughts?


-Ovatvvon :-Q
 
I don't think you can set the selected items from within the load event. Put it in a different event such as frmTest_Activated.

Also, this code only runs if there is only 1 item in the list

Code:
        If lstBox.Items.Count = 1 Then
            lstBox.SelectedItem = lstBox.Items.Item(0)
        End If

I suspect you want something more like this:

Code:
        If lstBox.Items.Count > 0 Then
            lstBox.SelectedItem = lstBox.Items.Item(0)
        End If
 
Actually, I don't want either of those if I am reading it correctly. Yes, there are 101 items in the list, but I don't want item(0) to always be selected...I want the displayed item to be selected. (Basically, I want them to be able to select it by scrolling to the number, rather than having to actually click on it as well. This list box displays only one column at a time.)

How can I make it so when they scroll to the item they want, it will automatically be selected for the processing, without them having to click on the listbox item after scrolling to it?


-Ovatvvon :-Q
 
I'm not sure whether or not this will be of any help to you but:

Code:
 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    For a As Integer = 0 To 100
      ListBox1.Items.Add(a)
    Next
    ListBox1.Focus()
    'if there is only 1 item select it otherwise select the first item
    ListBox1.SelectedIndex = 0

  End Sub

  Private Sub ListBox1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseWheel

    ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.X, e.Y)

  End Sub

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Label1.Text = ListBox1.SelectedItem.ToString

  End Sub

Unfortunately, I can't see a way to respond to the scroll bar, just the wheel mouse.

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top