Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Contextmenu problem when using waveformgraph

I am tring to popup contextmenu when I press the rigit mouse button on a .NET waveformgraph control. If I connect contextmenu to waveformgraph, it works well in normal situation. However, the contextmenu also apears when I am using new Zoom and Pan functionality of .Net waveformgrraph control. If I clik right mouse button to restore zoom, the contextmenu also apears.
 
To solve this problem, I tried as follows. I tired to ignore the right mouse button click event if the ctrl and shift keys are pressed. However it doesnot works. I think I failed to catch ctrl and shift key status. Please teach me the solution.
 
  private void wGraph_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if (e.Button == MouseButtons.Right)
   {
    if ((Control.ModifierKeys == Keys.ControlKey) | (Control.ModifierKeys == Keys.ShiftKey)) return;
    Point mPnt = new Point(e.X,e.Y);
    this.contextMenu1.Show(this,mPnt);
   }
  }
 
Thank you.
 
 
0 Kudos
Message 1 of 5
(3,690 Views)

You must use Keys.Control instead of Keys.ControlKey and Keys.Shift instead of Keys.ShiftKey. Also, the Keys enumeration is attributed with FlagsAttribute, which means that the value may be a bitwise combination of enumeration values. For example, if you are explicitly checking Control.ModifierKeys to be equal to Keys.Control and both the CTRL and SHIFT modifier keys are pressed, the check will fail. Therefore, you should check to see if the Keys.Control or Keys.Shift bit is set in the Control.ModifierKeys value. Here is your example with the suggested modifications that will make it work:

private void wGraph_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        if ((Control.ModifierKeys & (Keys.Control | Keys.Shift)) > 0) return;
        Point mPnt = new Point(e.X,e.Y);
        this.contextMenu1.Show(wGraph, mPnt);
    }
}

- Elton

0 Kudos
Message 2 of 5
(3,674 Views)

Thank you Elton. However, there occurs error.

If I use the code you suggested, I got the error message "System.Window.Forms.Keys Type cannot be converted into bool Type."

If I modify the code as follow,

if ((Control.ModifierKeys == Keys.Control) | (Control.ModifierKeys == Keys.Shift))

it works. But Shift + Ctrl Key generates unexpected response as you commented.

Do you have any idea?

 

Thanks you.

 

 

0 Kudos
Message 3 of 5
(3,668 Views)

How did you test the code that I suggested? I copied and pasted the code into a new test project and it compiled with no errors or warnings and the application behaved as expected when I ran it.

- Elton

0 Kudos
Message 4 of 5
(3,663 Views)

Sorry Elton. It's my typo.

Thank you very much.

 

0 Kudos
Message 5 of 5
(3,659 Views)