一个关于自定义组件的问题,还请各位大人帮忙(分不够可以再补)

builderman 2004-04-05 10:26:33
我现在做了一个简单的给件,就是从UserControl继承后,放了一个Label和一个textbox(就是想把Label和textBox邦定到一起),写了两个公共属用于设置Label与textbox的显示文本.
请问我还要怎么样才能做到在使用这个组件时,设置那两个公共属性后就会自动的改变Label或textBox的显示值(就像一个普通的textBox一样,设置了text的值后,textBox中显示的值马上就会变化).

...全文
56 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
builderman 2004-04-05
  • 打赏
  • 举报
回复
谢谢各位
sunruping 2004-04-05
  • 打赏
  • 举报
回复
可以用override来重写Text这个属性,如下:
public override string Text
{
get
{
return this.SelTextBox.Text;
}
set
{
this.SelTextBox.Text=value;
}
}

北京的雾霾天 2004-04-05
  • 打赏
  • 举报
回复
你可以用override来重写Text这个属性,如下:
public override string Text
{
get
{
return this.SelTextBox.Text;
}
set
{
this.SelTextBox.Text=value;
}
}
paulwoo 2004-04-05
  • 打赏
  • 举报
回复
简单啊~~~
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace HT.WinControls
{
/// <summary>
/// ultextBox 的摘要说明。
/// </summary>
public class ultextBox : System.Windows.Forms.UserControl
{
#region private fields

private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
/// <summary>
/// Label上的文本
/// </summary>
private string m_caption = "Label";
/// <summary>
/// textBox上的文本
/// </summary>
private string m_text = "textBox";

#endregion

#region 公共方法
public ultextBox()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化
label1.Text = m_caption;
textBox1.Text = m_text;
}
#endregion

#region 公共属性
/// <summary>
/// 返回或设置Label的文本
/// </summary>
public string Caption
{
get { return label1.Text; }
set { label1.Text = value; }

}
/// <summary>
/// 返回或设置textBox的文本
/// </summary>
public string TextVal
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
#endregion


/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Left;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(42, 17);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(42, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(94, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// ultextBox
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "ultextBox";
this.Size = new System.Drawing.Size(136, 21);
this.ResumeLayout(false);

}
#endregion


}
}
minajo21 2004-04-05
  • 打赏
  • 举报
回复
public string Text
{
get
{
return this.txt1.text;
}

set
{
this.txt1=value;
this.lbl1=value;
}
}
dowtao 2004-04-05
  • 打赏
  • 举报
回复
Up!!!!我也正遇到这个问题,
希望那位大侠给予解决!!!
期待ing!!!!
minajo21 2004-04-05
  • 打赏
  • 举报
回复
给这个control写一个public get() set() 的text对象,在里面同时显示和修改两个组合控件的值

110,533

社区成员

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

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

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