質問

public void TapeMeasure(object sender, EventArgs e)
  {
     if (TrussManager.Truss != null)
     {
        Point containerPoint = trussEditor.PointToClient(Cursor.Position);

        if (!trussEditor.MainMenu.CommandToolStrip.ClientRectangle.Contains(containerPoint))
           execute command A
        else
        execute command B
     }
  }

this event is called from

ToolStripButton commandButton = new ToolStripButton("Tape", ConvertIconToImage(icon), TapeMeasure);

and

ToolStripMenuItem tsmi = new ToolStripMenuItem("Tape", ConvertIconToImage(icon), TapeMeasure);

(Winforms Application)

I want to know when my cursor is not my Toolstrip. However the above code keeps returning the same result regardless of where my cursor is.

This code is located in an eventhandler that is called from either a button on Toolstrip or a Button on a contextmenu. If its called on a context menu, I assume the user wants to use the current mousepoint. Otherwise I want the user to go click the point they want

any suggestions?

役に立ちましたか?

解決

Since you are using the MouseClick event to initiate your Method the Sender Object of the Click Event will have the object that originated the Event. In this case I would just determine the Type of the Sender, since one is ToolStripButton and the other is a MenuItem. As I Mentioned in Chat the Cursor.Point is constantly being updated which is what I think is causing your problems.

This Example will determine which Object generated the ClickEvent and run the approriate method.

public void TapeMeasure(object sender, EventArgs e) 
{ 
    if (TrussManager.Truss != null) 
    { 
        System.Type sysType = sender.GetType();

        if (!(sysType.Name == "ToolStripButton"))
            //execute command A 
        else 
            //execute command B 
    } 
} 

And this example will take in to account the location of the ContextMenu and process the same Method as Clicking the Button if it is contained in the toolBar.

public void TapeMeasure(object sender, EventArgs e) 
{ 
    if (TrussManager.Truss != null) 
    {
        System.Type sysType = sender.GetType();
        if (!(sysType.Name == "ToolStripButton"))
        {
            if (sysType.Name == "ToolStripMenuItem")
            {
                ToolStripMenuItem temp = (ToolStripMenuItem)sender;
                if (trussEditor.MainMenu.CommandToolStrip.ClientRectangle.Contains(trussEditor.MainMenu.CommandToolStrip.PointToClient(temp.Owner.Location)))
                {
                    //execute command A
                }
                else
                {
                    //execute command B
                }
            }
        }
        else
        {
            //execute command A
        }
    } 
} 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top