Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all!

I got custom window which need to be dragged around but some cases I need to ignore dragging.

At the moment I subcribe delegate in window constructor

MouseLeftButtonDown += delegate { DragMove(); };


Which works fine and enables me to drag my custom window around

When I like to disable drag event I call

MouseLeftButtonDown -= delegate { DragMove(); };


Which does not work, Im still able to drag my custom window..

So how I properly unsubcribe my delegate?

Hope you got idea :)

Cheers!
Posted

You should do this by creating a bool varaiable (call it canDrag) as a form level variable and explicitly creating a MouseLeftButtonDown event which will only drag if canDrag = true. Something like this:-

C#
bool canDrag = false;

       public MainWindow()
       {
           InitializeComponent();
           MouseLeftButtonDown +=new System.Windows.Input.MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
       }

       void MainWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
       {
           if (canDrag)
           {
               this.DragMove();
           }
       }


You can then set canDrag to true and false in the appropriate places. I used buttons to test like this:-

C#
private void btnDrag_Click(object sender, RoutedEventArgs e)
        {
            canDrag = true;
        }

        private void btnNoDrag_Click(object sender, RoutedEventArgs e)
        {
            canDrag = false ;
        }


Hope this helps
 
Share this answer
 
Comments
paleGegg0 21-Oct-11 0:24am    
thanks for your time and guidance :)
The reason it doesn't work is because
C#
delegate { DragMove(); }

and
C#
delegate { DragMove(); }

and not equal thus the delegate is not removed.
You have to store the delegate in order to be able to remove it again, like this:
C#
MouseButtonEventHandler handler = delegate { GetMoreData(); };
MouseLeftButtonDown += handler;
MouseLeftButtonDown -= handler;
 
Share this answer
 
Comments
paleGegg0 21-Oct-11 0:25am    
Hi! Thanks for your time! Did not knew that delegates work this way, now I know :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900