locked
ListBox Double click on selected item C# RRS feed

  • Question

  • Hi there everyone!

    Quick (and not so quick i think) question.

    I have a listbox, that displays items retrieved by a SQL query on an Access Database.

    What i want to do is control the way double-click behaves inside the listBox. 

    So, if i double click an item, only THEN something will happen. But for example, if i have 5 items, and space for another 5 items, if i double click the empty spaces nothing will.

    How do i do that?


    I hope i have explained myself right....if not, ill be more than glad to give it another try.


    Thx in advance!
    Tuesday, January 29, 2008 4:07 PM

Answers

  •  

    Hi Darito,

    Based on my understanding, you want to enable the double click on item only, right? If so, you can try to handle the MouseDoubleClick event instead, and check that the current location of your cursor is on an item. You need to use the IndexFromPoint method of the ListBox. Here is a sample for your information.

    Code Snippet

        public partial class Form5 : Form

        {

            public Form5()

            {

                InitializeComponent();

            }

     

            private void Form5_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("aa");

                dt.Columns.Add("bb");

                for (int i = 0; i < 10; i++)

                {

                    dt.Rows.Add("aa" + i, "bb" + i);

                }

                dt.AcceptChanges();

                this.listBox1.DisplayMember = "bb";

                this.listBox1.DataSource = dt;

                this.listBox1.Height = 200;

                this.listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick);

            }

     

            void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)

            {

                int index = this.listBox1.IndexFromPoint(e.Location);

                if (index != System.Windows.Forms.ListBox.NoMatches)

                {

                    MessageBox.Show(index.ToString());

                    //do your stuff here

                }

            }

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Friday, February 1, 2008 8:58 AM
  • You can do somthing like the following.

     

    private void listBox_DoubleClick(object sender, EventArgs e)

    {

      if (listBox.SelectedItem != null)

        if (listBox.SelectedItem.ToString().Length != 0)

          MessageBox.Show("Do work here");

    }

     

    There is an inherent problem with this though. This assumes thats you will have entries in your list box that are blank. If thats whats coming from your database then this will work fine, except for when a use clicks outside the bounds of the data in your list box. What I mean by that is that suppose your list box is large enough to accomodate 15 records, but your database only contains 10 record, 5 blanks and 5 with real data. If a user double clicks in the area where there are no records then the selected item wont have changed, and the code for what ever record currently is selected in the list box will be excuted.

    Tuesday, January 29, 2008 4:28 PM

All replies

  • You can do somthing like the following.

     

    private void listBox_DoubleClick(object sender, EventArgs e)

    {

      if (listBox.SelectedItem != null)

        if (listBox.SelectedItem.ToString().Length != 0)

          MessageBox.Show("Do work here");

    }

     

    There is an inherent problem with this though. This assumes thats you will have entries in your list box that are blank. If thats whats coming from your database then this will work fine, except for when a use clicks outside the bounds of the data in your list box. What I mean by that is that suppose your list box is large enough to accomodate 15 records, but your database only contains 10 record, 5 blanks and 5 with real data. If a user double clicks in the area where there are no records then the selected item wont have changed, and the code for what ever record currently is selected in the list box will be excuted.

    Tuesday, January 29, 2008 4:28 PM
  • Hi Darito,

    Simply check if an item has been selected within your Double Click event.
    Tuesday, January 29, 2008 4:29 PM
  • The problem is that when you fill a listBox with the results from a query it automatically selects one item, so when clicking in the empty space it handles the event anyway.

    But i want the event handled ONLY when double clicking an item or maybe, have the posibility to have non-selected items when retrieving items.
    Tuesday, January 29, 2008 4:40 PM
  • Dunno why moderators moved my post.....anyways, i still need this and in C# (that's why i posted there...instead of here -.-)
    Tuesday, January 29, 2008 6:33 PM
  •  

    Hi Darito,

    Based on my understanding, you want to enable the double click on item only, right? If so, you can try to handle the MouseDoubleClick event instead, and check that the current location of your cursor is on an item. You need to use the IndexFromPoint method of the ListBox. Here is a sample for your information.

    Code Snippet

        public partial class Form5 : Form

        {

            public Form5()

            {

                InitializeComponent();

            }

     

            private void Form5_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("aa");

                dt.Columns.Add("bb");

                for (int i = 0; i < 10; i++)

                {

                    dt.Rows.Add("aa" + i, "bb" + i);

                }

                dt.AcceptChanges();

                this.listBox1.DisplayMember = "bb";

                this.listBox1.DataSource = dt;

                this.listBox1.Height = 200;

                this.listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick);

            }

     

            void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)

            {

                int index = this.listBox1.IndexFromPoint(e.Location);

                if (index != System.Windows.Forms.ListBox.NoMatches)

                {

                    MessageBox.Show(index.ToString());

                    //do your stuff here

                }

            }

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Friday, February 1, 2008 8:58 AM
  •  

    this code doesnt help in Listbox with ownerdraw property set.

    in the owner draw list box, it just give the index of the item which is selected even if we click on the listbox other than item. 

    Friday, July 25, 2008 11:59 AM
  • Hello Darito,

    I Had The Same Problem And Found The Solution is to Check if The Mouse is Inside The Item when The Double Click Event Fires

    Try To Add This Code To The MouseDoubleClick Event:

     

                if (myListBox.SelectedIndex != -1)
                {
                    Rectangle selectedItemRectangle = myListBox.GetItemRectangle(myListBox.SelectedIndex);
                    if (selectedItemRectangle.Contains(e.Location))
                    {
                        /*
                         * do your work here
                         */
                    }
                }
    


    • Proposed as answer by Hefff Tuesday, December 11, 2012 9:29 AM
    Thursday, October 13, 2011 12:06 PM
  • Thank you .

    but i want to know what is the "Location"?

    • Proposed as answer by mick1313 Thursday, June 19, 2014 7:10 PM
    Tuesday, April 2, 2013 7:26 PM
  • I got another solution... try this:

    if (myList.SelectedIndex != -1)
    {
         Rectangle screenRectangle = RectangleToScreen(this.ClientRectangle);
        int titleHeight = screenRectangle.Top - this.Top;

         int yMax = this.Location.Y + titleHeight + myList.Location.Y + 1 + (myList.Items.Count * 13); if (MousePosition.Y <= yMax) { //your code } }

    When you use the MouseDoubleClick-Event, the X-Position and the yMin Value doesn't matter. I have to add 1px for the border of the Listbox. Then I multiplied the amount of items with 13 because that's the normal height of a ListBox-Item...

    Good Luck!



    • Edited by P35c1 Friday, June 7, 2013 7:37 AM
    Thursday, June 6, 2013 12:44 PM