Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Simple Problem With ListBoxes

Simple Problem With ListBoxes

Scheduled Pinned Locked Moved Visual Basic
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pugman812
    wrote on last edited by
    #1

    i was wondering what the code was to invoke a click event to highlight the item in the listbox when you right click. Im just using this to pull up a context menu but i want the item to be selected. Simple solution i hope.

    J 1 Reply Last reply
    0
    • P Pugman812

      i was wondering what the code was to invoke a click event to highlight the item in the listbox when you right click. Im just using this to pull up a context menu but i want the item to be selected. Simple solution i hope.

      J Offline
      J Offline
      John Kuhn
      wrote on last edited by
      #2

      In VB.NET, something like this... Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown Dim item As Integer For j As Integer = 1 To ListBox1.Items.Count Dim rect As System.Drawing.Rectangle = ListBox1.GetItemRectangle(j - 1) If rect.Contains(e.X, e.Y) Then item = j - 1 Exit For End If Next ListBox1.SelectedIndex = item End Sub At least, it worked for me. And, you could test to see if it was the right-mouse button first, too. I had this sample working with a context menu assigned to the ListBox, as well.

      P 1 Reply Last reply
      0
      • J John Kuhn

        In VB.NET, something like this... Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown Dim item As Integer For j As Integer = 1 To ListBox1.Items.Count Dim rect As System.Drawing.Rectangle = ListBox1.GetItemRectangle(j - 1) If rect.Contains(e.X, e.Y) Then item = j - 1 Exit For End If Next ListBox1.SelectedIndex = item End Sub At least, it worked for me. And, you could test to see if it was the right-mouse button first, too. I had this sample working with a context menu assigned to the ListBox, as well.

        P Offline
        P Offline
        Pugman812
        wrote on last edited by
        #3

        Thank you that worked great. Do you know what the code would be for a 6.0 App. I want to use this in some of my 6.0 applications.

        J 1 Reply Last reply
        0
        • P Pugman812

          Thank you that worked great. Do you know what the code would be for a 6.0 App. I want to use this in some of my 6.0 applications.

          J Offline
          J Offline
          John Kuhn
          wrote on last edited by
          #4

          Something like this -- though bear in mind this needs to be refined, as it will error out if there are less than items currently populated in the list, and items should be defined as the total number of list items the list is capable of displaying.

          Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
          Dim items As Integer
          Dim averageHeight As Integer
          items = 14
          averageHeight = List1.Height / items
          For i = 1 To items
          If Y > (((i - 1) * averageHeight) + 1) And Y < (i * averageHeight) Then
          List1.ListIndex = i - 1
          End If
          Next
          If Button = 2 Then
          Form1.PopupMenu rclOne
          End If
          End Sub

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups