问题描述
大家好,
我有一个列表框,我可以通过点击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 ????
推荐答案
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.
这篇关于如何通过单击从列表框中选择多个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!