Windows Develop Bookmark and Share   
 index > Windows Forms Designer > SnapToGrid and GridSize in custom form designer
 

SnapToGrid and GridSize in custom form designer

Hi All,

I'm playing with the code provided with the following MSDN Magazine article:
The Perfect Host
Create And Host Custom Designers With The .NET Framework 2.0
http://msdn.microsoft.com/en-us/magazine/cc163634.aspx

I want to change the default GridSize and snap controls to grid.

In MainShell.cs, I added the DesignerOptionService, so the CustomInitialize() method now looks like this:

privatevoidCustomInitialize()
{
_hostSurfaceManager=newHostSurfaceManager();
_hostSurfaceManager.AddService(typeof(IToolboxService),this.toolbox1);
_hostSurfaceManager.AddService(typeof(ToolWindows.SolutionExplorer),this.solutionExplorer1);
_hostSurfaceManager.AddService(typeof(ToolWindows.OutputWindow),this.OutputWindow);
_hostSurfaceManager.AddService(typeof(System.Windows.Forms.PropertyGrid),this.propertyGrid1);
//Mychangesstart:
DesignerOptionServicedos=newSystem.Windows.Forms.Design.WindowsFormsDesignerOptionService();
dos.Options.Properties["UseSnapLines"].SetValue(this,(object)false);
dos.Options.Properties["ShowGrid"].SetValue(this,(object)true);
dos.Options.Properties["SnapToGrid"].SetValue(this,(object)true);
dos.Options.Properties["GridSize"].SetValue(this,newSize(100,20));
_hostSurfaceManager.AddService(typeof(IDesignerOptionService),dos);
//endMychanges
codeDomDesignerLoaderMenuItem_Click(null,null);
this.tabControl1.SelectedIndexChanged+=newEventHandler(tabControl1_SelectedIndexChanged);
//this.noLoaderMenuItem_Click(null,null);
this.basicDesignerLoaderMenuItem_Click(null,null);
}



This does not work -- I still see no grid but snap lines instead.
Please -- what am I missing?

Thanks!
Missis Polly  Saturday, December 27, 2008 11:44 PM
I believe it is:
dos.Options["WindowsFormsDesigner"].Properties["ShowGrid"].SetValue(this,(object)true);
dos.Options["WindowsFormsDesigner"].Properties["SnapToGrid"].SetValue(this,(object)true);
dos.Options["WindowsFormsDesigner"].Properties["GridSize"].SetValue(this,newSize(100,20));


-- Don't forget to close the thread by marking the correct post(s) as ANSWERED!
Marcel Nita  Sunday, December 28, 2008 11:38 AM
Thanks for your suggestion Marcel

dos.Options["WindowsFormsDesigner"] was turning into null, so I implemented a simple DesignerOptionService like this:

usingSystem;
usingSystem.Windows.Forms.Design;
namespaceShell
{
classMyDesignerOptionService:WindowsFormsDesignerOptionService
{
publicMyDesignerOptionService():base()
{
}
protectedoverridevoidPopulateOptionCollection(DesignerOptionCollectionoptions)
{
if(options.Parent==null)
{
DesignerOptionCollectionwfd=this.CreateOptionCollection(options,"WindowsFormsDesigner",null);
DesignerOptionsdesignerOptions=newDesignerOptions();
designerOptions.GridSize=newSystem.Drawing.Size(100,20);
designerOptions.SnapToGrid=true;
designerOptions.UseSnapLines=false;
designerOptions.ShowGrid=true;
designerOptions.UseSmartTags=true;
DesignerOptionCollectionwfdg=this.CreateOptionCollection(wfd,"General",designerOptions);
}
}
}
}


So the CustomInitialize() became looking like this:

privatevoidCustomInitialize()
{
_hostSurfaceManager=newHostSurfaceManager();
_hostSurfaceManager.AddService(typeof(IToolboxService),this.toolbox1);
_hostSurfaceManager.AddService(typeof(ToolWindows.SolutionExplorer),this.solutionExplorer1);
_hostSurfaceManager.AddService(typeof(ToolWindows.OutputWindow),this.OutputWindow);
_hostSurfaceManager.AddService(typeof(System.Windows.Forms.PropertyGrid),this.propertyGrid1);
//Mychangesstart:
DesignerOptionServicedos=newShell.MyDesignerOptionService();
_hostSurfaceManager.AddService(typeof(IDesignerOptionService),dos);
//endMychanges
codeDomDesignerLoaderMenuItem_Click(null,null);
this.tabControl1.SelectedIndexChanged+=newEventHandler(tabControl1_SelectedIndexChanged);
//this.noLoaderMenuItem_Click(null,null);
this.basicDesignerLoaderMenuItem_Click(null,null);
}

No effect... No grid visible and no snapping to grid...
Any idea how to make it work?

Thanks!
Happy New Year?



Missis Polly  Thursday, January 01, 2009 5:28 AM

Martin,

Thanks for the kind reply. I did implement the steps as you described. The below is my DesignerOptions and DesignerOptionService implementation (benefited from this forum)

using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;

namespace MyDesignerHost
{
public class AtlasOptionServiceProvider : IServiceProvider
{
private DesignerOptionService dos = new OptionService();

#region IServiceProvider Members

public object GetService(Type serviceType)
{
if (serviceType == typeof(DesignerOptionService))
{
return dos;
}
return null;
}

#endregion

private class OptionService : DesignerOptionService
{
protected override void PopulateOptionCollection(DesignerOptionCollection options)
{
if (options.Parent == null)
{
base.CreateOptionCollection(options, "DesignerOptions", new Options());
}
}
}

private class Options : DesignerOptions
{
public override bool UseSmartTags
{
get
{
return true;
}
}

public override bool UseSnapLines
{
get
{
return false;
}
}

public override bool ShowGrid
{
get
{
return true;
}
}
}

}
}

Then I added the service in the designerhost implementation:


AtlasOptionServiceProvider aosp = new AtlasOptionServiceProvider();
designerOptionService = aosp.GetService(typeof(DesignerOptionService)) as DesignerOptionService;
serviceContainer.AddService(typeof(DesignerOptionService), designerOptionService);

The smartTag feature still cannot be activated. However, the snapline and showgrid feature works as expected. I have no clue at this point what went wrong. Could you please shed more light on this, preferably with some code demo. Thank you!

Alan


source: http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/e08ef80a-75b8-4229-b53c-2f16ff07dbd3
Campa  Thursday, September 10, 2009 9:50 AM

You can use google to search for other answers

Custom Search

More Threads

• Property Grids
• DataGrid Question
• Display a string on an AVI movie
• Highlight a column in tableLayoutPanel after a button click
• MenuStrip and ToolStrip Items disappear in design and debug modes
• UserControl, ParentControlDesigner and dragging in designer mode
• TypeConverter - Exception - Unable to cast type x to type x
• Can Someone Show Me An Example of How Can I Use Listview to Show contents of a directory
• scroll to a point/rectangle in a usercontrol programmatically in c#
• Problem after hiding an owned form