C# 获取mouseDown事件的发送者

C# 获取mouseDown事件的发送者,c#,user-interface,mouseevent,C#,User Interface,Mouseevent,我需要从事件中获取mouseDown事件的发送方,并将其设置为全局变量以在dragDrop事件中使用,以便它根据拖动的picturebox调用方法。我需要控件名或其他什么。我的尝试: 全局变量“dragSource”: 穆斯敦 private void pbxMinotaur_MouseDown(object sender, MouseEventArgs e) { pbxMap.AllowDrop = true;

我需要从事件中获取mouseDown事件的发送方,并将其设置为全局变量以在dragDrop事件中使用,以便它根据拖动的picturebox调用方法。我需要控件名或其他什么。我的尝试:

全局变量“dragSource”:

穆斯敦

private void pbxMinotaur_MouseDown(object sender, MouseEventArgs e)
            {
                pbxMap.AllowDrop = true;
                pbxMinotaur.DoDragDrop(pbxMinotaur.Name, DragDropEffects.Copy |
                DragDropEffects.Move);
                dragSource = sender;
            }
拖拽

private void pbxMap_DragDrop(object sender, DragEventArgs e)
        {
            {
                if (dragSource == pbxMinotaur)
                {
                    myDetectMouse.setMinotaur(e, myMap.myCells);
                }

那么到底是什么不起作用。。。我认为唯一可能导致问题的原因是,您正在将对整个控件的引用存储在拖动源中

一个更好的办法可能是只描述Id,然后根据更下面的Id进行测试

 string dragSourceName = null;


 private void pbxMinotaur_MouseDown(object sender, MouseEventArgs e)
        {
            pbxMap.AllowDrop = true;
            pbxMinotaur.DoDragDrop(pbxMinotaur.Name, DragDropEffects.Copy |
            DragDropEffects.Move);
            Control c = (sender as Control);
            if(c != null)
                 dragSourceName = c.Name;
        }

    private void pbxMap_DragDrop(object sender, DragEventArgs e)
    {
        if (dragSourceName == pbxMinotaur.Name)
        {
            myDetectMouse.setMinotaur(e, myMap.myCells);
        }

这里的问题是什么?我的意思是你面临什么样的问题?鼠标向下和拖放可能都有相同的发送器,因为一个逻辑上遵循另一个。这应该没有必要。谢谢。我获取并出错“System.Window.Forms.Control”不包含“Id”的定义。在我把寄件人打印在标签上,上面写着系统…图片盒。我确实需要身份证、姓名或其他什么。抱歉。。。处于ASP模式。是的,控件的名称属性就是我的意思。这很有效。我已经在这上面呆了一段时间了。非常感谢。值得注意的是,如果有人尝试这样做,我必须将代码从MouseDown方法中的“Control c=“on”移动到方法的顶部。
 string dragSourceName = null;


 private void pbxMinotaur_MouseDown(object sender, MouseEventArgs e)
        {
            pbxMap.AllowDrop = true;
            pbxMinotaur.DoDragDrop(pbxMinotaur.Name, DragDropEffects.Copy |
            DragDropEffects.Move);
            Control c = (sender as Control);
            if(c != null)
                 dragSourceName = c.Name;
        }

    private void pbxMap_DragDrop(object sender, DragEventArgs e)
    {
        if (dragSourceName == pbxMinotaur.Name)
        {
            myDetectMouse.setMinotaur(e, myMap.myCells);
        }