在以下的代码中,我是要实现一个带下边框的编辑控件,并且在最右边带有一个按钮,其它的就不说了,但父类中的几个属性我想重写,如Text属性,我按代码中的样式进行了重写,编译通过,但在使用这个控件时,在属性窗口中却没显示出Text属性,我把属性名改为Text1,然后去掉override关键字,重新编译后,在属性窗口就可以看到这个Text1属性。但我就想重写Text属性,各位看看我该怎么做呢?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace CS1
{
public partial class UserControl1 : UserControl
{ protected String txttitle;
protected bool hasbtn=true; public UserControl1()
{
InitializeComponent();
// textBox1.BackColor = this.Parent.BackColor;
} private void UserControl1_Paint(object sender, PaintEventArgs e)
{
Graphics dc = e.Graphics;
Pen p = new Pen ( Color.Blue ); if (hasbtn)
{
dc.DrawLine(p, TextTitle.Width + 1, 16, TextTitle.Width + textBox1.Width, 16);
}
else
{
dc.DrawLine(p, TextTitle.Width + 1, 16, this.Width, 16);
}
} private void UserControl1_Resize(object sender, EventArgs e)
{ this.Height = 18; //设置布局
Graphics dc = TextTitle.CreateGraphics();
dc.PageUnit = GraphicsUnit.Pixel;
SizeF s = dc.MeasureString(TextTitle.Text, TextTitle.Font); Size textsize = new Size((int)s.Width, (int)s.Height);
TextTitle.Location = new Point(0, 0);
TextTitle.Size = new Size(textsize.Width, 14); if (hasbtn)
{
textBox1.Location = new Point(textsize.Width + 1, 0);
textBox1.Size = new Size(this.Width - btnSelect.Width - TextTitle.Width, 14); btnSelect.Location = new Point(this.Width - btnSelect.Width, 0);
}
else
{
textBox1.Location = new Point(textsize.Width + 1, 0);
textBox1.Size = new Size(this.Width - textsize.Width, 14);
}
} private void UserControl1_Layout(object sender, LayoutEventArgs e)
{ this.Height = 18; //设置布局
Graphics dc = TextTitle.CreateGraphics() ;
dc.PageUnit = GraphicsUnit.Pixel;
SizeF s =dc.MeasureString(TextTitle.Text, TextTitle.Font); Size textsize = new Size((int)s.Width , (int)s.Height );
TextTitle.Location = new Point(0, 0);
TextTitle.Size=new Size( textsize.Width , 14 ); if (hasbtn)
{
textBox1.Location = new Point(textsize.Width + 1, 0 );
textBox1.Size = new Size( this.Width- btnSelect.Width - TextTitle.Width, 14); btnSelect.Location = new Point(this.Width - btnSelect.Width, 0);
}
else
{
textBox1.Location = new Point(textsize.Width + 1, 0);
textBox1.Size = new Size(this.Width - textsize.Width, 14);
} }
public Color BackColor1
{
get
{
return this.BackColor;
}
set
{
this.BackColor = value;
textBox1.BackColor = value;
}
} [Description("设置控件标题"), Category("外观")]
public String TxtTitle
{
get
{
return TextTitle.Text;
}
set
{
TextTitle.Text = value;
}
} [DefaultValue("true"),Description("设置是否有选择在右边"), Category("外观")]
public bool HasSelectButton
{
get
{
return hasbtn;
}
set
{
btnSelect.Visible = value;
hasbtn = value;
}
}
[DefaultValue("Text1"),Description("设置编辑器文本"),Category("数据")]
public override String Text
{
get
{
return textBox1.Text;
}
set
{
textBox1.Text = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace CS1
{
public partial class UserControl1 : UserControl
{ protected String txttitle;
protected bool hasbtn=true; public UserControl1()
{
InitializeComponent();
// textBox1.BackColor = this.Parent.BackColor;
} private void UserControl1_Paint(object sender, PaintEventArgs e)
{
Graphics dc = e.Graphics;
Pen p = new Pen ( Color.Blue ); if (hasbtn)
{
dc.DrawLine(p, TextTitle.Width + 1, 16, TextTitle.Width + textBox1.Width, 16);
}
else
{
dc.DrawLine(p, TextTitle.Width + 1, 16, this.Width, 16);
}
} private void UserControl1_Resize(object sender, EventArgs e)
{ this.Height = 18; //设置布局
Graphics dc = TextTitle.CreateGraphics();
dc.PageUnit = GraphicsUnit.Pixel;
SizeF s = dc.MeasureString(TextTitle.Text, TextTitle.Font); Size textsize = new Size((int)s.Width, (int)s.Height);
TextTitle.Location = new Point(0, 0);
TextTitle.Size = new Size(textsize.Width, 14); if (hasbtn)
{
textBox1.Location = new Point(textsize.Width + 1, 0);
textBox1.Size = new Size(this.Width - btnSelect.Width - TextTitle.Width, 14); btnSelect.Location = new Point(this.Width - btnSelect.Width, 0);
}
else
{
textBox1.Location = new Point(textsize.Width + 1, 0);
textBox1.Size = new Size(this.Width - textsize.Width, 14);
}
} private void UserControl1_Layout(object sender, LayoutEventArgs e)
{ this.Height = 18; //设置布局
Graphics dc = TextTitle.CreateGraphics() ;
dc.PageUnit = GraphicsUnit.Pixel;
SizeF s =dc.MeasureString(TextTitle.Text, TextTitle.Font); Size textsize = new Size((int)s.Width , (int)s.Height );
TextTitle.Location = new Point(0, 0);
TextTitle.Size=new Size( textsize.Width , 14 ); if (hasbtn)
{
textBox1.Location = new Point(textsize.Width + 1, 0 );
textBox1.Size = new Size( this.Width- btnSelect.Width - TextTitle.Width, 14); btnSelect.Location = new Point(this.Width - btnSelect.Width, 0);
}
else
{
textBox1.Location = new Point(textsize.Width + 1, 0);
textBox1.Size = new Size(this.Width - textsize.Width, 14);
} }
public Color BackColor1
{
get
{
return this.BackColor;
}
set
{
this.BackColor = value;
textBox1.BackColor = value;
}
} [Description("设置控件标题"), Category("外观")]
public String TxtTitle
{
get
{
return TextTitle.Text;
}
set
{
TextTitle.Text = value;
}
} [DefaultValue("true"),Description("设置是否有选择在右边"), Category("外观")]
public bool HasSelectButton
{
get
{
return hasbtn;
}
set
{
btnSelect.Visible = value;
hasbtn = value;
}
}
[DefaultValue("Text1"),Description("设置编辑器文本"),Category("数据")]
public override String Text
{
get
{
return textBox1.Text;
}
set
{
textBox1.Text = value;
}
}
}
}
解决方案 »
- C# 中若想输出一个 Tab,要如何实现,只知道有个PadRight方法!
- 关于snmp中 读出某一进程cpu占用值为27612是什么意思 怎么解释
- C#菜鸟提问
- 安装vs2005 _1986_RTL_x86_chs_VB_Compiler_Package.cab CRC文件损坏
- C#关闭窗口时的交互问题,请各位来帮下忙哦
- 关于事务
- .NET技术公寓(高级群)欢迎您的加入!
- 这个问题用C#要怎么做?
- C# winform 怎么把excel数据导入到listview里面 麻烦给个例子 谢谢
- c#对bmp图片轮廓识别与轮廓坐标分类保存
- 怎么样可以将打开的Excel工作簿1复制一个为工作簿2
- 请教一对像数组的问题!
public new String Text
{
get
{
return textBox1.Text;
}
set
{
textBox1.Text = value;
}
}
[DefaultValue("Text"),Description("设置编辑器文本"),Category("数据")]
public override string Text
{
get
{
return textBox1.Text;
}
set
{
textBox1.Text = value;
}
}
就OK了,Browsable(true)很管用,只是弄不明白为什么一定要加这个。
另外如果加上[BindableAttribute(true)]属性,则可以执行数据绑定.微软的技术弄死人啊