自定义属性

shuiwenyu 2009-04-30 04:22:18
我想做一个自定义属性
在属性编辑器里面鼠标进入设置时就弹出一对话框下来可以再上面写文字
就类似Textbox的lines属性
谁知道怎么实现啊
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2009-05-01
  • 打赏
  • 举报
回复
如果不知道怎么测试下面的例子你再发问。


public class My : Component
{
private string[] defaultLines;
private string[] myLines;

[Editor("System.Windows.Forms.Design.StringArrayEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string[] DefaultTextBoxLines { get { return this.defaultLines; } set { this.defaultLines = value; } }

[Editor( typeof(MyEditor), typeof(UITypeEditor))]
public string[] MyLines { get { return this.myLines; } set { this.myLines = value; } }
}

public class MyEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
MyEditorForm editorForm = new MyEditorForm(value as string[]);
editorService.ShowDialog( editorForm );
return editorForm.Lines;
}

private class MyEditorForm : Form
{
public MyEditorForm(string[] lines)
{
InitializeComponent();
this.textBox.Lines = lines;
}

public string[] Lines
{
get { return this.textBox.Lines; }
}

void InitializeComponent()
{
textBox.Multiline = true;
textBox.Dock = DockStyle.Top;
textBox.Height = 200;

okButton.Text = "&Ok";
okButton.Dock = DockStyle.Bottom;
okButton.DialogResult = DialogResult.OK;
okButton.Height = this.ClientRectangle.Height - textBox.Height;

this.Controls.Add(textBox);
this.Controls.Add(okButton);
this.StartPosition = FormStartPosition.CenterScreen;
}

TextBox textBox = new TextBox();
Button okButton = new Button();
}
}
shalen520 2009-04-30
  • 打赏
  • 举报
回复
为这个属性写一个ControlDesigner
格拉 2009-04-30
  • 打赏
  • 举报
回复
DD
deng520159 2009-04-30
  • 打赏
  • 举报
回复
顶一下,这个东东有用哦,
cppfaq 2009-04-30
  • 打赏
  • 举报
回复
看完这个你就会了
里面有完整的例子

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧