使用类方法进行拖放 [英] Drag and drop using a class method

查看:200
本文介绍了使用类方法进行拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列拖放活动在我的测验中都能正常工作。 (从回答问题到选择头像)



有很多重复的代码,我认为它会更好地从类中实现,并调用每个方法时间我使用拖放。



首先可以做到这一点吗?其次,我需要一种新的拖放方法?



任何想法或粗略的想法都会很棒。

  private void pictureBox2_MouseDown(object sender,MouseEventArgs e)
{
pictureBox2.DoDragDrop(pictureBox2.Image,DragDropEffects.Copy);
}

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

private void panel1_DragDrop(object sender,DragEventArgs e)
{
//将面板的背景图像设置为选定的头像'
panel1.BackgroundImage =(Image)e.Data.GetData(DataFormats.Bitmap);
}


解决方案

如果你想要的是避免复制几个控件的相同事件,您应该使用常见事件:

  private void commonPBox_MouseDown(object sender,MouseEventArgs e)
{
PictureBox PB =发件人为PictureBox;
if(PB == null)return; //或抛出一个异常

PB.DoDragDrop(PB.Image,DragDropEffects.Copy);
}

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

private void commonPanel_DragDrop(object sender,DragEventArgs e)
{
Panel Pan = sender as Panel;
if(Pan == null)return; //或抛出一个异常

//将面板的背景图像设置为选定的头像
Pan.BackgroundImage =(Image)e.Data.GetData(DataFormats.Bitmap);
}

选择相应的控件,并将事件名称输入到相应的事件名称插槽中请注意,如何投放事件的发件人参数,以获取类型的属性选项卡的事件窗格。



引用触发事件的控件。如果投票错误,参考设置为 null



如果您想要更多的控制和更灵活的话,你可以考虑创建一个 DragAndDropcontroller class ..:

 静态类DnDCtl 
{
static List< Control> Targets = new List< Control>();
static List< Control> Sources = new List< Control>();

static public void RegisterSource(Control ctl)
{
if(!Sources.Contains(ctl))
{
Sources.Add(ctl) ;
ctl.MouseDown + = ctl_MouseDown;
}
}

static public void UnregisterSource(Control ctl)
{
if(Sources.Contains(ctl))
{
Sources.Remove(ctl);
}
}

static public void RegisterTarget(Control ctl)
{
if(!Targets.Contains(ctl))
{
Targets.Add(ctl);
ctl.DragEnter + = ctl_DragEnter;
ctl.DragDrop + = ctl_DragDrop;
ctl.AllowDrop = true;
}
}

static public void UnregisterTarget(Control ctl)
{
if(Targets.Contains(ctl))
{
Targets.Remove(ctl);
ctl.DragEnter - = ctl_DragEnter;
ctl.DragDrop - = ctl_DragDrop;

}
}

static void ctl_MouseDown(object sender,MouseEventArgs e)
{
PictureBox PB =发件人为PictureBox;
if(PB!= null)PB.DoDragDrop(PB.Image,DragDropEffects.Copy);

面板Pan =发送者作为面板;
if(Pan!= null)Pan.DoDragDrop(Pan.BackgroundImage,DragDropEffects.Copy);
}

static void ctl_DragEnter(object sender,DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

static void ctl_DragDrop(object sender,DragEventArgs e)
{
面板Pan =发件人作为面板;
if(Pan!= null)Pan.BackgroundImage =(Image)e.Data.GetData(DataFormats.Bitmap);

PictureBox PB =发件人为PictureBox;
if(PB!= null)PB.BackgroundImage =(Image)e.Data.GetData(DataFormats.Bitmap);
}
}

注意:




  • 我已经为面板编码对称动作 PictureBoxes 来显示您可以处理不同的控件。

  • 我已经创建但未使用源$和目标的列表。对于更复杂的项目,你会发现它们很有用。

  • 我编码了注册取消注册方法。在不再适用的情况下,您可以注册一些条件并取消注册。

  • 这样的控制器适用于动态地减轻或禁止拖放控件。当您动态创建它们。

  • 您还可以绕过代理人将dragdrop操作与控制器分离。



另外请注意,有些人有时更喜欢使用 MouseMove 事件超过 MouseDown esp。因为它不会那么容易干扰进行选择。



ListView 具有专门的事件,而$ code> ListView.ItemDrag ,这显然应该在注册时使用!


I have a range of drag drop activities all working fine on my quiz. (From answering questions to selecting an avatar)

There is a lot of repetition of the code and I am thinking it would be better implemented from a class and call the method each time that I use a drag drop.

Firstly can this be done? and secondly would I need a new method for the dragging and dropping?

Any thoughts or rough ideas would be great.

private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
pictureBox2.DoDragDrop(pictureBox2.Image, DragDropEffects.Copy);
}

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

private void panel1_DragDrop(object sender, DragEventArgs e)
{
    //Set background image of panel to selected avatar'
    panel1.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap);
}

解决方案

If all you want is to avoid duplicating the same events of several controls you should use common events:

private void commonPBox_MouseDown(object sender, MouseEventArgs e)
{
    PictureBox PB = sender as PictureBox;
    if (PB == null) return; //or throw an exception

    PB.DoDragDrop(PB.Image, DragDropEffects.Copy);
}

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

private void commonPanel_DragDrop(object sender, DragEventArgs e)
{
    Panel Pan = sender as Panel;
    if (Pan == null) return; //or throw an exception

    //Set background image of panel to selected avatar
    Pan.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap);
}

Select the respective controls and enter the event names into the respective event name slots in the event pane of the properties tab.

Note how I cast the sender param of the event to get a types reference to the control that triggers the event. If the cast goes wrong the reference is set to null.

If you want more control and more flexibilty you may consider creating a DragAndDropcontroller class..:

static class DnDCtl
{
    static List<Control> Targets = new List<Control>();
    static List<Control> Sources = new List<Control>();

    static public void RegisterSource(Control ctl)
    {
        if (!Sources.Contains(ctl) ) 
        {
            Sources.Add(ctl);
            ctl.MouseDown += ctl_MouseDown;
        }
    }

    static public void UnregisterSource(Control ctl)
    {
        if (Sources.Contains(ctl))
        {
            Sources.Remove(ctl);
        }
    }

    static public void RegisterTarget(Control ctl)
    {
        if (!Targets.Contains(ctl))
        {
            Targets.Add(ctl);
            ctl.DragEnter += ctl_DragEnter;
            ctl.DragDrop += ctl_DragDrop;
            ctl.AllowDrop = true;
        }
    }

    static public void UnregisterTarget(Control ctl)
    {
        if (Targets.Contains(ctl))
        {
            Targets.Remove(ctl);
            ctl.DragEnter -= ctl_DragEnter;
            ctl.DragDrop -= ctl_DragDrop;

        }
    }

    static void ctl_MouseDown(object sender, MouseEventArgs e)
    {
        PictureBox PB = sender as PictureBox;
        if (PB != null) PB.DoDragDrop(PB.Image, DragDropEffects.Copy);

        Panel Pan = sender as Panel;
        if (Pan != null) Pan.DoDragDrop(Pan.BackgroundImage, DragDropEffects.Copy);
    }

    static void ctl_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    static void ctl_DragDrop(object sender, DragEventArgs e)
    {
        Panel Pan = sender as Panel;
        if (Pan != null) Pan.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap);

        PictureBox PB = sender as PictureBox;
        if (PB != null) PB.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap);
    }
}

Notes:

  • I have coded symmetric actions for Panels and PictureBoxes to show how you can handle different controls.
  • I have created but not used Lists of sources and targets. For more complex projects you will find them useful.
  • I have coded both Register and Unregister methods. You can register after some condition and unregister when it no longer applies
  • Such a Controller is good for dynamically alowing or disallowing Drag&Drop for controls, esp. when you create them dynamically.
  • You could also pass around delegates to decouple the dragdrop action from the controller.

Also note that some folks sometimes do prefer to use the MouseMove event over MouseDown esp. as it won't so easily interfere with making a selection.

ListView has a dedicated event instead, ListView.ItemDrag, which obviously should be used when registering!

这篇关于使用类方法进行拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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