c#winfrom PropertyGrid 运行时变更属性项

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PropertyGridCodeDemoByDynamic
{
    public partial class Form1 : Form
    {
        CustomObjectType obj = null;
        public Form1()
        {
            InitializeComponent();
            obj = new CustomObjectType
            {
                Name = "Foo",
                Properties =
                {
                    new CustomProperty { Name = "Bar", Type = typeof(int), Desc = "I'm a bar", DefaultValue=0},
                    new CustomProperty { Name = "EnumType", Type = typeof(EnumType), Desc = "choose the enumType", DefaultValue= EnumType.Age},
                    new CustomProperty { Name = "When", Type = typeof(DateTime), Desc = "When it happened",DefaultValue=System.DateTime.Now},
                }
            };
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = obj;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (obj != null)
                obj.Properties.Add(new CustomProperty() { Name = "Add" + (obj.Properties.Count-2), Type = typeof(string), Desc = "I'm a bar", DefaultValue = "" });
            this.propertyGrid1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (obj != null)
            {
                int j = obj.Properties.Count - 3;
                int i = obj.Properties.FindIndex(p => p.Name.Equals("Add" + j, StringComparison.OrdinalIgnoreCase));
                if (i < 0)
                    return;
                obj.Properties.RemoveAt(i);
            }
            this.propertyGrid1.Refresh();
        }
    }

    [TypeConverter(typeof(CustomObjectType.CustomObjectConverter))]
    public class CustomObjectType
    {
        [Category("Standard")]
        public string Name { get; set; }

        [Category("Standard")]
        public string Label { get; set; }
        [Category("Standard")]
        public string Description { get; set; }
        private readonly List<CustomProperty> props = new List<CustomProperty>();
        [Browsable(false)]
        public List<CustomProperty> Properties { get { return props; } }

        private Dictionary<string, object> values = new Dictionary<string, object>();

        public object this[string name]
        {
            get
            {
                object val;
                values.TryGetValue(name, out val); return val;
            }
            set
            {
                values[name] = value;
            }
        }

        private class CustomObjectConverter : ExpandableObjectConverter
        {
            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                var stdProps = base.GetProperties(context, value, attributes);
                CustomObjectType obj = value as CustomObjectType;
                List<CustomProperty> customProps = obj == null ? null : obj.Properties;
                PropertyDescriptor[] props = new PropertyDescriptor[stdProps.Count + (customProps == null ? 0 : customProps.Count)];
                stdProps.CopyTo(props, 0);
                if (customProps != null)
                {
                    int index = stdProps.Count;
                    foreach (CustomProperty prop in customProps)
                    {
                        props[index++] = new CustomPropertyDescriptor(prop);
                    }
                }
                return new PropertyDescriptorCollection(props);
            }
        }
        private class CustomPropertyDescriptor : PropertyDescriptor
        {
            private readonly CustomProperty prop;
            public CustomPropertyDescriptor(CustomProperty prop)
                : base(prop.Name, null)
            {
                this.prop = prop;
            }
            public override string Category { get { return "Dynamic"; } }
            public override string Description { get { return prop.Desc; } }
            public override string Name { get { return prop.Name; } }
            public override bool ShouldSerializeValue(object component) { return ((CustomObjectType)component)[prop.Name] != null; }
            public override void ResetValue(object component) { ((CustomObjectType)component)[prop.Name] = null; }
            public override bool IsReadOnly { get { return false; } }
            public override Type PropertyType { get { return prop.Type; } }
            public override bool CanResetValue(object component) { return true; }
            public override Type ComponentType { get { return typeof(CustomObjectType); } }
            public override void SetValue(object component, object value) { ((CustomObjectType)component)[prop.Name] = value; }
            public override object GetValue(object component)
            {
                return ((CustomObjectType)component)[prop.Name] == null ? prop.DefaultValue : ((CustomObjectType)component)[prop.Name];
            }
        }
    }


    public class CustomProperty
    {
        public string Name { get; set; }
        public string Desc { get; set; }
        public object DefaultValue { get; set; }
        Type type;

        public Type Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
                //DefaultValue = Activator.CreateInstance(value);
            }
        }
    }


    public enum EnumType
    {
        Money,
        Message,
        Age,
        RealName
    }
}

代码来源于网上。有修改.关于PropertyGrid相关参见:http://www.cnblogs.com/mywebname/archive/2007/11/15/959732.html

代码下载:http://download.csdn.net/detail/fuweiping/9497360

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# WinForms 中,可以使用 ProgressBar 控件来实现进度条功能。下面是一个简单的示例代码,演示如何创建和使用进度条: 1. 首先,在你的窗体上添加一个 ProgressBar 控件。你可以在设计视图中直接拖拽该控件到窗体上,或者通过代码动态创建该控件并将其添加到窗体中。 2. 在窗体的代码中,你可以使用 ProgressBar 控件的 Value 属性来设置当前进度的值。通常,该属性的值应介于 Minimum 和 Maximum 之间。你可以通过修改这两个属性来调整进度条的最小和最大值。 3. 若要更新进度条的值,你可以使用 ProgressBar 控件的 Increment 方法来增加当前值。你还可以直接设置 Value 属性来确切指定进度。 下面是一个简单的示例代码,演示如何使用进度条控件: ```csharp using System; using System.Threading; using System.Windows.Forms; namespace ProgressBarExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void buttonStart_Click(object sender, EventArgs e) { // 设置进度条的最小值和最大值 progressBar.Minimum = 0; progressBar.Maximum = 100; // 模拟耗时操作 for (int i = 0; i <= 100; i++) { // 更新进度条的值 progressBar.Value = i; // 延迟一段时间,以展示进度条的更新 Thread.Sleep(100); } // 完成后显示消息框 MessageBox.Show("操作已完成!"); } } } ``` 在上面的示例中,我们在按钮的 Click 事件处理程序中模拟了一个耗时操作,并使用进度条控件来展示操作的进度。注意,在实际的应用程序中,你需要根据具体的需求和业务逻辑来更新进度条的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值