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. C#
  4. Concerning ListBoxes

Concerning ListBoxes

Scheduled Pinned Locked Moved C#
8 Posts 4 Posters 2 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.
  • Q Offline
    Q Offline
    quiteSmart
    wrote on last edited by
    #1

    Hi, i am using a listBox that contains some items. I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired. I wish to know if there is a way to make sure that the event is only raised when only an item is double clicked. thanks in advance

    CPalliniC L 2 Replies Last reply
    0
    • Q quiteSmart

      Hi, i am using a listBox that contains some items. I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired. I wish to know if there is a way to make sure that the event is only raised when only an item is double clicked. thanks in advance

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      quiteSmart wrote:

      I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired.

      You can programmatically distinguish between the two cases. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      In testa che avete, signor di Ceprano?

      Q 1 Reply Last reply
      0
      • CPalliniC CPallini

        quiteSmart wrote:

        I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired.

        You can programmatically distinguish between the two cases. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        Q Offline
        Q Offline
        quiteSmart
        wrote on last edited by
        #3

        CPallini wrote: You can programmatically distinguish between the two cases. How to do that?

        CPalliniC 1 Reply Last reply
        0
        • Q quiteSmart

          CPallini wrote: You can programmatically distinguish between the two cases. How to do that?

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          use the ListBox.SelectedIndex Property as stated in MSDN:

          Property Value
          A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

          hence, if you got a -1 then no item is selected. hope that helps. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          In testa che avete, signor di Ceprano?

          Q 1 Reply Last reply
          0
          • CPalliniC CPallini

            use the ListBox.SelectedIndex Property as stated in MSDN:

            Property Value
            A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

            hence, if you got a -1 then no item is selected. hope that helps. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            Q Offline
            Q Offline
            quiteSmart
            wrote on last edited by
            #5

            CPallini wrote:

            Property Value A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

            I have already tried this. It workd fine with one exception when u select an item and then double click in the empty space it gives u the returned index as the index of the selected item so in short this wont work perfectly. do u have another suggestion?

            B CPalliniC 2 Replies Last reply
            0
            • Q quiteSmart

              CPallini wrote:

              Property Value A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

              I have already tried this. It workd fine with one exception when u select an item and then double click in the empty space it gives u the returned index as the index of the selected item so in short this wont work perfectly. do u have another suggestion?

              B Offline
              B Offline
              Bhupi Bhai
              wrote on last edited by
              #6

              DoubleClick event occurs when the control is double-clicked. This is a normal behaviour. Regards, Bhupi Bhai.

              1 Reply Last reply
              0
              • Q quiteSmart

                CPallini wrote:

                Property Value A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

                I have already tried this. It workd fine with one exception when u select an item and then double click in the empty space it gives u the returned index as the index of the selected item so in short this wont work perfectly. do u have another suggestion?

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                quiteSmart wrote:

                do u have another suggestion?

                Of course... You can retrieve the Rectangle of the selected item (if there is one) and then check if it contains the point where DoubleClick occurred. It follows an ugly example, using VB.NET (was handy, 'cause I had a VB project easyly available... Conversion to C# is straightforward):

                Sub ListBox1DoubleClick(sender As Object, e As System.EventArgs)
                Dim index As Integer
                index = listBox1.SelectedIndex
                Dim ev As System.Windows.Forms.MouseEventArgs
                ev = e
                If index <> -1 Then
                Dim rc As Rectangle
                rc =listBox1.GetItemRectangle(index)
                If rc.Contains(ev.X, ev.Y) Then
                MsgBox("Hello")
                End If
                End If
                End Sub

                Hope that helps. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • Q quiteSmart

                  Hi, i am using a listBox that contains some items. I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired. I wish to know if there is a way to make sure that the event is only raised when only an item is double clicked. thanks in advance

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  in your click handler, try using ListBox.IndexFromPoint() and test for ==ListBox.NoMatches (which equals -1 I guess) :)

                  Luc Pattyn

                  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