WinForm关于VS自动更改继承的Button.Text的问题

阿良chjlcn 2010-03-03 07:21:05
以下自定义控件继承自Button,已经设置 了Text=粘贴全部
但是从VisualStudio的工具箱中拖出来时,自动将Text设置为pasteAllButton1
怎样才能不能让VS自动更改设置好的Text。

using System;

using System.Windows.Forms;
using System.ComponentModel;

namespace Lib
{
public class PasteAllButton : Button
{
public PasteAllButton()
: base()
{
InitializeComponent();

}


public TextBox PasteTextBox { get; set; }

private void InitializeComponent()
{
this.SuspendLayout();
//
// PasteAllButton
//
this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Text = "粘贴全部";

this.Click += new System.EventHandler(this.PasteAllButton_Click);
this.ResumeLayout(false);

}



private void PasteAllButton_Click(object sender, EventArgs e)
{
string text = Clipboard.GetText();
if (string.IsNullOrEmpty(text))
{
MessageBox.Show("剪贴板没有数据。");
return;
}
PasteTextBox.Text = text;
}


}
}
...全文
159 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿良chjlcn 2010-03-04
  • 打赏
  • 举报
回复
要使用控件设计器。写一个继承ControlDesigner的类,然后重写InitializeNewComponent方法,在该方法中设置Text属性。
完整代码如下:

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Collections;

namespace Lib
{
[Designer(typeof(PasteAllButtonDesigner))]
public class PasteAllButton : Button
{
public PasteAllButton()
: base()
{
InitializeComponent();
}


public TextBox PasteTextBox { get; set; }

private void InitializeComponent()
{
this.SuspendLayout();
//
// PasteAllButton
//
this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Click += new System.EventHandler(this.PasteAllButton_Click);
this.ResumeLayout(false);

}



private void PasteAllButton_Click(object sender, EventArgs e)
{
string text = Clipboard.GetText();
if (string.IsNullOrEmpty(text))
{
MessageBox.Show("剪贴板没有数据。");
return;
}
PasteTextBox.Text = text;
}

class PasteAllButtonDesigner : ControlDesigner
{
PasteAllButton _button;
public override void Initialize(IComponent component)
{
base.Initialize(component);
_button = component as PasteAllButton;
}
public override void InitializeNewComponent(IDictionary defaultValues)
{
base.InitializeNewComponent(defaultValues);
_button.Text = "粘贴全部";
}


DesignerActionListCollection _lists;
public override DesignerActionListCollection ActionLists
{
get
{
if (_lists == null)
{
_lists = new DesignerActionListCollection();
_lists.Add(new MyDesignerActionList(Component));
}

return _lists;
}
}

class MyDesignerActionList : DesignerActionList
{
PasteAllButton _button;
public MyDesignerActionList(IComponent component)
: base(component)
{
_button = component as PasteAllButton;
}

public TextBox PasteTextBox
{
get
{
return _button.PasteTextBox;
}
set
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(_button)["PasteTextBox"];
descriptor.SetValue(_button, value);
}
}
public string Text
{
get
{
return _button.Text;
}
set
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(_button)["Text"];
descriptor.SetValue(_button, value);
}
}
}



}
}


}

阿良chjlcn 2010-03-04
  • 打赏
  • 举报
回复
楼上的这个不行, 我试过了
jbo126 2010-03-03
  • 打赏
  • 举报
回复

public PasteAllButton()
: base()
{
InitializeComponent();
this.Text = "粘贴全部";


}

8,832

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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