简体   繁体   English

如何使用Ctrl键+鼠标单击来选择多个控件?

[英]How to use ctrl key + mouse click to select multiple controls?

Probably this question has already an answer here but I was not able to find it.. I have a tabControl with a flowlayoutpanel in each tab page where I can add controls at run time. 可能这个问题已经在这里找到答案了,但是我找不到它。我在每个选项卡页面中都有一个带有flowlayoutpanel的tabControl,可以在运行时添加控件。 I can rearrange them, move them across tab pages.. How can I select multiple controls to be able to move them around using ctrl key + mouse click? 我可以重新排列它们,在选项卡页之间移动它们。我如何选择多个控件以便能够使用ctrl键+鼠标单击来移动它们?

This is my drag event so far: 到目前为止,这是我的拖动事件:

private void control_DragDrop(object sender, DragEventArgs e)
    {
        Control target = new Control();

        target.Parent = sender as Control;

        if (target != null)
        {
            int targetIndex = FindCSTIndex(target.Parent);
            if (targetIndex != -1)
            {
                string cst_ctrl = typeof(CustomControl).FullName;
                if (e.Data.GetDataPresent(cst_ctrl))
                {
                    Button source = new Button();
                    source.Parent = e.Data.GetData(cst_ctrl) as CustomControl;

                    if (targetIndex != -1)

                        fl_panel = (FlowLayoutPanel)tabControl1.SelectedTab.Controls[0];
                    if (source.Parent.Parent.Name == target.Parent.Parent.Parent.Name)
                    {
                        this.fl_panel.Controls.SetChildIndex(source.Parent, targetIndex);
                    }
                    else
                    {
                        target.Parent.Parent.Parent.Controls.Add(source.Parent);
                        this.fl_panel.Controls.SetChildIndex(source.Parent, targetIndex);
                    }
                }
            }
        }
    }

    private int FindCSTIndex(Control cst_ctr)
    {
        fl_panel = (FlowLayoutPanel)tabControl1.SelectedTab.Controls[0];
        for (int i = 0; i < this.fl_panel.Controls.Count; i++)
        {
            CustomControl target = this.fl_panel.Controls[i] as CustomControl;

            if (cst_ctr.Parent == target)
                return i;
        }
        return -1;
    }

This is not an easy, nor a common task. 这不是一件容易的事,也不是常见的任务。 But surely doable and depending on preconditions could become trivial without need to spend multi-man-year effort on it ^^. 但是,确实可行且取决于前提条件的方法可能变得微不足道,而无需花很多年的精力在上面^^。

You have many options: 您有很多选择:

  • controls support selection; 控制支持选择;
  • container control support children controls selection; 容器控件支持子控件的选择;
  • overlay. 覆盖。

Handling selection is pretty easy: have a dictionary (or a control property, possibly using Tag ) to store if control is selected or not, show selection somehow, when control is Ctrl -clicked invert selection. 处理选择非常容易:拥有字典(或控件属性,可能使用Tag )来存储是否选择控件,以Ctrl方式单击反转选择时,以某种方式显示选择。 You can even provide Shift -key selection. 您甚至可以提供Shift键选择。

As @Hans Passant commented, you can use overlay window (invisible window on top of everything) to draw selection reticle there as well as handle selection and dragging itself. 正如@Hans Passant所评论的那样,您可以使用覆盖窗口(位于所有内容顶部的不可见窗口)在此处绘制选择标线以及处理选择并拖动自身。 Or it could be a custom control with property IsSelected , setting which will draw something (border?) to indicate selection. 或者它可以是具有属性IsSelected ,该设置将绘制某些内容(边框?)以指示选择。

Easiest option would be to create SelectionPanel control, which can host any other controls inside, has IsSelected indication and is draggable. 最简单的选择是创建SelectionPanel控件,该控件可以承载内部的任何其他控件,具有IsSelected指示并且可拖动。 When children is added subscribe to MouseUp / MouseDown events or you can only allow to drag if special area of SelectionPanel is clicked. 添加子项后,请订阅MouseUp / MouseDown事件,或者仅在单击SelectionPanel特殊区域时才允许拖动。 To example, you could have option Enable dragging in your software, when set all SelectionPanel s will display special area (header?) which you can drag or Ctrl -click. 例如,您可以在软件中选择“ 启用拖动” ,设置后,所有SelectionPanel都会显示特殊区域(标题?),您可以拖动或按Ctrl键并单击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM