改变PropertyGrid编辑风格(转)
忘了在哪转了.....
改变 PropertyGrid 控件的编辑风格(1)——加入日期控件
编辑日期类型数据
步骤一:定义从UITypeEditor 派生的类,示例如下:
using System;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace blog.csdn.net.zhangyuk
{
/// <summary>
/// 在PropertyGrid 上显示日期控件
/// </summary>
public class PropertyGridDateItem : UITypeEditor
{
MonthCalendar dateControl = new MonthCalendar();
public PropertyGridDateItem()
{
dateControl.MaxSelectionCount = 1;
}
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
System.IServiceProvider provider, object value)
{
try
{
IWindowsFormsEditorService edSvc =
(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
if (value is string)
{
dateControl.SelectionStart = DateTime.Parse(value as String);
edSvc.DropDownControl(dateControl);
return dateControl.SelectionStart.ToShortDateString();
}
else if (value is DateTime)
{
dateControl.SelectionStart = (DateTime)value;
edSvc.DropDownControl(dateControl);
return dateControl.SelectionStart;
}
}
}
catch (Exception ex)
{
System.Console.WriteLine("PropertyGridDateItem Error : " + ex.Message);
return value;
}
return value;
}
}
}
步骤二:编辑属性类,指定编辑属性。示例如下:
namespace blog.csdn.net.zhangyuk
{
public class SomeProperties
{
private string _finished_time = "";
//……
[Description("完成时间"), Category("属性"), EditorAttribute(typeof(PropertyGridDateItem),
typeof(System.Drawing.Design.UITypeEditor))]
public String 完成时间
{
get { return _finished_date; }
set { _finished_date = value; }
}
//……
}
}
步骤三:设置PropertyGrid的属性对象。示例如下:
private void Form1_Load(object sender, System.EventArgs e)
{
this.propertyGrid1.SelectedObject = new SomeProperties();
}
改变 PropertyGrid 控件的编辑风格(2)——编辑多行文本
效果:
适用场合:
1、 编辑多行文本;
2、 编辑长文本。
步骤一:定义从UITypeEditor 派生的类,示例如下:
using System;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace blog.csdn.net.zhangyuk
{
/// <summary>
/// PropertyGridMutiText 的摘要说明。
/// </summary>
public class PropertyGridRichText : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
try
{
IWindowsFormsEditorService edSvc =
(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
if (value is string)
{
RichTextBox box = new RichTextBox();
box.Text = value as string;
edSvc.DropDownControl(box);
return box.Text;
}
}
}
catch (Exception ex)
{
System.Console.WriteLine("PropertyGridRichText Error : " + ex.Message);
return value;
}
return value;
}
}
}
步骤二:编辑属性类,指定编辑属性。示例如下:
namespace blog.csdn.net.zhangyuk
{
public class SomeProperties
{
private string _finished_time = "";
// ……
// 多行文本编辑框
string _mutiLineSample = "";
[Description("多行文本编辑框"), Category("属性"), EditorAttribute(typeof(PropertyGridRichText),
typeof(System.Drawing.Design.UITypeEditor))]
public String 多行文本
{
get { return _mutiLineSample; }
set { _mutiLineSample = value; }
}
//……
}
}
步骤三:设置PropertyGrid的属性对象。示例如下:
private void Form1_Load(object sender, System.EventArgs e)
{
this.propertyGrid1.SelectedObject = new SomeProperties();
}
改变 PropertyGrid 控件的编辑风格(3)——打开对话框
适用场合:
1、 打开文件、打印设置等通用对话框
2、 打开特定的对话框
步骤一:定义从UITypeEditor 派生的类,以 OpenFileDialog 对话框为例,示例代码如下:
using System;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace blog.csdn.net.zhangyuk
{
/// <summary>
/// IMSOpenFileInPropertyGrid 的摘要说明。
/// </summary>
public class PropertyGridFileItem : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.
IServiceProvider provider, object value)
{
IWindowsFormsEditorService edSvc =
(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
// 可以打开任何特定的对话框
OpenFileDialog dialog = new OpenFileDialog();
dialog.AddExtension = false;
if (dialog.ShowDialog().Equals(DialogResult.OK))
{
return dialog.FileName;
}
}
return value;
}
}
}
步骤二:编辑属性类,指定编辑属性。示例如下:
namespace blog.csdn.net.zhangyuk
{
public class SomeProperties
{
private string _finished_time = "";
//……
// 文件
string _fileName = "";
[Description("文件打开对话框"), Category("属性"), EditorAttribute(typeof(PropertyGridFileItem),
typeof(System.Drawing.Design.UITypeEditor))]
public String 文件
{
get { return _fileName; }
set { _fileName = value; }
}
//……
}
}
步骤三:设置PropertyGrid的属性对象。示例如下:
private void Form1_Load(object sender, System.EventArgs e)
{
this.propertyGrid1.SelectedObject = new SomeProperties();
}
改变 PropertyGrid 控件的编辑风格(4)——加入选择列表
适用场合:限制选择输入
// 步骤一:定义从UITypeEditor 继承的抽象类:ComboBoxItemTypeConvert。示例如下:
using System;
using System.Collections;
using System.ComponentModel;
namespace blog.csdn.net.zhangyuk
{
/// IMSTypeConvert 的摘要说明。
public abstract class ComboBoxItemTypeConvert : TypeConverter
{
public Hashtable _hash = null;
public ComboBoxItemTypeConvert()
{
_hash = new Hashtable();
GetConvertHash();
}
public abstract void GetConvertHash();
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
int[] ids = new int[_hash.Values.Count];
int i = 0;
foreach (DictionaryEntry myDE in _hash)
{
ids[i++] = (int)(myDE.Key);
}
return new StandardValuesCollection(ids);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object v)
{
if (v is string)
{
foreach (DictionaryEntry myDE in _hash)
{
if (myDE.Value.Equals((v.ToString())))
return myDE.Key;
}
}
return base.ConvertFrom(context, culture, v);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
object v, Type destinationType)
{
if (destinationType == typeof(string))
{
foreach (DictionaryEntry myDE in _hash)
{
if (myDE.Key.Equals(v))
return myDE.Value.ToString();
}
return "";
}
return base.ConvertTo(context, culture, v, destinationType);
}
public override bool GetStandardValuesExclusive(
ITypeDescriptorContext context)
{
return false;
}
}
}
步骤二:定义 ComboBoxItemTypeConvert 的派生类,派生类中实现父类的抽象方法:public abstract void GetConvertHash();
示例如下:
using System;
using System.Collections;
using System.ComponentModel;
namespace blog.csdn.net.zhangyuk
{
public class PropertyGridBoolItem : ComboBoxItemTypeConvert
{
public override void GetConvertHash()
{
_hash.Add(0, "是");
_hash.Add(1, "否");
}
}
public class PropertyGridComboBoxItem : ComboBoxItemTypeConvert
{
public override void GetConvertHash()
{
_hash.Add(0, "炒肝");
_hash.Add(1, "豆汁");
_hash.Add(2, "灌肠");
}
}
}
步骤三:编辑属性类,指定编辑属性。示例如下:
namespace blog.csdn.net.zhangyuk
{
public class SomeProperties
{
private string _finished_time = "";
// ……
// 布尔
bool _bool = true;
[Description("布尔"), Category("属性"), TypeConverter(typeof(PropertyGridBoolItem))]
public int 布尔
{
get { return _bool == true ? 0 : 1; }
set { _bool = (value == 0 ? true : false); }
}
// 选择列表
int _comboBoxItems = 0;
[Description("选择列表"), Category("属性"), TypeConverter(typeof(PropertyGridComboBoxItem))]
public int 选择列表
{
get { return _comboBoxItems; }
set { _comboBoxItems = value; }
}
//……
}
}
步骤四:设置PropertyGrid的属性对象。示例如下:
private void Form1_Load(object sender, System.EventArgs e)
{
this.propertyGrid1.SelectedObject = new SomeProperties();
}
忘了在哪转的了....