社区
C#
帖子详情
自定义控件中属性的重写
mfst
2004-11-18 10:57:41
自定义控件由一个Label和TextBox组成。想把TextBox的Text属性当做自定义控件的Text属性该怎样写。谢谢。
...全文
499
19
打赏
收藏
自定义控件中属性的重写
自定义控件由一个Label和TextBox组成。想把TextBox的Text属性当做自定义控件的Text属性该怎样写。谢谢。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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属性。
该怎样处理呢?
android
重写
控件添加自定义
属性
android
重写
控件添加自定义
属性
,具体可见http://blog.csdn.net/zenmela2011/article/details/42459481
google官网
自定义控件
实例
google官方的
自定义控件
实例,
重写
了onMeasure,onDraw方法以及自定义
属性
的使用。
Android
自定义控件
(一)完全
自定义控件
Android
自定义控件
,是Android开发者必须熟练掌握的技术之一,特别是
中
高级开发者。本套视频详细讲解了安卓
自定义控件
开发的过程,并且结果实战开发。学习本套视频后,你会真正理解
自定义控件
在UI效果展示上的强大之...
自定义控件
C#自定义开发组件,
重写
控件方法;
重写
LABEL 控件添加
属性
Android控件架构与
自定义控件
详解(二)——自定义View
在自定义View时,我们通常会去
重写
onDraw()方法来绘制View的显示内容。如果该View还需要使用wrap_content
属性
,那么还必须
重写
onMeasure()方法。另外,通过自定义attrs
属性
,还可以设置新的
属性
配置值。
C#
110,539
社区成员
642,577
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章