tomet
tomet

Reputation: 2556

Mouse up event doesn't get triggered

I have the following problem: I have a panel which has a specific color, say red. When the user presses his mouse, the color of this panel gets stored in a variable. Then the user moves, his mouse still pressed, over to another panel. When he releases the mouse there, this panel should get the background color of the first that had been stored in the variable. My code looks something like this:

    public Color currentColor;
    private void ColorPickMouseDown(object sender, MouseEventArgs e)
    {
        Panel pnlSender = (Panel)sender;                   
        currentColor = pnlSender.BackColor;
    }

    private void AttempsColorChanger(object sender, MouseEventArgs e)
    {
        Panel pnl = (Panel)sender;
        pnl.BackColor = currentColor;
    }

I need to identify the sender first because there are many possible panels that can trigger this event. The first MouseDown method works totally fine, the color is stored nicely in the variable. The secon one however doesn't even get triggered when the user does what I described above. When the ser clicks on the second panel, it works (there is an MouseUp part in a click aswell I guess).

What's wrong here? Why is the event not triggered when the user holds the mouse key down before?

Upvotes: 0

Views: 10644

Answers (4)

Anya Hope
Anya Hope

Reputation: 1361

I know it's an old question but I had the same issue and none of the above answers worked for me. In my case I had to handle the MouseMove event in the target control and check for the mouse to be released. I did set 'BringToFront' on my target panel just in case that helped at all.

public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
    Panel pnlSender = (Panel)sender;                   
    currentColor = pnlSender.BackColor;
}

private void panelTarget_MouseMove(object sender, MouseEventArgs e)
{
    //the mouse button is released
    if (SortMouseLocation == Point.Empty)
    {
        Panel pnl = (Panel)sender;
        pnl.BackColor = currentColor;
    }
}

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

As I mentioned in my comment Mouse Events are captured by the originating control, You would probably be better off using the DragDrop functionality built into Windows Forms. Something like this should work for you. I assigned common event handlers, so they can be assigned to all of your panels and just work.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void panel_MouseDown(object sender, MouseEventArgs e)
    {
       ((Control)sender).DoDragDrop(((Control)sender).BackColor,DragDropEffects.All);
    }

    private void panel_DragDrop(object sender, DragEventArgs e)
    {
        ((Control)sender).BackColor = (Color)e.Data.GetData(BackColor.GetType());
    }
    private void panel_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

}

Upvotes: 1

mojtaba
mojtaba

Reputation: 339

when your mouse enter the target control , mouse down triggerd ang get target BackColor! you need add an boolean flag to your code :

 public Color currentColor;       
 bool flag=false;
    private void ColorPickMouseDown(object sender, MouseEventArgs e)
    {
        if(flag==false)
        {
        flag=true
        Panel pnlSender = (Panel)sender;                   
        currentColor = pnlSender.BackColor;
       }
    }
    //assume mouse up for panles
    private void AttempsColorChanger(object sender, MouseEventArgs e)
    {
       if(flag==true)
       {  
        Panel pnl = (Panel)sender;
        pnl.BackColor = currentColor;
        flag=flase;
       }
    }

and also you need change your flag in mouseMove( if )

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109567

(This answer assumes you are using Windows Forms.)

It could be that you need to capture the mouse by setting this.Capture = true in the MouseDown of the source control. (See Control.Capture)

If you did that, the source window would get the MouseUp event, and it would be the source window that had to determine the destination window under the mouse coords. You can do that using Control.GetChildAtPoint() (see this answer on Stack Overflow).

Use Windows Forms Drag and Drop Support Instead! <- Click for more info

I'm going to suggest you bite the bullet and use the .Net Drag and Drop methods to do this. It requires some reading up, but it will be much better to use it.

You start a drag in response to a MouseDown event by calling Control.DoDragDrop().

Then you need to handle the Control.DragDrop event in the drop target control.

There's a few more things you might need to do to set it up; see the Control.DoDragDrop() documentation for an example.

(For WPF drag and drop support, see here.)

Upvotes: 4

Related Questions