如何通过鼠标拖动选择多个控件

4
我希望能够拖动一组控件并选择其中某种类型的控件(TextBoxes)。
完成拖动操作后,我想显示一个输入框(是的,我将不得不引用/使用VB .dll),提示用户输入每个选定的TextBox中将要输入的值。
这可以做到吗?(当然可以,但如何实现?)
或者还有其他方法可以实现相同的功能(允许用户快速选择多个控件,然后对它们同时执行操作)?
更新:
我已经做到了这样的工作-“警告”或“陷阱”是我必须向用户弹出MessageBox.Show()才能使其工作。基本上,我:
在容器的MouseDown事件上设置一个布尔值为true,如果选择了右鼠标按钮。
在容器的MouseUp事件上,如果选择了右鼠标按钮,则将该布尔值设置为false。
然后,我在该表单上所有TextBox的共享MouseHover事件处理程序中,如果布尔值为true,则更改BackColor(从Window更改为Gainsboro)。
在容器的 MouseUp 事件中,我还使用了一个 InputBox(引用/导入/使用 VB .dll),要求用户输入将成为“突出显示”文本框通用的值。然后,我循环遍历它们,查找具有该 BackColor 的文本框,并将用户提供的值分配给它们的 Text 属性。
大功告成!
不幸的是,当您以这种方式分配值时,文本框的 Modified 属性似乎没有被改变,所以我不得不解决这个问题(显式地将“保存”按钮设置为启用状态),并且我不得不添加更多代码来复制我的 KeyPressed 代码,以限制用户输入的值。
因此,当然可以实现,尽管有点笨拙。我还没有决定 MessageBox.Show() 是一个“错误”还是一个特性...
相关帖子是: 为什么只有在消息框显示(或断点发生)之前才会调用 MouseHover 事件?

你能否在包含TextBox控件的控件上加入任何限制吗? - Mike Guthrie
这是一个 FlowLayoutPanel,在这种情况下。 - B. Clay Shannon-B. Crow Raven
你已经修改了原始问题。你应该关闭这个问题并创建一个新的问题。现在不太清楚你的问题是什么,而且你似乎在这个阶段有很多未知因素,现在看起来就像是你的更新设计不好。 - David Anderson
1个回答

11
这其实非常简单。我假设你所说的“拖动”意味着你想要“选择”控件,就像在绘画程序中选择一些像素一样。下面是我为你编写的一个示例,它只选择文本框控件,并忽略其他控件。
namespace WindowsFormsApplication5
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;

    /// <summary>
    /// Main application form
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// Initializes a new instance of the WindowsFormsApplication5.Form1 class
        /// </summary>
        public Form1() {
            InitializeComponent();
            DoubleBuffered = true;
        }

        private Point selectionStart;
        private Point selectionEnd;
        private Rectangle selection;
        private bool mouseDown;

        private void GetSelectedTextBoxes() {
            List<TextBox> selected = new List<TextBox>();

            foreach (Control c in Controls) {
                if (c is TextBox) {
                    if (selection.IntersectsWith(c.Bounds)) {
                        selected.Add((TextBox)c);
                    }
                }
            }

            // Replace with your input box
            MessageBox.Show("You selected " + selected.Count + " textbox controls.");
        }

        protected override void OnMouseDown(MouseEventArgs e) {
            selectionStart = PointToClient(MousePosition);
            mouseDown = true;
        }

        protected override void OnMouseUp(MouseEventArgs e) {
            mouseDown = false;

            SetSelectionRect();
            Invalidate();

            GetSelectedTextBoxes();
        }

        protected override void OnMouseMove(MouseEventArgs e) {
            if (!mouseDown) {
                return;
            }

            selectionEnd = PointToClient(MousePosition);
            SetSelectionRect();

            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);

            if (mouseDown) {
                using (Pen pen = new Pen(Color.Black, 1F)) {
                    pen.DashStyle = DashStyle.Dash;
                    e.Graphics.DrawRectangle(pen, selection);
                }
            }
        }

        private void SetSelectionRect() {
            int x, y;
            int width, height;

            x = selectionStart.X > selectionEnd.X ? selectionEnd.X : selectionStart.X;
            y = selectionStart.Y > selectionEnd.Y ? selectionEnd.Y : selectionStart.Y;

            width = selectionStart.X > selectionEnd.X ? selectionStart.X - selectionEnd.X : selectionEnd.X - selectionStart.X;
            height = selectionStart.Y > selectionEnd.Y ? selectionStart.Y - selectionEnd.Y : selectionEnd.Y - selectionStart.Y;

            selection = new Rectangle(x, y, width, height);
        }
    }
}

目前存在一些限制。 其中一个明显的限制是,它将无法选择嵌套容器控件中的TextBox(例如,表单上包含TextBox的面板)。 如果是这种情况,选择将在面板下方绘制,因为我编写的代码不检查嵌套容器,因此无法选择TextBox。

不过,您可以轻松地更新代码来解决所有问题,这应该会给您一个扎实的起点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接