首页 新闻 会员 周边 捐助

谁有这类的集合编辑器源码不?最好C#的~(如下图)

0
悬赏园豆:5 [已解决问题] 解决于 2013-02-16 21:46

最近在用PropertyGrid来管理类对象,

遇到一个问题,

如果类中用 Dictionary<string, TaskRequest> 做集合,

PropertyGrid控件弹出的集合编辑器无法操作,

用 List<string, TaskRequest> 做集合,就可以用。

所以我想自己开发一个集合编辑器

大家有这类的源码介绍一下。我自己写很麻烦。主要技术有点菜~

问题补充:

请问这是什么控件?

我找不到这类的控件呢?

我是小罗的主页 我是小罗 | 初学一级 | 园豆:6
提问于:2012-11-13 21:37
< >
分享
最佳答案
0

------------------------------------
-- 自定义属性编辑器
------------------------------------
自定义属性编辑器
定义一个从 UITypeEditor 派生的类。
重写 EditValue 方法,以处理用户界面、用户输入操作以及值的分配。
重写 GetEditStyle 方法,以便将编辑器将使用的编辑器样式的类型通知给“属性”窗口。
通过执行下列步骤,可以为在“属性”窗口中绘制值的表示形式添加附加支持:
重写 GetPaintValueSupported 方法以指示编辑器支持显示值的表示形式。
重写 PaintValue 方法以实现该值的表示形式的显示。
如果编辑器应具有初始化行为,则重写 UITypeEditor 构造函数方法


保存文件编辑器
/// <summary>
/// 保存文件编辑器
/// </summary>
/// <example>
/// [Editor(typeof(SaveFileEditor), typeof(System.Drawing.Design.UITypeEditor))]
/// public string FilePath { get; set; }
/// </example>
public class SaveFileEditor : UITypeEditor
{
SaveFileDialog _dlg;
public SaveFileEditor()
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "All files (*.*)|*.*";
InitializeDialog(dlg);
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (DialogResult.OK == _dlg.ShowDialog())
return _dlg.FileName;
else
return value.ToString();
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
protected virtual void InitializeDialog(SaveFileDialog saveFileDialog)
{
_dlg = saveFileDialog;
}
}


枚举下拉框编辑器
[Editor(typeof(SourceTypeEditor), typeof(UITypeEditor))]
public SourceType SourceType {get; set;}
--------------------------------------------
public enum SourceType { LAN, WebPage, FTP, eMail, OCR }
--------------------------------------------
/// <summary>
/// 图片枚举下拉框(属性编辑器)
/// </summary>
public class SourceTypeEditor : UITypeEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}

public override void PaintValue(PaintValueEventArgs e)
{
// 获取资源名称
int i = (int)e.Value;
string sourceName = "";
switch (i)
{
case ((int)SourceType.LAN): sourceName = "LANTask"; break;
case ((int)SourceType.WebPage): sourceName = "WebTask"; break;
case ((int)SourceType.FTP): sourceName = "FTPTask"; break;
case ((int)SourceType.eMail): sourceName = "eMailTask"; break;
case ((int)SourceType.OCR): sourceName = "OCRTask"; break;
}

// 加载资源文件
string module = this.GetType().Module.Name;
module = module.Substring(0, module.Length - 4);
ResourceManager resourceManager = new ResourceManager(module + ".ResourceStrings.SampleResources", Assembly.GetExecutingAssembly());

// 获取并绘制图像资源
Bitmap newImage = (Bitmap)resourceManager.GetObject(sourceName);
Rectangle destRect = e.Bounds;
newImage.MakeTransparent();
e.Graphics.DrawImage(newImage, destRect);
}
}

对比度设置编辑器
[Editor(typeof(ContrastEditor), typeof(System.Drawing.Design.UITypeEditor))]
public int Contrast {get; set;}
--------------------------------------------
/// <summary>
/// 对比度编辑器。使用 FrmContrast 窗口来设置值
/// </summary>
public class ContrastEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService wfes = provider.GetService(typeof(IWindowsFormsEditorService))
as IWindowsFormsEditorService;

if (wfes != null)
{
FrmContrast frmContrast = new FrmContrast(wfes, (int)value);
wfes.DropDownControl(frmContrast);
value = frmContrast.Value;
}
return value;
}
}
------------------------------------------
/// <summary>
/// Summary description for frmContrast.
/// </summary>
public class FrmContrast : System.Windows.Forms.Form
{
public int Value
{
get { return trackBar.Value; }
set { trackBar.Value = value; }
}
private IWindowsFormsEditorService _editorService;
public FrmContrast(IWindowsFormsEditorService editorService, int value)
{
InitializeComponent();
TopLevel = false;
_editorService = editorService;
Value = value;
btnTrack.Text = Value.ToString();
}

private void frmContrast_Closed(object sender, System.EventArgs e)
{
if (_editorService != null)
_editorService.CloseDropDown();
}

private void trackBar_ValueChanged(object sender, System.EventArgs e)
{
btnTrack.Text = Value.ToString();
}

private void btnTrack_Click(object sender, System.EventArgs e)
{
Close();
}
}

 

集合编辑器
方法一:用 List<T> 最为简单
public List<Employee> Employees { get; set; }
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
方法一:继承CollectionEditor
// 集合类编辑器
public class EmployeeCollectionEditor : CollectionEditor
{
public EmployeeCollectionEditor(Type type) : base(type) {}
protected override string GetDisplayText(object value)
{
Employee item = new Employee();
item = (Employee)value;
return base.GetDisplayText(string.Format("{0}, {1}",item.LastName,item.FirstName));
}
}
// 集合类
public class EmployeeCollection : CollectionBase
{
public Employee this[int index]
{
get { return (Employee)List[index]; }
}
public void Add(Employee emp)
{
List.Add(emp);
}
public void Remove(Employee emp)
{
List.Remove(emp);
}
}
// 类
public class Organization
{
[Editor(typeof(EmployeeCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
public EmployeeCollection Employees { get; set;}
}

 

收获园豆:5
surfsky | 菜鸟二级 |园豆:210 | 2013-01-10 10:23
其他回答(1)
0

呵呵 没有

钢的锅 | 园豆:10 (初学一级) | 2012-11-13 22:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册