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

6

1
这是一个很大的话题。你的组件现在有任何设计师支持吗? - John Saunders
谢谢,为了简化问题,让我们假设我有一个数字文本框,并且我想将启用/禁用数字属性添加到智能标记中。 - Wael Dalloul
1
谢谢你,但那并没有回答我的问题。你的组件目前是否有设计师支持? - John Saunders
2个回答

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

任何做同样搜索的人都会找到同样的文章。


更新:该链接已失效。我刚试了一下搜索"智能标记 Windows 窗体设计器", 并在第一项搜索结果中找到了"演练:向 Windows 窗体组件添加智能标记"。在Google中进行相同的搜索,第一项搜索结果显示"如何:将智能标记附加到 Windows 窗体组件", 但第二项搜索结果显示相同的演练。

8
好的,我会尽力将下面的内容翻译成通俗易懂的中文,但不改变原意。是的。从提问者的角度考虑,花费30秒将问题放在数千个专家面前比花费2分钟可能或可能不会得到任何帮助要有更多的价值。这就是我们在这里的原因。粗鲁的回答会减少它本来可以提供的帮助。 - Rex M
4
它为什么没用?这是解决方案。应该有人在Stack Overflow上重复解决方案吗?我认为假设提问者能够进行搜索并向他展示我使用的搜索词,不会是居高临下。如果这是我认为他无法独自完成的复杂搜索,那会是居高临下的行为。 - John Saunders
3
@Rex:那是一种错误的经济方式,会导致开发者社区分化。将会有那些懂得和那些需要询问的人。这不健康。顺便注意一下,这并不需要专家花费两分钟进行搜索!如果发帖人不知道MSDN搜索,我发布链接告诉了他。如果他无法确定使用哪些搜索术语(也许他没有尝试“设计师”),我则向他展示了应该使用哪些搜索术语。我试图使他成为我的“同等”,而不是继续优越于他! - John Saunders
3
所以您的意思是,SO上每个问题的答案都应该是“去谷歌搜索并输入您的问题”?这不是这个网站的宗旨。而且,在这个社区中说“我相信你有能力…”非常无礼,并且在我看来带有轻蔑的味道。 - Mark Carpenter
9
@大家:如果你认为我在意票数,那么你就误解了我的想法。我只是想知道投票反对的原因而已。有时候,重要的是要站稳脚跟,不再朝错误的方向走。如果我们“鼓励”开发者到这里来请求专家帮他们完成所有事情,那么他们永远不会成为专家。这对他们和我们都不利,我还没有准备停止影响这种情况。 - John Saunders
显示剩余10条评论

3
您可以使用以下代码在自定义控件中查看示例标记。
您可以从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); }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接