I have a goView where I draw simplenodes with snapping to grid enabled. I want the simplenodes to snap to the grid in the center of the icon instead of the top-left corner.
I have overridden the Location property of the simplenode to achive this when I create the node and add it to the view:
public override PointF Location
{
get
{
return base.Location;
}
set
{
base.Location = value;
SetSpotLocation(Middle,value);
}
}
But when I move the nodes afterwards, the spotlocation seem to have moved back to the top-left corner, because nodes now snap to the grid in the top-left corner.
How do I keep the spotlocation in the center of the icons of the simplenodes when I move them after they have been added to the goView ?
Regards
Stig Nielsson
That’s very odd, because the behavior I believe you want is actually the default behavior. I just tried running Demo1 to make sure–I used the PropertyGrid to change the view’s values for GridStyle and GridSnapDrag.
So if you are using GoSimpleNodes, the centers of the icons should snap to the grid points, without having to override any methods.
If you do want to change the “Location” of an object, you need to do so consistently between the getter and the setter:
public override PointF Location {
get { return this.Icon.Center; }
set { this.Icon.Center = value; }
}
This is actually what GoSimpleNode implements, except that the setter is implemented more efficiently, to avoid unnecessary calls to LayoutChildren:
set {
SizeF off = GoTool.SubtractPoints(this.SelectionObject.Center, this.Position);
this.Position = new PointF(value.X - off.Width, value.Y - off.Height);
}
(The SelectionObject is the same as the GoSimpleNode.Icon)
Yes, you are right. I must have had something cluttering up my code and understanding of what was happening. Thanks for the explanation and sorry for wasting your time.
Regards
Stig Nielsson
Walter,
[code]GoSelection sel = new GoSelection(null);
Nothing seems to happen when I implement this code.
And goView1.GridSnapStyle isn’t None, right?
Walter,
What’s the value of your GoView.GridSnapStyle?
I don’t have a property called GridSnapStyle, just GridStyle and GridSnapDrag.
Oops, my typo – I meant: GoView.GridSnapDrag and GoView.GridOrigin. And GoView.GridStyle and .GridCellSize too, I suppose.
I have GridSnapDrag and GridStyle set. Do I need to also set GridCellSize and GridOrigin? If so, what do you recommend I set the values to?