Context menus

Walter,

I was also wondering what I could do about detecting whether or not the user was right clicking on a node or just somewhere on the view/document where there is empty space.
Right now I have my context menu working but only when the user right-clicks on an actual node. If they right-click in an empty area a runtime error is generated b/c I am overriding the GetContextMenu() method in my custom GoTextNode and in it I refer to the node itself using "this.SomeProperty". Since there is no "this" b/c the user didn't right-click on a node I get the error.
Since the eventhandler is triggered using OnBackgroundContextClicked, I would like to perform some form of validation to see if there is a GoObject at the particular coordinates that the user clicked.
Could I do something like:
Point pscreen = goView1.PointToScreen(goView1.LastInput.ViewPoint); Point p1view = goView1.PointToClient(pscreen); PointF p1doc = goView1.ConvertViewToDoc(p1view); GoObject obj = goView1.PickObject(true, false, p1doc, true) as GoObject;
if (obj is PersonNode) {
PersonNode node = obj as PersonNode;
node.ShowContextMenu(goView1);
}
I haven't tried this yet but I was wondering if I am headed in the right direction?
Thanks!

There are parallel sets of GoView events depending on whether the click is happening on a GoObject or not:

BackgroundSingleClicked, ObjectSingleClicked
BackgroundDoubleClicked, ObjectDoubleClicked
BackgroundContextClicked, ObjectContextClicked
Also:
BackgroundHover, ObjectHover
BackgroundSelectionDropReject, ObjectSelectionDropReject
BackgroundSelectionDropped, ObjectSelectionDropped
The Background... events get a GoInputEventArgs;
the corresponding Object... events get a GoObjectEventArgs.
Are you trying to generate different context menus depending on whether there is an object, and perhaps on what kind of object, when there is a context click? You can assign a regular context menu to the GoView, and define a GoView.ObjectContextClicked event handler to handle all context clicks on GoObjects. That event handler can decide which context menu to bring up, as follows:
[code] private void goView1_ObjectContextClicked(object sender, GoObjectEventArgs e) {
GoBasicNode node = e.GoObject.ParentNode as GoBasicNode;
if (node != null) { // and not a link (nor a different kind of node, nor some other kind of object)
contextMenuStrip2.Show(goView1.PointToScreen(e.ViewPoint));
}
}
private void nodeCommandToolStripMenuItem_Click(object sender, EventArgs e) {
GoBasicNode node = goView1.PickObject(true, false, goView1.LastInput.DocPoint, true) as GoBasicNode;
if (node != null) {
MessageBox.Show(node.Text);
}
}
private void backgroundCommandToolStripMenuItem_Click(object sender, EventArgs e) {
GoBasicNode node = new GoBasicNode();
node.LabelSpot = GoObject.Middle;
node.Text = goView1.LastInput.DocPoint.ToString();
node.Location = goView1.LastInput.DocPoint;
goView1.Document.Add(node);
}[/code]
where the context menu for nodes is defined by the Form designer:
[code] //
// contextMenuStrip2
//
this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.nodeCommandToolStripMenuItem});
this.contextMenuStrip2.Name = "contextMenuStrip2";
this.contextMenuStrip2.Size = new System.Drawing.Size(161, 26);
//
// nodeCommandToolStripMenuItem
//
this.nodeCommandToolStripMenuItem.Name = "nodeCommandToolStripMenuItem";
this.nodeCommandToolStripMenuItem.Size = new System.Drawing.Size(160, 22);
this.nodeCommandToolStripMenuItem.Text = "Node Command";
this.nodeCommandToolStripMenuItem.Click += new System.EventHandler(this.nodeCommandToolStripMenuItem_Click);[/code]