关于用户自定义控件的问题,请各位大侠帮忙!

vcforever 2003-11-19 10:41:32
我用C#创建了一个自定义的控件,这个控件是从UserCtrl派生来的,是个复合控件,包括一个TextBox和一个按钮,我要完成的功能是:这个控件有两个属性一个是显示在TextBox中的文本内容,另一个属性是一个值(这个值也是个字符串),要求当焦点在TextBox上的时候,当按回车键的时候,在控件中显示的文本就变成相应的值,
还有就是当这个控件完成时,允许用户像使用Button一样,可以在Button的Click事件中编写代码,我现在就是不知道怎样继承这个复合控件中的Button的Click事件来达到我要的效果,小弟的表达能力有限.
希望大侠们出手相救,分不是问题!最好有例子或代码,感谢!
...全文
28 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
vcforever 2003-11-20
  • 打赏
  • 举报
回复
谢谢你的帮助!马上给分!呵呵
xiaha3 2003-11-19
  • 打赏
  • 举报
回复

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(72, 96);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// UserControl1
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(240, 152);
this.ResumeLayout(false);

}
#endregion
}
}
xiaha3 2003-11-19
  • 打赏
  • 举报
回复
实际上你的要求都可以用其他的方法实现,也不是很麻烦的
这个空间在使用时可以在属性表中找到我添加的两个属性,可以实现你的要求

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WindowsApplication4
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public UserControl1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化

}

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

}
set
{

this.textBox1 .Text =value;
}
}
public event EventHandler Botton1Click
{
add
{
this.button1 .Click+=value;
}
remove
{
this.button1 .Click -=value;
}
}
vcforever 2003-11-19
  • 打赏
  • 举报
回复
大侠们都那里去了???
up

110,534

社区成员

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

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

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