Results 1 to 3 of 3

Thread: Programmatically Select item in ListBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    13

    Cool Programmatically Select item in ListBox

    I'm Posting this here as I had a lot of trouble finding a solution that fit what I needed.
    Basically I have a ListBox that drops down from a text field and populates with items, however hovering the mouse over an item had zero effect on the display of the items, selecting an option closes the listbox so using listbox.selecteditem = x wasn't working for me. Neither was any draw method mentioned while searching. This is what I came up with to Highlight an Item in the listbox without triggering the listbox's SelectedIndexChanged property until I needed it.

    Code:
        Dim trigger As Boolean
        Private Sub listtake_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listtake.SelectedIndexChanged
            If trigger = True Then
                Exit Sub
            End If
            takenametxt.Text = listtake.SelectedItem.ToString
            listtake.Items.Clear()
            listtake.Visible = False
        End Sub
    
        Dim item As Decimal
    
        Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listtake.MouseMove
            trigger = True
            item = listtake.IndexFromPoint(New Point(e.X, e.Y))
            listtake.SelectedIndex = item
        End Sub
    
        Private Sub listtake_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listtake.MouseUp
            listtake.SelectedIndex = -1
            trigger = False
            listtake.SelectedIndex = item
        End Sub
    Last edited by drpepper1324; Nov 18th, 2016 at 03:18 PM. Reason: varible wrong

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Programmatically Select item in ListBox

    This seems like a job for a ComboBox, what's the reason for this approach?

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    13

    Re: Programmatically Select item in ListBox

    It was just the natural flow of things, I needed a List... one that can hide and can select in. There was just an extra event firing that caused problems. posted the work around for others. This example is tons easier than the other work arounds posted.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width