Click here to Skip to main content
16,020,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I want to display a vertical line cursor on an mschart based on a menu option that is checked/unchecked using c#.

What I have tried:

I can turn the event handler on like this below and the cursor tracks the mouse as it should:

C#
chart.MouseMove += new MouseEventHandler(chart_MouseMove);


but I want to disable the event when the menu option is unchecked. I've tried using

C#
chart.MouseMove -= new MouseEventHandler(chart_MouseMove);


which is supposed to turn off the event handler, but it doesn't work. I've also tried setting a bool flag that gets the menu option checked state and then testing for the flag in the event handler (like below), but it ignores it and just keeps tracking the mouse.

C#
private void chart_MouseMove(object sender, MouseEventArgs e) {
   if (_bVCursor) {
      Point mousePoint = new Point(e.X, e.Y);
      chArea.CursorX.SetCursorPixelPosition(mousePoint, true);
   }
}
Posted
Comments
[no name] 27-Jun-24 0:22am    
don't stop 'mousemove tracking', but stop the 'display render'..
It's using the other side of the feature you want.
Darryl Bryk 27-Jun-24 12:47pm    
Not sure how to do that, but that might be a forced way to do it, although it may waste resources as its still mouse tracking in the background?

You need to check your other code - if I try your code in one of my apps, it works fine:
C#
static int x = 0;
private void LoadFromFile_Click(object sender, EventArgs e)
    {
    chart.MouseMove += new MouseEventHandler(chart_MouseMove);
    }

private void SaveToFile_Click(object sender, EventArgs e)
    {
    chart.MouseMove -= new MouseEventHandler(chart_MouseMove);
    }
private void chart_MouseMove(object sender, MouseEventArgs e)
    {
    label1.Text = (x++).ToString();
    }
When I move over the control, the label doesn't change. Press the first button, and it increments. Press the second, it stops changing. Repeat and it works every time (provided I don't add two handlers and only remove one in which case a second remove takes that out as well.).
 
Share this answer
 
Comments
0x01AA 27-Jun-24 10:47am    
But is it not strange that this works? I mean when removing with chart.MouseMove -= new MouseEventHandler(chart_MouseMove); it 'removes' an instance created with new and which has not been added yet. I'm confused...
Chris Copeland 1-Jul-24 6:06am    
I believe it's because MouseEventHandler is a delegate which functions as a pointer to the address of the function being called, so behind the scenes the Delegate.Combine() and Delegate.Remove() methods refer to the pointer address directly. It is confusing though.
0x01AA 1-Jul-24 8:36am    
Makes sense. Thank you very much.
Darryl Bryk 1-Jul-24 14:44pm    
I don't understand why you said it removes an instance which hasn't been added yet. But the instance was added by a menu option, and then later removed, by the menu option. I suppose if it could somehow get removed before it was added (which wasn't possible in my code), then the remove would not find the call pointer or it would be null or something and code would continue without crashing - assume some try block in the system code.
Darryl Bryk 27-Jun-24 12:48pm    
Thats interesting. What version of .Net are you running?
OK, got it working! My bool variable that was supposed to keep track of the menu state needed to be declared static - it was getting reset at each re-entry to the class.
 
Share this answer
 

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