我自定义了一个c#控件,其中一个属性的取值是固定的几个值,我希望开发人员将这个控件放入窗体,修改这个属性时用ComboBox风格进行选择,而不是直接输入(这样有可能会输错),就象listBox的BorderStyle 属性那样.请指教
最好能给个小例子

解决方案 »

  1.   

    private void button1_Click(object sender, EventArgs e)
            {
                if (comboBox1.SelectedIndex==0 )            linkLabel1.BorderStyle = BorderStyle.FixedSingle;
                else if (comboBox1.SelectedIndex==1 )
                    linkLabel1.BorderStyle=BorderStyle.Fixed3D;
                else
                    linkLabel1.BorderStyle=BorderStyle.None;
                
                //linkLabel1.Update;
            }
      

  2.   

    其中一个属性的取值是固定的几个值=======定义一个枚举类型,来包含这几个值,
    修改这个属性时用ComboBox风格进行选择,而不是直接输入(这样有可能会输错),就象listBox的BorderStyle 属性那样.=====某个属性类型是枚举类型之后, VS Designer 自动支持,下来选择看看 BorderStyle 就是一个枚举类型
      

  3.   

    定义一个枚举类型,再根据属性获得的值重绘控件
    例如在你的自定义了控件的代码中加入下面代码,其他开发人员将这个控件放入窗体后,就要以在属性栏找到ComboBoxStyle属性,然后就可以选“平面”、“立体”两项了public enum eComboBoxStyle
    {
        平面,
        立体
    }private eComboBoxStyle comboBoxStyle;/// <summary>
    /// ComboBox样式
    /// </summary>
    public eComboBoxStyle ComboBoxStyle
    {
        get { return comboBoxStyle; }
        set 
        { 
            comboBoxStyle = value;
            switch (comboBoxStyle)
            {
                case eComboBoxStyle.平面 :
                    //写代码重绘控件
                    break;
                case eComboBoxStyle.立体 :
                    //写代码重绘控件
                    break;
            }
        }
    }
      

  4.   

    如何将属性加在控制面板中,我找到一个例子.[CategoryAttribute("自定义的复杂类型设置(包括自定义类型转换器)"),  TypeConverterAttribute(typeof(PropertyGridApp.FileNameConverter)),  ReadOnlyAttribute(false)] 
    我在网上找到一个例子可以编绎的出错, 提示 PropertyGridApp 没有找到类型.
    namespace StyleOcx
    {
    /// <summary>
    /// FileNameConverter 的摘要说明。
    /// </summary>
    public class FileNameConverter:System.ComponentModel.StringConverter
    {
    public FileNameConverter()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)  {  return true;  } 
    public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)  {  return new StandardValuesCollection(new string[]{"File1.bat","File2.exe","File3.dll"});  } 
    public override bool GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext context)  {  return true;  } 
    }
    }
      

  5.   

    1.修改这个属性时用ComboBox风格进行选择,而不是直接输入
    用枚舉,樓上的說得很清楚了,不明白,可以查一下資料
    2.如何將屬性增加到控製面板中
    /// <summary>
    /// 當前操作DataTable對象
    /// </summary>
    [DescriptionAttribute("當前操作DataTable對象"),CategoryAttribute("擴展屬性"),DefaultValueAttribute(null)]
    public DataTable myDataTable
    {
    get{return _myDataTable;}
    }