如何通过鼠标拖动他们,要选择多个控件(How to select multiple controls

2019-06-23 22:45发布

我希望能够拖过一堆的控制和选择那些某一类型(文本框)的。

一旦拖动动作已经完成,我要显示一个输入框(是的,我将不得不参考/使用VB .DLL)提示将在每个选定的文本框输入的值的用户。

可以这样做? (当然,但是怎么样?)

还是有别的办法来完成同样的事情(允许用户快速选择多个控件,然后同时执行所有他们的动作)?

更新:

我有这种工作的 - 在“警告”或“疑难杂症”是,我要弹出一个MessageBox.Show()的用户为它工作。 从本质上讲,我:

设置一个布尔值,真正在容器的(FlowLayoutPanel的,在我的情况)MouseDown事件,如果选择了鼠标右键。

设置在容器的MouseUp事件相同的布尔为假,如果选择了鼠标右键。

然后,我有所有的形式,如果布尔值为true,改变背景色上的文本框的共享MouseHover事件处理程序(以亮灰,在我的情况下,从窗口)。

在容器的MouseUp事件,我也使用的InputBox(引用/输入/使用VB .dll文件),要求用户输入将共同为“突出了”文本框的值。 然后,我通过他们循环,寻找那些具有背景色,并assing用户提供的价值,他们Text属性。

瞧!

不幸的是,文本框修改后的财产似乎并没有当你将它值这个方式被改变,所以我不得不解决的是(明确设置的‘保存’按钮即可启用),我不得不添加更多的代码复制我的则KeyPressed代码这限制由用户输入的值。

所以,这是当然的,可能的,虽然有点缺憾。 我还没有决定,如果MessageBox.Show()是一个“错误”或功能,但...

相关的职位是: 为什么MouseHover事件只得到如果messagebox.show()或断点之前发生叫什么名字?

Answer 1:

这其实是很简单的。 我受拖累假设你是说你要“选择”控制,就像你会在例如画图程序“选择”一些像素。 这是我写给你,不只是这一点,只选择TextBox控件的例子。 它忽略其他控件。

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表单上的面板)。 如果是这样的情况下,选择将在面板下方绘制,然后将文本框将不被选中,因为我写的代码不检查嵌套的容器。

您可以轻松地更新代码来完成所有的然而这,但是这应该给你一个良好的开端。



文章来源: How to select multiple controls by mouse-dragging over them