是否可以通过点击其中一个子控件来移动组合控件? [英] is it possible to move a composite control by clicking on one of its daughter controls?

查看:141
本文介绍了是否可以通过点击其中一个子控件来移动组合控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





目前我只能通过复合控件的边缘来做到这一点。甚至可以点击复合控件中的子/子控件,以便能够移动整个复合控件?



这是我的代码将控件添加到主窗体 panel3 中:

  private void newPictureBox_Click(object sender,EventArgs e)
{
UserControl1 _UserControl = new UserControl1();
PictureBox _PictureBox =(PictureBox)发件人;
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 =发件人为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);
}


解决方案

没有简单的方法做这个。



您可以尝试递归附加您的事件处理程序给每个子控件。

这样的一个例子:

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

private void ApplyChildEvents(控件控件)
{
foreach(control.Controls中的控件子控件)
{
subcontrol.MouseMove + = _UserControl_MouseMove;
subcontrol.DragDrop + = _UserControl_DragDrop;
subcontrol.DragEnter + = _UserControl_DragEnter;

ApplyChildEvents(subcontrol);
}
}
}

所以,你的所有控件 UserControl1 将调用此方法。


What I am attempting to accomplish is to move by drag and drop method a bunch of composite controls as outlined by the following image:

Currently I am able to do this by only the edge of the composite control. Is it even possible to be able to click on a child/daughter control in the composite control to be able to move the entire composite control?

Here is my code for the addition of the control to panel3 of the main form:

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);
}

解决方案

There is no simple way to do this.

You can try to attach your event handler to every child controls recursively.
Something like this:

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);
        }
    }
}

So, all controls on your UserControl1 will call this method.

这篇关于是否可以通过点击其中一个子控件来移动组合控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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