Adding Nodes, Links via C# Code

I’m sure I’m missing something obvious here, but I am trying to create the functionality of double clicking an object on the palette and having it create the node on the canvas, and if a node already exists on the canvas, to connect them.
I have it working, but to get it to draw the nodes, I have to single click on the canvas. I even found some code you provided in another thread and it works great, except, to get it the nodes to appear, I have to single click the canvas.
I built my code on the FlowCharter sample. I’m wondering if I’m not missing a single click event or something. I did remove the background single click event handler that the sample had, but maybe I didn’t remove it enough?

Any ideas?

Here’s the code i found…
// create two nodes for fun…
GoBasicNode node1 = new GoBasicNode();
// specify label, appearance, and position
node1.LabelSpot = GoObject.Middle;
node1.Text = “first”;
node1.Brush = Brushes.Blue;
node1.Location = new PointF(100, 100);
// add to the document, not to the view
goView1.Document.Add(node1);

GoBasicNode node2 = new GoBasicNode();
node2.LabelSpot = GoObject.Middle;
node2.Text = “second”;
node2.Brush = Brushes.Magenta;
node2.Location = new PointF(200, 100);
goView1.Document.Add(node2);

// create a link between the two nodes’ ports
GoLink link = new GoLink();
link.ToArrow = true;
link.FromPort = node1.Port;
link.ToPort = node2.Port;
goView1.Document.Add(link);

Thanks for your help!

That codes adds 2 nodes and then links them.

What does your code for double click in the palette look like?
How do you decide which node in the view to connect to the newly created palette node? The current selection?

I just included that code, because I wanted to show that I get the same behavior even when I use a sample you provided.

Basically, I found some code that you provided to traverse the tree. I find the first node without a destination and add to that. I may have to tweak this later, but for now a linear traversal is fine.
Everything appears to work, but I have to click on the canvas for it to repaint and display my new node and link.
Here's the code...
protected void MyPalette_ObjectDoubleClicked(object sender, Northwoods.GoWeb.GoInputEventArgs e)
{ GoObject pObj = MyPalette.Selection.Primary;
GraphNode gn = pObj as GraphNode;
PointF pf = new PointF();
GraphNode node = new GraphNode(gn.Kind);
GraphNode lastnode = new GraphNode(gn.Kind);
GoLink link = new GoLink(); foreach (GoObject obj in this.MyView.Document) {
node = obj as GraphNode;
if (node != null && node.Destinations.Count == 0) {
astnode = node;
if = node.Location;
}
} pf.Y += 75;
pObj = MyView.Document.AddCopy(pObj, pf);
gn = pObj as DPSFlowCharter.GraphNode; link.FromPort = lastnode.BottomPort;
link.ToPort = gn.TopPort;
link.ToArrow = true;
MyView.Document.Add(link); MyView.Selection.OnGotFocus();
}
Thanks!

You should not be calling OnGotFocus().

If you want to select the new node, you should do something like:

MyView.Selection.Select(gn);

To make sure that node can be seen, you can also do:

MyView.ScrollRectangleToVisible(gn.Bounds);

You create “node” and “lastnode” but never add them to the document.

Does the node/link appear if you hide/show the window, or only if you click?

MyView.Selection.Select(gn) doesn’t appear to have any effect.

Jake, I'm not sure what you mean by hide/show the window. Do you mean minimize restore or select a different tab in the browser? I tried doing a MyView.Visible=false, and then a MyView.Visible=true, but it still requires a click to make the item appear.
"node" and "lastnode" are already on the document. I'm just using them to save the reference to the terminating node, so that I can create a link from that node to the new node.

Ah, sorry, Jake and I didn’t realize you were using GoDiagram Web.

So, it makes sense when GoView.NoPost is true that an interaction in one view (the GoPalette) does not cause an update of any other views (such your main GoView). Or, to put it another way, you wouldn’t have encountered this lack of update if you were doing a full postback (not that you want to do a full postback).

Basically you want to update the main GoView after each update of the GoPalette. You can accomplish this by executing this JavaScript code as part of the initialization of the client page, after both elements have been defined, such as at the end of the page:

[code][/code]
Of course, substitute the id’s as appropriate for your two controls.

Thank you very much. Works like a charm now.