Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all,

I have a list box and i can choose multiply choices from it by clicking on "Ctrl + left mouse".

When I click on just the mouse ... It will be one choice.

How can I choose all choices by the mouse just ????
Posted
Comments
DamithSL 7-Jan-15 4:19am    
just what?
Praveen Kumar Upadhyay 7-Jan-15 4:20am    
Question is not clear. Do you want to select all items of list box?
Jochen Arndt 7-Jan-15 4:24am    
You can create a context menu that opens upon right click and provides a 'Select all' item.
Generally you can use the keyboard to select all items (Ctrl+A).
BillWoodruff 7-Jan-15 11:11am    
In a WinForms ListBox Ctrl-A will not select all items.

I've used this in the past, and modified it to handle left-click-Alt: it let's you select/de-select all with either alt-down and left click, or control-A/a: if all are selected it de-selects all.

To build this: Add a new Project Item to your WinForm Project of Type Component named : ListBoxWithSelectAll, and paste in the following code:
C#
using System;
using System.Windows.Forms;

namespace CustomControls
{
    public partial class ListBoxWithSelectAll : ListBox
    {
        public ListBoxWithSelectAll()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private bool MouseIsDown = false;
        
        private bool AllLBItemsSelected = false;

        private void ListBoxWithSelectAll_KeyDown(object sender, KeyEventArgs e)
        {
            if ((MouseIsDown && e.Alt && MouseButtons == MouseButtons.Left) || (e.Control && e.KeyCode == Keys.A))
            {
                AllLBItemsSelected = !AllLBItemsSelected;

                for (int i = 0; i < this.Items.Count; i++)
                {
                    this.SetSelected(i, AllLBItemsSelected);
                }

                // use this to prevent flicker from multiple whatever
                MouseIsDown = false;
            }
        }

        private void ListBoxWithSelectAll_MouseDown(object sender, MouseEventArgs e)
        {
            MouseIsDown = true;
        }

        private void ListBoxWithSelectAll_MouseUp(object sender, MouseEventArgs e)
        {
            MouseIsDown = false;
        }

        // why we're doing this ... like this ... is ... too long a story
        private void ListBoxWithSelectAll_Enter(object sender, EventArgs e)
        {
            // guard against incorrect SelectionMode setting
            if (this.SelectionMode == SelectionMode.One || this.SelectionMode == SelectionMode.None)
            {
                this.SelectionMode = SelectionMode.MultiSimple;
            }
        }
    }
}
What I changed: this was adapted from some code that also implemented an 'Undo mechanism for selections, but I can't share that since it "belongs" to a client.

This code has been tested in FrameWork 4.5 and 3.0. It works with the custom sub-classed ListBox 'SelectionMode set to either 'MultiSimple or 'MultiExtended modes.
 
Share this answer
 
Try SelectionMode.MultiSimple property. It should get you the results.
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 7-Jan-15 4:32am    
This is not an answer. OP has already mentioned that he is selecting multiple items with Ctrl+Mouse click, It means this property is already been set.
BillWoodruff 7-Jan-15 11:40am    
In a WinForms ListBox the SelectionMode could have been set to either 'MultiSimple or 'MultiExtended to produce effects that the OP reports.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900