加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

扩展System.Windows.Forms.Button并更改c#中的默认文本

发布时间:2020-12-14 05:36:10 所属栏目:Windows 来源:网络整理
导读:我通过扩展System. Windows.Forms.Button类创建了一个自定义控件按钮. 我在新类的构造函数中设置了默认的.Text .Width和.Height. 当我将此控件放到表单上时,IDE足够聪明,可以注意构造函数中指定的宽度和高度,并将这些属性分配给正在创建的新按钮,但它会忽略T
我通过扩展System. Windows.Forms.Button类创建了一个自定义控件按钮.

我在新类的构造函数中设置了默认的.Text .Width和.Height.

当我将此控件放到表单上时,IDE足够聪明,可以注意构造函数中指定的宽度和高度,并将这些属性分配给正在创建的新按钮,但它会忽略Text属性,并指定.Text按钮是“ucButtonConsumables1”

有没有办法将.Text设置为我选择的默认值?

public partial class ucButtonConsumables : System.Windows.Forms.Button {
    public ucButtonConsumables() {

        this.Text = "Consumables";                   
        this.Width = 184;
        this.Height = 23;

        this.Click += new EventHandler(ucButtonConsumables_Click);

    }

    void ucButtonConsumables_Click(object sender,EventArgs e) {

        MessageBox.Show("Button Clicked")

    }

}

解决方法

从设计器序列化中隐藏Text属性:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text
{
    get { return base.Text; }
    set { base.Text = value; }
}

或使用默认值创建设计器:

public class ConsumablesButtonDesigner : System.Windows.Forms.Design.ControlDesigner
{
    public override void OnSetComponentDefaults()
    {
        base.OnSetComponentDefaults();
        Control.Text = "Consumables";
    }
}

并为您的按钮提供设计师:

[Designer(typeof(ConsumablesButtonDesigner))]
public class ucButtonConsumables : Button
{
   //...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读