简体   繁体   中英

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. 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?

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. You can even provide Shift -key selection.

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. Or it could be a custom control with property IsSelected , setting which will draw something (border?) to indicate selection.

Easiest option would be to create SelectionPanel control, which can host any other controls inside, has IsSelected indication and is draggable. When children is added subscribe to MouseUp / MouseDown events or you can only allow to drag if special area of SelectionPanel is clicked. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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