如何将智能标记添加到我的.NET组件?

时间:2009-08-15 13:46:43

标签: c# .net vb.net visual-studio components

请看下面的图片,它显示了DataGridView的智能标记。

DataGridView Smart Tags http://img199.imageshack.us/img199/5871/post517531249536112.jpg

现在我正在创建一个新组件,我希望它支持在智能标记中显示一些属性。如何将属性添加到智能标记?

2 个答案:

答案 0 :(得分:5)

我使用http://msdn.microsoft.com/en-us/library/default.aspx?q=smart+tag+windows+forms+designer

结果,我找到了Walkthrough: Adding Smart Tags to a Windows Forms Component

进行相同搜索的任何人都会找到相同的文章。


更新:该链接不再有效。我刚尝试搜索"smart tag windows forms designer",发现"Walkthrough: Adding Smart Tags to a Windows Forms Component"是第一次搜索。 Google中的相同搜索显示"How to: Attach Smart Tags to a Windows Forms Component"为第一次点击,但显示与第二次点击相同的漫游。

答案 1 :(得分:2)

您可以使用以下代码在自定义控件中查看示例代码。

您可以从www.windowsclient.com获取有关该视频的视频。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
namespace MakeSmartTag
{
    public enum Languages
    {
        English,
        Arabic,
        Japanese
    }

    [Designer(typeof(UserControlDesigner))]
    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
        }

        private Languages _language;

        public Languages Language
        {
            get { return _language; }
            set
            {
                switch (value)
                {
                    case Languages.Arabic:
                        {
                            label1.Text = "مرحباً";
                            _language = value;
                        }
                        break;
                    case Languages.English:
                        {
                            label1.Text = "Hello";
                            _language = value;
                        }
                        break;
                    case Languages.Japanese:
                        {
                            label1.Text = "Conechoa";
                            _language = value;
                        }
                        break;
                }
            }
        }
    }

    public class UserControlDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        private DesignerActionListCollection lists;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (lists == null)
                {

                    lists = new DesignerActionListCollection();
                    lists.Add(new UserControlActioonList(this.Component));
                }
                return lists;
            }
        }
    }

    public class UserControlActioonList : DesignerActionList
    {
        private UserControl2 myUserControl;
        private DesignerActionUIService designerActionSvc = null;

        public UserControlActioonList(IComponent component)
            : base(component)
        {
            this.myUserControl = (UserControl2)component;

            this.designerActionSvc =
              (DesignerActionUIService)GetService(typeof(DesignerActionUIService));
        }

        private PropertyDescriptor GetPropertyByName(string propName)
        {
            PropertyDescriptor prop = default(PropertyDescriptor);
            prop = TypeDescriptor.GetProperties(myUserControl)[propName];
            if (prop == null)
            {
                throw new ArgumentException("Invalid Property", propName);
            }
            else
            {
                return prop;
            }
        }

        public override System.ComponentModel.Design.DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection item = new DesignerActionItemCollection();
            item.Add(new DesignerActionHeaderItem(
                       "المظهر"));
            item.Add(new DesignerActionPropertyItem(
                       "BackColor", "لون الخلفية", "Appearance", "Set background Color of the control"));
            item.Add(new DesignerActionHeaderItem("تحديد اللغة"));
            item.Add(new DesignerActionPropertyItem(
                       "Language", "اللغة", "Functions", "Set the language of the control"));
            return item;
        }

        public Color BackColor
        {
            get { return this.myUserControl.BackColor; }
            set { GetPropertyByName("BackColor").SetValue(myUserControl, value); }
        }

        public Languages Language
        {
            get { return this.myUserControl.Language; }
            set { GetPropertyByName("Language").SetValue(myUserControl, value); }
        }
    }
}