Chinese (People's Republic of China)  English  Français


Supinfo-Projects.com
Supinfo Students' projects



Projects
  Last project
  Top projects
  All projects

118 User's online
3168 Projects


My Supinfo-Projects

   Login
   Create account


Synopsis

   6 Hits
   Visitors Score : 20
    (3 Voter)
   0 Comments

   Read the article

Evaluate this article

20
18
16
14
12
10
8
6
4
2
0


Comment this article

Author :

Email :

Your comment :



 
2006 - Pérennisation
Use and customize of host of forms of Visual Studio 2005 in your applications .net
[30 mn of reading - published 6/6/2006 8:37:04 PM - Target : Confirmé]

Author

wildarkWilliam GUIOUBLY
Student-Engineer Supinfo Paris
SUPINFO graduate year  2007

   Write to the person
   All projects of the same author
   Mini-CV of the author

6 The IToolboxService service

The IToolboxService service is an interface of the namespace System.Drawing.Design.It provides methods for creation and management of a toolbox (To consult the MSDN to know all its methods, properties and events). One recovers this service via the GetService method of the DesignSurface class, while passing the type System.D rawing.Design.IToolboxService in parameter. 

 

Let us examine somebody of these methods, properties and events:

- The CategoryNames property turns over the name of all the categories in Toolbox.

- The SelectedCategory property turns over or defines the name of the category selected in Toolbox.

- The AddCreator method: Add new a toolboxItem to Toolbox.

- The GetSelectedToolboxItem method: The element of Toolbox currently selected obtains. 

- The IsSupported Method: Indicate if the specified object can be used by the specified originator. 

- The IsToolboxItem method: A value obtains indicating if the specified object is an element of serialized Toolbox. 

- The Refresh Method: Bring up to date the state of the elements of Toolbox. 

- The SelectedToolboxItemUsed Method: Inform the IToolboxService service that the selected tool was used. 

- The SerializeToolboxItem Method: Turn over a serializable object representing the element of Toolbox specified to also use for the drag and drop. 

- The SetCursor Method: The cursor indicating defines that a elements of Toolbox currently selected. 

- The SetSelectedToolboxItem Method: Select the element of specified Toolbox. 

 

There is no IToolboxService service by defect in DesignSurface. We thus will create one of them to create a new ToolBoxService class of it in our project which inherits System.Drawing.Design.IToolboxService as follows:

class ToolBoxService : System.Drawing.Design.IToolboxService

{

    // ToolBox

    ListBox toolBox = null;

    // View of host

    Control view = null;

 

    // Define the toolbox

     public ListBox ToolBox { set { toolBox = value; } }

 

    // Define the view of host

    public Control View { set { view = value; } }

 

    // Not use

    public void AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator,

string format, System.ComponentModel.Design.IDesignerHost host)

    {

    }

 

    // Not use

    public void AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator,

string format)

    {

    }

 

    // Not use

    public void AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem,

string category, System.ComponentModel.Design.IDesignerHost host)

    {

    }

 

    // Not use

    public void AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, System.ComponentModel.Design.IDesignerHost host)

    {

    }

 

    // Add a ToolboxItem at the toolBox

    public void AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem,

string category)

    {

        AddToolboxItem(toolboxItem);

    }

 

    // Add a ToolboxItem at the toolBox

    public void AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)

    {

        toolBox.Items.Add(toolboxItem);

    }

 

    // Not use

    public System.Drawing.Design.CategoryNameCollection CategoryNames

    {

        get { return null; }

    }

 

    // Deserialize a toolboxitem necessary for the drag and drop

    public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(

object serializedObject, System.ComponentModel.Design.IDesignerHost host)

    {

        return DeserializeToolboxItem(serializedObject);

    }

 

    // Deserialize a toolboxitem necessary for the drag and drop

    public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(object serializedObject)

    {

        IDataObject dataObject = serializedObject as IDataObject;

       

        if (dataObject == null)

        {

            return null;

        }

 

        System.Drawing.Design.ToolboxItem t = (System.Drawing.Design.ToolboxItem)

dataObject.GetData(typeof(System.Drawing.Design.ToolboxItem));

        return t;

    }

 

    // Return the selected ToolboxItem

    public System.Drawing.Design.ToolboxItem GetSelectedToolboxItem(System.ComponentModel.Design.IDesignerHost host)

    {

        return GetSelectedToolboxItem();

    }

 

    // Return the selected ToolboxItem

    public System.Drawing.Design.ToolboxItem GetSelectedToolboxItem()

    {

        if(toolBox == null)

            return null;

 

        return (System.Drawing.Design.ToolboxItem)toolBox.SelectedItem;

    }

 

    // Return the list of selected ToolboxItems

    public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(string category, System.ComponentModel.Design.IDesignerHost host)

    {

        return GetToolboxItems();

    }

 

    // Return the list of selected ToolboxItems

    public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(string category)

    {

        return GetToolboxItems();

    }

 

    // Return the list of selected ToolboxItems

    public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(System.ComponentModel.Design.IDesignerHost host)

    {

        return GetToolboxItems();

    }

 

    // Return the list of selected ToolboxItems

    public System.Drawing.Design.ToolboxItemCollection GetToolboxItems()

    {

        if (toolBox == null)

            return null;

 

        System.Drawing.Design.ToolboxItem[] t = new System.Drawing.Design.ToolboxItem[toolBox.Items.Count];

        toolBox.Items.CopyTo(t, 0);

 

        return new System.Drawing.Design.ToolboxItemCollection(t);

    }

 

    // Not use

    public bool IsSupported(object serializedObject, System.Collections.ICollection filterAttributes)

    {

        return false;

    }

 

    // Not use

    public bool IsSupported(object serializedObject, System.ComponentModel.Design.IDesignerHost host)

    {

        return false;

    }

 

    // Not use

    public bool IsToolboxItem(object serializedObject, System.ComponentModel.Design.IDesignerHost host)

    {

        return false;

    }

 

    // Not use

    public bool IsToolboxItem(object serializedObject)

    {

        return false;

    }

 

    // Not use

    public void RemoveCreator(string format, System.ComponentModel.Design.IDesignerHost host)

    {

    }

 

    // Not use

    public void RemoveCreator(string format)

    {

    }

 

    // Remove a toolboxItem from toolbox

    public void RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category)

    {

        RemoveToolboxItem(toolboxItem);

    }

 

    // Remove a toolboxItem from toolbox

    public void RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)

    {

        if (toolBox == null)

            return;

 

        toolBox.SelectedItem = null;

        toolBox.Items.Remove(toolboxItem);

    }

 

    // Not use

    public string SelectedCategory

    {

        get

        {

             return null;

        }

        set

        {

 

        }

    }

 

    // Indicate that a toolboxItem was used, one puts the selection of the toolbox at null

    public void SelectedToolboxItemUsed()

    {

         if (toolBox == null)

             return;

 

        toolBox.SelectedItem = null;

    }

 

    // Serialize the toolboxItem necessary for the drag and drop

    public object SerializeToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)

    {

        DataObject dataObject = new DataObject();

        dataObject.SetData(typeof(System.Drawing.Design.ToolboxItem), toolboxItem);

        return dataObject;

    }

 

    // Indicate if a cursor is to use if a toolboxItem is selected

    public bool SetCursor()

    {

        if (view == null)

            return false;

 

        if (toolBox.SelectedItem != null)

        {

            view.Cursor = Cursors.Cross;

             return true;

        }

 

        return false;

    }

 

    // Define a toolboxItem to be selected on the toolBox

    public void SetSelectedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)

    {

        if (toolBox == null)

            return;

 

        toolBox.SelectedItem = toolboxItem;

    }

 

    // Refresh the toolbox

    public void Refresh()

    {

        toolBox.Refresh();

    }

}

Now one will associate our ToolBoxService class to our host by modifying the code of the Designer class: 

public class Designer : System.ComponentModel.Design.DesignSurface

{

    public Designer()

    {

        // Service of creation of name      

        ServiceContainer.AddService(

typeof(System.ComponentModel.Design.Serialization.

INameCreationService), new NameCreationService());

 

        // Service of management of the toolbox

         ServiceContainer.AddService(

typeof(System.Drawing.Design.IToolboxService), new ToolBoxService());   

    }

}

The ServiceContainer property of the DesignSurface class makes it possible to add and modify the list of the services associated with the originator.

 

And to finish one will modify the code of the form program:

private void Form1_Load(object sender, EventArgs e)

{

   

 

    // For the management of the drag and drop of the toolboxItems

    ((Control)designer.View).AllowDrop = true;

    ((Control)designer.View).DragDrop += new DragEventHandler(Form1_DragDrop);

 

     

 

     // Select the service IToolboxService

     ToolBoxService toolbox = (ToolBoxService) designer.GetService(typeof(System.Drawing.Design.IToolboxService));

 

    // Assocation the toolbox to the service

    toolbox.ToolBox = listBox1;

    // Assocation the view of host to the toolbox

    toolbox.View = panel1;

    // Add the toolboxItems at the toolbox (Button, Textbox et Listbox)

    toolbox.AddToolboxItem(new System.Drawing.Design.ToolboxItem(typeof(Button)));

     toolbox.AddToolboxItem(new System.Drawing.Design.ToolboxItem(typeof(TextBox)));

     toolbox.AddToolboxItem(new System.Drawing.Design.ToolboxItem(typeof(ListBox)));

}

 

// Release the ressources

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

    // Release the evenements on the service of IComponentChangeService    System.ComponentModel.Design.ISelectionService selectionService =

         (System.ComponentModel.Design.ISelectionService)

designer.GetService(typeof(System.ComponentModel.Design.ISelectionService));

 

    selectionService.SelectionChanged -= new EventHandler(selectionService_SelectionChanged);

 

    // Release the evenements on the service of selection

    ((Control)designer.View).DragDrop -= new DragEventHandler(Form1_DragDrop);

 

    // Delete the view of designer of panel

    panel1.Controls.Remove((Control)designer.View);

 

    // Release the designer to the form

    designer.Dispose();

}

 

// Management of the drag and drop of the toolboxItems

// Event MouseDown on the listBox

private void listBox1_MouseDown(object sender, MouseEventArgs e)

{

    if (listBox1.SelectedItem != null) {

        listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Copy | DragDropEffects.Move);

    }

}

 

// Management of the drag and drop of the toolboxItems

void Form1_DragDrop(object sender, DragEventArgs e)

{

    if (e.Data.GetDataPresent(typeof(System.Drawing.Design.ToolboxItem)))

    {

        System.Drawing.Design.ToolboxItem item = (System.Drawing.Design.ToolboxItem)listBox1.SelectedItem;

 

        System.ComponentModel.Design.IDesignerHost host =

        (System.ComponentModel.Design.IDesignerHost)

    designer.GetService(typeof(System.ComponentModel.Design.IDesignerHost));

                item.CreateComponents(host);

    }

}

To compile and launch the project and here what you obtain:

Exemple IToolBoxService.JPG

You can from now on add controls to your host and also make a drag and drop toolbox with the host.



Articles de la même catégorie

 Pages : Top


5 Hits
0 Comments
ID3 and C4.5 algorithms
[40 mn of reading - published 6/6/2006 7:48:47 PM - Target : Confirmé]

More


9 Hits
0 Comments
Generics with .NET
[30 mn of reading - published 6/6/2006 3:37:29 PM - Target : Confirmé]

More


5 Hits
0 Comments
Widget Development
[10 mn of reading - published 6/6/2006 3:03:12 PM - Target : Débutant]

More

Powered by Campus-Booster Technology
Conditions d'utilisation & Copyright | Respect de la vie privée
© Copyright 1965-2006 Supinfo Paris, Paris Academy of Computer Science
Supinfo, Ecole Supérieure d'Informatique et Paris Academy Of Computer Science are trade marks.
23, rue de Château LANDON - 75010 PARIS - Phone : +33 (0) 153359 700 Fax : +33 (0) 153359 701

Web site autided by :