如何通过单击从列表框中选择多个选项 [英] how to choose multiply choices from list box by click

查看:122
本文介绍了如何通过单击从列表框中选择多个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个列表框,我可以通过点击Ctrl +鼠标左键从中选择多个选项。



当我点击鼠标时......这将是一个选择。



我如何选择所有选择鼠标只是????

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 ????

推荐答案

我以前用过这个,修改它来处理左键单击Alt:它让你选择/ de - 使用alt-down和left click选择all,或者选择control-A / a:如果全部被选中则取消全部。



构建此:添加一个名为ListBoxWithSelectAll的类型组件的WinForm项目的新项目项,并粘贴以下代码:
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:
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;
            }
        }
    }
}

我改变了什么:这是从一些代码中改编而来的,这些代码也实现了一个'撤销机制来进行选择,但我可以不要分享,因为它属于客户。



此代码已在FrameWork 4.5和3.0中测试过。它适用于自定义子类ListBox的SelectionMode设置为'MultiSimple或'MultiExtended modes。

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.


尝试SelectionMode.MultiSimple属性。它应该能得到你的结果。
Try SelectionMode.MultiSimple property. It should get you the results.


这篇关于如何通过单击从列表框中选择多个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆