是否可以通过单击其中一个子控件来移动复合控件?

时间:2015-11-04 04:22:22

标签: c# winforms drag-and-drop

我试图通过拖放方法移动一堆复合控件,如下图所示:

CompositeControl Example

目前我只能通过复合控件的边缘来完成此操作。甚至可以单击复合控件中的子/子控件来移动整个复合控件吗?

以下是我将代码添加到主窗体panel3的代码:

private void newPictureBox_Click(object sender, EventArgs e)
{
    UserControl1 _UserControl = new UserControl1();
    PictureBox _PictureBox = (PictureBox)sender;
    string _NewControlClusterName = "_New" + _PictureBox.Name;

    _UserControl.Name = _NewControlClusterName;
    _UserControl.ThreadCount = 16;
    _UserControl.ImageBackground = _PictureBox.BackColor;
    _UserControl.Dock = DockStyle.Top;
    _UserControl.AllowDrop = true;
    _UserControl.Cursor = Cursors.SizeAll;

    _UserControl.MouseMove += _UserControl_MouseMove;
    _UserControl.DragDrop += _UserControl_DragDrop;
    _UserControl.DragEnter += _UserControl_DragEnter;

    string ColorName = toolTip1.GetToolTip(_PictureBox);
    string ColorCode = toolTip2.GetToolTip(_PictureBox);
    toolTip1.SetToolTip(_UserControl.pictureBox1, ColorName);
    toolTip2.SetToolTip(_UserControl.pictureBox1, ColorCode);
    toolTip2.Active = false;

    _UserControl.PictureClick += new EventHandler(ClusterControl_Click);
    _UserControl.TrackBarScroll += new EventHandler(GetTartanCode);

    panel3.Controls.Add(_UserControl);
    panel3.Controls.SetChildIndex(_UserControl, 0);
}

private void _UserControl_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void _UserControl_DragDrop(object sender, DragEventArgs e)
{
    UserControl1 target = sender as UserControl1;
    if (target != null)
    {
        int targetIndex = FindUserControlIndex(target);
        if (targetIndex != -1)
        {
            string _UserControlFormat = typeof(UserControl1).FullName;
            if (e.Data.GetDataPresent(_UserControlFormat))
            {
                UserControl1 source = e.Data.GetData(_UserControlFormat) as UserControl1;
                int sourceIndex = this.FindUserControlIndex(source);
                if (targetIndex != -1)
                    this.panel3.Controls.SetChildIndex(source, targetIndex);
            }
        }
    }
}

private int FindUserControlIndex(UserControl1 _UserControl)
{
    for (int i = 0; i < this.panel3.Controls.Count; i++)
    {
        UserControl1 target = this.panel3.Controls[i] as UserControl1;
        if (_UserControl == target)
            return i;
    }
    return -1;
}

private void _UserControl_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        UserControl1 _UserControl1 = sender as UserControl1;
        _UserControl1.BorderStyle = BorderStyle.Fixed3D;
        _UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
    }
}

private void ClusterControl_Click(object sender, EventArgs e)
{
    PictureBox _PictureBox = (PictureBox)sender;
    GroupBox _GroupBox = (GroupBox)_PictureBox.Parent;
    UserControl1 _UserControl1 = (UserControl1)_GroupBox.Parent;
    panel3.Controls.Remove(_UserControl1);
}

2 个答案:

答案 0 :(得分:2)

没有简单的方法可以做到这一点。

您可以尝试递归地将事件处理程序附加到每个子控件 像这样:

public class UserControl1 : Control
{
    public UserControl1()
    {
        // ...
        ApplyChildEvents(this);
    }

    private void ApplyChildEvents(Control control)
    {
        foreach (Control subcontrol in control.Controls)
        {
            subcontrol.MouseMove += _UserControl_MouseMove;
            subcontrol.DragDrop += _UserControl_DragDrop;
            subcontrol.DragEnter += _UserControl_DragEnter;

            ApplyChildEvents(subcontrol);
        }
    }
}

因此,UserControl1上的所有控件都会调用此方法。

答案 1 :(得分:1)

使用上面的答案以及对_UserControl_MouseMove事件处理程序的以下代码调整,它现在可以正常工作。

$timeZone = new DateTimeZone('UTC');
$dt = new DateTime($from_date,$timeZone);
$dt->setTime(23, 59, 59);
echo $format=$dt->format('Y-m-d H:i:s');

并且还必须将上面的代码更改为以下内容(因为后面提到的区域是动态生成的):

<强> newPictureBox_Click

private void _UserControl_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (sender.GetType() == typeof(GroupBox))
        {
            GroupBox _GroupBox = sender as GroupBox;
            UserControl1 _UserControl1 = _GroupBox.Parent as UserControl1;
            _UserControl1.BorderStyle = BorderStyle.Fixed3D;
            _UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
        }
        else
        {
            UserControl1 _UserControl1 = sender as UserControl1;
            _UserControl1.BorderStyle = BorderStyle.Fixed3D;
            _UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
        }
    }
}

完整代码如下:

foreach (Control subcontrol in _UserControl.Controls)
{
    if (subcontrol.GetType() == typeof(GroupBox)){
        subcontrol.MouseMove += _UserControl_MouseMove;
        subcontrol.DragDrop += _UserControl_DragDrop;
        subcontrol.DragEnter += _UserControl_DragEnter;
    }
}