Can I center the diagram

Send email to GoDiagram at our domain and I'll reply with a very small sample program that displays a couple of GoIconicNodes with a link between them, shows a line grid in the background, and handles a double click in the background to center the diagram using the code from the start of this conversation.

The sample seems to work fine for me and I see no loss in image quality when the diagram is centered. I'd be interested to see if this simple sample works correctly for you. Assuming that it does, we'll need to figure out what additional things in your project might be causing the problem.

So far I tried the following code:

RectangleF c = methodBuilderView1.ComputeDocumentBounds();
methodBuilderView1.Document.TopLeft = new PointF(methodBuilderView1.Width / 2 - c.Width / 2,0);

which should do what I need but the diagram is still located at the upper left corner of the view.

Do I need some property of the document to be set or something else?

Thanks

Susan

First, if I understand you correctly, I think you're trying to scroll the GoView so that a given position within the GoDocument is at the top left of the GoView. To do this you want to be setting the GoView DocPosition property.
The next issue is that the GoView LimitDocPosition method is going to try to limit the value you set for DocPosition so that it is a postion within the current document. In your case I think you'll be scrolling the document horizontally to the right so that the x position of the left side of the GoView will actually be a negative value, and to do that you'd need to extend the document. So it's likely nothing would happen when you changed the DocPosition. To fix this you can change the GoViewSheetStyle so that it isn't None. Then LimitDocPosition won't set any limits on the value of DocPosition.
Next, I think the value you're computing for the x coordinate is in the wrong direction. I think you want to use the negative of that.
So, try something like the following:
methodBuilderView1.SheetStyle = GoViewSheetStyle.Sheet;
methodBuilderView1.DocPosition = new PointF(c.Width/2 - methodBuilderView1.Width/2, 0);

That’s not exactly what I am trying to do.

Is there a way I can send you a screen capture of what the picture looks like and also of what I would like it to be.
I think that woulud be helpful in trying to explain.
Thanks Very much in advance.
Susan

Sure, send the screen capture and what you’re looking for to GoDiagram at our domain.

Good Morning,
I sent the two images to the e-mail address above.

You can post the reply here.
Thanks very much.
Susan

I just want to add one more thing.
The code which Walter suggested does center the graph but it centers it horizontally AND vertically and I only needed it to be centerd horizontally, so no matter how long the document is the graph needs to always start at the top.
Thanks
Susan

suzi,

I was having similar problems centering my tree. Make sure that if you create a view and add it to your form programatically that you actually set it's size (i.e. width and height) otherwise none of the advice given will work. To make things easier, I would add the actual controls provided by Northwoods to a custom tab in your Visual Studio 2005 toolbox and then drag the GoView onto the form. This will autogenerate everything for you.
Once that is done, you can then use the dock property and set it to something like "fill". Try that and let me know if it solves your problem.

Thanks very much for the reply,

I am already doing all the suggestions you have - I did add it to my VS toolbar and then dragged it to my form.
Here is my view-related code:
[code]this.methodBuilderView1.Dock = DockStyle.Fill; //Make it fill the entire object
this.methodBuilderView1.DocPosition = new PointF(0F, 0F);
//this line keeps the diagram at the upper left corner of the document
methodBuilderView1.Document.Bounds = methodBuilderView1.Document.ComputeBounds();
[/code]
then I thought that maybe the DocPosition is messing me up so I changes it to:
[code]this.methodBuilderView1.Dock = DockStyle.Fill; //Make it fill the entire object
methodBuilderView1.SheetStyle = GoViewSheetStyle.WholeSheet;
RectangleF b = methodBuilderView1.Document.ComputeBounds();
methodBuilderView1.Document.Bounds = b;
this.methodBuilderView1.DocPosition = new PointF(b.X + b.Width / 2, 0F);[/code]
but I am having the same result - the tree appears in the upper left corner while I need it to appear in the upper center at location.
Thanks
Susan

Try this …

myView.SheetStyle = GoViewSheetStyle.Sheet; RectangleF b = myView.Document.ComputeBounds(); myView.Document.Bounds = b; PointF pf = GetDocExtentCenter(b); myView.DocExtentCenter = new PointF(b.X + b.Width / 2, b.Y + b.Height / 2);

Or try this:

methodBuilderView1.SheetStyle = GoViewSheetStyle.Sheet;
RectangleF docBounds = methodBuilderView1.Document.ComputeBounds();
PointF newTopLeft = new PointF(docBounds.Width/2 - methodBuilderView1.Width/2, 0);
methodBuilderView1.DocPosition = newTopLeft;

Scott,

Do you mind checking out my last post over in this thread?
http://www.nwoods.com/forum/forum_posts.asp?TID=2142&PN=2
Thanks!

Hello Scott,

Thanks for the reply.
I think I am almost there.
When I implement the code u suggested I still see the tree in the upper left corner but I see the scrollBars reflecting it's actual position.
When I click on the document however I see the tree exactly as I need it to be so I am trying to find out how can I simulate the same thing in code.
I tried registering the click event in my constructor and then calling it from the place where I have the rest of the centering code but it did not do the trick.
So I am wondering what is happening when I click on the view that makes the tree center?
Could it be that it gets the focus and that is why it centers? I am not sure if that is the case because if I click on the scrollbar itself it does not do it (and I think clicking on the scrollbar should move the focus to that view but I might be wrong) .
Thanks again for the help.
Susan

Suzi,

You are running into the same exact issues I ran into the other week. I tried to see if there was a way to register click events on the scrollbar and the weird thing is that even though the GoDocument/GoView VerticalScrollbar and HorizontalScrollbar both inherit from Windows.Forms.Scrollbar they do not handle the click event. I found out that in the end my problem was that nowhere in my Visual Studio designer generated code were any values set for the size of my GoView.
Try this:
Make sure that you are not trying to load your GoDocument into your GoView before the GoView has been initialized in the constructor. In fact, I would create a totally separate method (maybe call it Setup() or something) that gets called from your form's class constructor.

Thanks for the suggestion.

What do u mean when u say to "Load the GoDocument"?
It comes as part of the view.
I use the InitializeComponents() function in the constructor to initialize the GoVIew.
I have the size being set up there.
Let me poke around a bit more........

Actually let me show u the entire function.

[code]private void Layout_Graph() {
_mainLayout.Network = null; // GoLayout has to have the residual memory cleared _mainLayout.LayerSpacing = 10; _mainLayout.DirectionOption = GoLayoutDirection.Down; _mainLayout.PackOption = GoLayoutLayeredDigraphPack.Median; _mainLayout.PerformLayout(); _mainLayout.LayoutNodesAndLinks(); //this line keeps the diagram at the upper left corner of the document
methodBuilderView1.Document.Bounds = methodBuilderView1.Document.ComputeBounds();
methodBuilderView1.SheetStyle = GoViewSheetStyle.WholeSheet; RectangleF b = methodBuilderView1.Document.ComputeBounds(); methodBuilderView1.DocPosition = new PointF(b.Width / 2 - methodBuilderView1.Width / 2, 0);

// methodBuilderView1.DocExtentCenter = new PointF(b.X + b.Width / 2, b.Y + b.Height / 2);

}[/code]
I call this function after I am done adding the goObjects to the
document
[code] this.methodBuilderView1.Document.Add(obj);[/code]
and I am also calling it from the MouseCaptureChanged event
[code]void methodBuilderView1_MouseCaptureChanged(object sender, EventArgs e) {
Layout_Graph();
}[/code]
I do that because I want to re-layout the tree if a user clicks on a subgraph and it expands.
So the funny thing is that when I click on the view then the MouseCaptureChanged event fires up and executes the Layout_Graph code again (after it has been called once from the first place.)
If I call the Layout_Graph function twice from the first place though nothing happens....
Strange.......

Try changing
GoViewSheetStyle.WholeSheet to GoViewSheetStyle.Sheet

...