自定义控件中属性的重写

mfst 2004-11-18 10:57:41
自定义控件由一个Label和TextBox组成。想把TextBox的Text属性当做自定义控件的Text属性该怎样写。谢谢。
...全文
499 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfst 2004-11-19
  • 打赏
  • 举报
回复
谢谢 forestwind(森林之风) ,谢谢各位。问题解决了。
forestwind 2004-11-18
  • 打赏
  • 举报
回复
public override string Text
寒星 2004-11-18
  • 打赏
  • 举报
回复
你检查一下你的自定义控件类的父类中是否已经定义了Text属性。
520NET 2004-11-18
  • 打赏
  • 举报
回复
LG
mfst 2004-11-18
  • 打赏
  • 举报
回复
如果我把属性名称改为其他如 Text1,则正常。但是属性名称为 Text 时怎么用?
mfst 2004-11-18
  • 打赏
  • 举报
回复
我也是这样写的,可是使用该控件的时候设置了Text的值,运行时值确丢失了!!!
寒星 2004-11-18
  • 打赏
  • 举报
回复
在你的控件类中定义一个名为Text的属性嘛。
...

public string Text
{
get
{
return TextBox.Text;
}
set
{
TextBox.Text = value;
}
}

...
forestwind 2004-11-18
  • 打赏
  • 举报
回复
public override string Text
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text=value;
}
}
forestwind 2004-11-18
  • 打赏
  • 举报
回复
原来如此:DesignerSerializationVisibility要设为Visible,才可以让“The code generator produces code for the object”而基类中public override string Text { get; set; }是被设定成(DesignerSerializationVisibility.Hidden的。如果不去显式的改它,它就不变。这就是为什么Text1能用而Text不能用的原因。

forestwind 2004-11-18
  • 打赏
  • 举报
回复
在控件Text属性代码前面加上
[Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
问题就解决了。
mfst 2004-11-18
  • 打赏
  • 举报
回复
to: forestwind(森林之风)

这样用是没有问题的。
但是,
你在控件Text属性代码前面加上[Browsable ( true ) ]。在Form1中控件usercontrol1的属性中修改Text的值,你运行试试。
forestwind 2004-11-18
  • 打赏
  • 举报
回复
我真的就是这样做的,给你代码:
http://asp2.6to23.com/wyf/test2.rar
mfst 2004-11-18
  • 打赏
  • 举报
回复
to: forestwind(森林之风)

用你的代码生成控件,然后在某个窗体上拖一个该控件,然后修改属性Text的,你运行再看看结果。
biliboy 2004-11-18
  • 打赏
  • 举报
回复
up
forestwind 2004-11-18
  • 打赏
  • 举报
回复
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;
public override string Text
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text=value;
}
}
public UserControl1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 24);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(128, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// UserControl1
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(296, 88);
this.ResumeLayout(false);

}
}
调用时:
this.userControl11.Text="sdfgdfgdfgd";
forestwind 2004-11-18
  • 打赏
  • 举报
回复
我这里测试没有问题,看看你那里是不是在设置了Text的值以后又执行了一遍这个自定义控件的InitializeComponent()
mfst 2004-11-18
  • 打赏
  • 举报
回复
to: forestwind(森林之风)

按你的写法,使用该控件的时候设置了Text的值,运行时值会丢失的!!!
forestwind 2004-11-18
  • 打赏
  • 举报
回复
按我说的办。
mfst 2004-11-18
  • 打赏
  • 举报
回复
to:TonyJoule(寒星㊣)

父类是System.Windows.Forms.UserControl,有Text属性。
该怎样处理呢?

110,539

社区成员

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

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

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