如何通过鼠标拖动他们,要选择多个控件 [英] How to select multiple controls by mouse-dragging over them

查看:117
本文介绍了如何通过鼠标拖动他们,要选择多个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to drag over a bunch of controls and select those of a certain type (TextBoxes).

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

Once the dragging action has been completed, I want to display an inputbox (yes, I'll have to reference/use the VB .dll) prompting the user for the value that will be entered in each selected TextBox.

可以这样做? (当然,但如何?)

Can this be done? (of course, but how?)

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

Or is there another way to accomplish the same thing (allow the user to quickly select multiple controls and then perform an action on all of them at once)?

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

I've got this sort of working - the "caveat" or "gotcha" being that I have to pop up a MessageBox.Show() to the user for it to work. Essentially, I:

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

Set a boolean to true on the container's (FlowLayoutPanel, in my case) MouseDown event, if the right mouse button was selected.

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

Set that same boolean to false on the container's MouseUp event, if the right mouse button was selected.

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

I then have a shared MouseHover event handler for all of the TextBoxes on that form that, if the boolean is true, changes the BackColor (to Gainsboro, in my case, from Window).

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

In the container's MouseUp event, I also use an InputBox (referencing/importing/using the VB .dll) requesting the user enter the value that will be common for the "highlighted" TextBoxes. I then loop through them, looking for those with that BackColor, and assing the user-supplied value to their Text properties.

瞧!

不幸的是,文本框修改财产似乎并没有被改变,当你为它分配值这样,所以我不得不解决的(明确设置的保存按钮即可启用),我有添加更多的代码重复我的keyPressed代码,限制由用户输入的值。

Unfortunately, the TextBoxes' Modified property does not seem to be altered when you assign it values this way, so I had to work around that (explicitly setting the "Save" button to enabled), and I had to add more code to duplicate my KeyPressed code which limits the values entered by the user.

因此,它是,当然,可能的,尽管有点缺憾。如果MessageBox.Show()是一个错误或要素,虽然我还没有决定...

So, it is, of course, possible, albeit a little kludgy. I haven't decided if the MessageBox.Show() is a "bug" or a feature, though...

一个相关的帖子是:的Why确实MouseHover事件只被调用,如果的MessageBox.show()或之前出现断点?

A related post is: Why does MouseHover event only get called if a messagebox.show() or breakpoint occurs before it?

推荐答案

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

This is actually very simple. I assume by drag you mean that you want to 'select' controls, like you would 'select' some pixels in a paint program for example. Here is an example I wrote you that does just this and only selects TextBox controls. It ignores other controls.

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窗体上的小组)。如果是这样的情况下,选择将在面板下方引出,文本框将不被选中,因为我写的代码不检查嵌套的容器。

Now there are limitations with this currently. The obvious is that this will not select a TextBox within a nested container control (eg. a panel on your form containing a TextBox). If that were the case the selection would be drawn underneath the panel, and the TextBox would not be selected because the code I wrote does not check nested containers.

您可以很容易地更新代码然而,做到这一切,但是这应该给你一个良好的开端。

You can easily update the code to do all of this however, but this should give you a solid start.

这篇关于如何通过鼠标拖动他们,要选择多个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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