How to Create such a Task Panel?
Asked Answered
D

2

5

In Visual Studio 2008,
If you create a Form and put a Control on it,
you can edit the control's properties via the Properties window.

Some controls enable changing their properties in another way,
in addition to the Properties window.

It looks like this:

It seems that all controls that has this pane, has it in the same style,
meaning it's something that is provided by Visual Studio,
and the maker of the control just chooses the items to include inside,
like Fields, and Clickable Links that open some windows.

So my question:
What is the name of this pane control,
and how do I create one?

Darya answered 11/6, 2018 at 15:23 Comment(4)
I think this might explain: msdn.microsoft.com/en-us/library/ms171840.aspxArrear
You need to create a custom Designer for your control and override DesignerActionListCollectionLogarithmic
they are provided by custom Designer implementation, which has some design actions. msdn.microsoft.com/en-us/library/ms171567.aspxToddy
Thank you all. The correct name is DesignerActionList class. I am now reading about it..Darya
L
6

That menu is called Smart Tags or Designer Actions and you can add smart tag to your control. To do so, you need to create a custom Designer for your control and in the designer, override its ActionLists property.

Example

Let's say we have created a control having some properties, and we want to show the following properties of out control in smart tags window:

public Color SomeColorProperty { get; set; }
public string[] Items { get; set; }

And the expected result for us is:

enter image description here

MyControl

Here we decorate the control with Designer attribute to register the custom designer:

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;

[Designer(typeof(MyControlDesigner))]
public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }
    void InitializeComponent() { }
    public Color SomeColorProperty { get; set; }
    public string[] Items { get; set; }
}

MyControlDesigner

Here we override ActionLists and return a new DesignerActionListCollection containing the action list items which we need:

public class MyControlDesigner : ControlDesigner
{
    private DesignerActionListCollection actionList;
    public override DesignerActionListCollection ActionLists
    {
        get
        {
            if (actionList == null)
                actionList = new DesignerActionListCollection(new[] {
                    new MyControlActionList(this) });
            return actionList;
        }
    }
}

MyControlActionList

Here we create properties which get/set out control properties. Also we create methods which are responsible to show custom editor for some properties or do some actions. Then return a list of action items by overriding GetSortedActionItems:

public class MyControlActionList : DesignerActionList
{
    ControlDesigner designer;
    MyControl control;
    public MyControlActionList(ControlDesigner designer) : base(designer.Component)
    {
        this.designer = designer;
        control = (MyControl)designer.Control;
    }
    public Color SomeColorProperty
    {
        get { return control.SomeColorProperty;  }
        set {
            TypeDescriptor.GetProperties(
                (object)this.Component)["SomeColorProperty"]
                .SetValue((object)this.Component, (object)value);
        }
    }
    public void EditItems()
    {
        var editorServiceContext = typeof(ControlDesigner).Assembly.GetTypes()
            .Where(x => x.Name == "EditorServiceContext").FirstOrDefault();
        var editValue = editorServiceContext.GetMethod("EditValue",
            System.Reflection.BindingFlags.Static |
            System.Reflection.BindingFlags.Public);
        editValue.Invoke(null, new object[] { designer, this.Component, "Items" });
    }

    public override DesignerActionItemCollection GetSortedActionItems()
    {
        return new DesignerActionItemCollection() {
            new DesignerActionMethodItem(this, "EditItems", "Edit Items",  true),
            new DesignerActionPropertyItem("SomeColorProperty", "Some Color"),
        };
    }
}

For more information about this topic, take a look at this MSDN Walkthrough.

Download Example

You can download a working example from the following repository:

Logarithmic answered 11/6, 2018 at 16:7 Comment(0)
G
0

This is called 'DesignerActionList' or SmartTag. Smart tags are menu-like user interface (UI) elements that supply commonly used design-time options.

Step:

You must add a reference to the design-time assembly, System.Design.dll

Create DesignerActionList class and get the reference to control in the constructor.

public class MyControlTasks : System.ComponentModel.Design.DesignerActionList 
{
  private MyControl myControl;

  private DesignerActionUIService designerActionUISvc = null;

  public MyControlTasks( IComponent component ) : base(component) 
  {
    this.myControl = component as MyControl;
    this.designerActionUISvc =
        GetService(typeof(DesignerActionUIService))
        as DesignerActionUIService;
  }
}

Add methods and properties that you want to associate to smart-tag items

Create base designer for the control

public interface IDesigner {
   void Dispose();
   void Initialize(IComponent component);
   IComponent Component {
        get;
   }
}

Return a new instance of the MyControlTasks class that you created earlier.

public override DesignerActionListCollection ActionLists
{
    get
    {   
        var actionLists = new DesignerActionListCollection();
        actionLists.Add(new MyControlTasks(this.Component));
        return actionLists;
    }
}
Glyconeogenesis answered 11/6, 2018 at 15:55 Comment(1)
Thank you Nilay. Why is IDesigner necessary here? In all the examples that I see now (from Google), they do not need IDesigner.. For example here: codeproject.com/Articles/17125/…Darya

© 2022 - 2024 — McMap. All rights reserved.