简体   繁体   中英

Get sender of mouseDown event

I need to get the sender of the mouseDown event from within the event and set it as a global variable to use in a dragDrop event, so that it calls a method depending on what picturebox was dragged. I need the control name or something. My attempt:

Global variable "dragSource":

public partial class MapDesignerView : Form
    {
        public Map myMap { get; set; }
        public MapController myMapController { get; set; }
        public MapConstructor myMapConstructor { get; set; }
        public MouseEventHandler myDetectMouse { get; set; }


        object dragSource = null;

mouseDown

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

DragDrop

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

So what exactly isn't working... the only thing I can think that might be causing the problem is that you're storing the reference to the entire control in your drag source.

A better idea might be to just story the Id. and then test it based on the Id further down.

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

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