求范例如何写自定义label,比如新增属性,方法这些!
解决方案 »
- 请问哪位知道C1Flexgrid直接打印的报表多标题怎样设置?
- winform中放了一个TextBox,怎样慢慢滚动翻屏?先感谢前辈了!
- 高手请进。。。winform(c#)问题
- 当前标识(B205101\ASPNET)没有对“C:\WINNT\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files”的写访问权限。
- 问个简单的问题!!!
- 有没有连接池的一些代码啊
- 前台绑定动态字段,求助!
- 如何调用一个类文件里的公共函数?
- (100分求)请问有ComponentArt公司ComponentArt_WebUI_20产品的注册码么?
- 界面修改无效
- dataGridView的显示数据很慢(非数据库性能问题)
- 做一个CPU温度的程序
其实我就是想,我传text到Label的TEXT时,我做一下加工,比如给他的TEXT是100,我就加工成100$,怎么写呢
private void label1_TextChanged(object sender, EventArgs e)
{
if (!label1.Text.EndsWith("$")) label1.Text = label1.Text + "$";
}
{
public MyLabel()
{
InitializeComponent();
} protected override void OnTextChanged(EventArgs e)
{
if (!this.Text.EndsWith("$"))
this.Text += "$"; base.OnTextChanged(e);
}
}
public class MyLabel:System.Windows.Forms.Label
{
public override string Text
{
get
{
return base.Text + "$";
}
set
{
base.Text = value;
}
}
}
因为看到你的需求是从TextBox赋值到label
所以我就给TextBox扩展了一个方法public static class MyExtensions
{
public static string CustomerStyle(this TextBox tb)
{
if (string.IsNullOrEmpty(tb.Text))
return "";
if (tb.Text.EndsWith("$"))
return tb.Text; return tb.Text + "$";
}
}调用的地方lable.Text=textbox.CustomerStyle();
这不是设计的时候啊!我在我重写的label里 public MoneyLabel()
{
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.AutoSize = false;
this.Size = new System.Drawing.Size(120, 20);
//this.Size = new System.Drawing.Size(120, 20);
}这个autoSize根本没设成功! 后面的SIZE就也没成功!但是前面两个属性都可以的!
是原label里那里给autoSize设置了吧,我该在哪里改了?
控件在设计时会自动生成一些代码(放在:如Form1.Designer.cs 里)
AutoSize是默认为True的,但是,就算我们new AutoSize,并控制在设计时不要生成AutoSize的代码,刚拉过去时,这个控件显示为“可改变大小(就是控件周围有变改大小的标记)”,但却不起作用,编译一下,控件才真正可以改变大小(且大小为我们设置的初始大小),所以感觉在MS内部代码没处理好。我一般用这个办法处理: public partial class MyLabel : Label
{
public MyLabel()
{
InitializeComponent(); this.Size = new Size(200, 50);
} bool autoSize = false;
public new bool AutoSize
{
get { return autoSize; }
set { base.AutoSize=autoSize = value; }
} protected override void OnParentChanged(EventArgs e)
{
if (this.Parent != null)
this.AutoSize = false; base.OnParentChanged(e);
} public new Size Size { get; set; } protected override void OnTextChanged(EventArgs e)
{
if (!this.Text.EndsWith("$"))
this.Text += "$"; base.OnTextChanged(e);
}
}