IToolboxService and the DesignSurface class
I am trying to use the DesignSurface in VS 2k5 Beta2 and I'm a bit confused when it comes to the interaction between it and the IToolboxService.
I have an implementation of the IToolboxService and I have added it to the DesignerHost. My toolbox service uses a treeview to show the controls available and in each category (root nodes) I have a selection pointer node and various other windows forms control nodes.
When I drag say a button ToolboxItem on to the surface I get a button. I then react to the SelectedToolboxItemUsed method on IToolboxService to change the selected node in the tree to the selection pointer.
If I then click on the design surface my implementation of GetSelectedToolboxItem is called. Can anyone tell me what the designer framework uses the returned ToolboxItem for? It seems to me that the designer is adding new controls to the surface depending on the return value from this method, but not always!!
Just on the off chance this sounds familiar to someone, my designer has a problem in that as soon as I have added say, a button, to the surface I cannot seem to select any other control on the surface.
Regards
Graham
[1169 byte] By [
Gravy] at [2007-12-16]
Ok, so I have found a better / easier way of doing this. At the beginning of all this I started off with the following article
http://msdn.microsoft.com/msdnmag/issues/04/12/CustomFormsDesigner/default.aspx which described how to do a custom forms designer for .NET 1.1. From there I went on to changing the sample to include the DesignSurface in .NET 2.0. What I didn't realise until recently is that .NET 2.0 also provides a default implementation of the ToolboxService which was where I was having a little trouble. Changing over to this implementation (well actually I mean deriving my own class from this abstract class) all my troubles have gone away. For now anyway :)
Thanks
Graham
Andrey,
I didn't need to specifally get a Toolbox item. My ToolboxService derived class contained an array of toolbox items, in their appropriate toolbox item container. This was populated in the ctor as in:
uiToolboxItems.Add(new ToolboxItemContainer(new ToolboxItem(typeof(Label))));
uiToolboxItems.Add(new ToolboxItemContainer(new ToolboxItem(typeof(Button))));
uiToolboxItems.Add(new ToolboxItemContainer(new ToolboxItem(typeof(TextBox))));
uiToolboxItems.Add(new ToolboxItemContainer(new ToolboxItem(typeof(TabControl))));
And then when I needed to populate my tree view of toolbox item controls I used:
ToolboxItemCollection tools = toolboxService.GetToolboxItems(categoryName);
foreach (ToolboxItem toolItem in tools)
{
...
<Create tree node and add toolItem reference to the Tag
...
}
And then, during a mouse down handler on the tree view I initiated the drag operation with the toolbox item like...
ToolboxItem item = clickedNode.Tag as ToolboxItem;
// if its not the pointer tool
if (string.Compare(item.DisplayName, "Pointer", true) != 0)
{
// serialize the item and send it over
DataObject dataObj = toolboxService.SerializeToolboxItem(item) as DataObject;
DoDragDrop(dataObj, DragDropEffects.Copy);
}
Hope this helps.
Graham