根据运行时的新自定义属性初始化自定义c#控件

时间:2016-04-20 10:26:09

标签: c# winforms

我制作了一个新的自定义按钮和一个新的布尔属性。 当我在Visual Studio中将新的自定义按钮添加到我的表单时,设计功能很有用。

我想根据布尔属性加载两个设计,但父窗体已经在formdesigner.cs中有设计代码,按钮看起来一样。

我应该覆盖哪种方法来访问"加载"按钮?

这是我的按钮cs

     namespace Regio.UI
      {
public class MetroButton : System.Windows.Forms.Button
{
    public bool Highlight { get; set; }
    public MetroButton()
    {
        if (Highlight == true)
        {


            base.BackColor = Color.White;
            base.ForeColor = Color.Black;
            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderColor = Color.Gray;
            FlatAppearance.BorderSize = 2;
            FlatAppearance.MouseOverBackColor = Color.WhiteSmoke;
        }
        else {
            base.BackColor = Color.White;
            base.ForeColor = Color.Black;
            base.FlatStyle = FlatStyle.Flat;
            base.FlatAppearance.BorderColor = Color.FromArgb(0, 174, 219);
            base.FlatAppearance.BorderSize = 3;
            base.FlatAppearance.MouseOverBackColor = Color.WhiteSmoke;

        }
    }

}
 }

这是Form1.Designer.cs:

 this.metroButton1 = new Regio.UI.MetroButton();
        this.metroButton2 = new Regio.UI.MetroButton();
        this.SuspendLayout();
        // 
        // metroButton1
        // 
        this.metroButton1.BackColor = System.Drawing.Color.Black;
        this.metroButton1.FlatAppearance.BorderColor = System.Drawing.Color.Gray;
        this.metroButton1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.metroButton1.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.metroButton1.ForeColor = System.Drawing.Color.White;
        this.metroButton1.Highlight = false;
        this.metroButton1.Location = new System.Drawing.Point(212, 172);
        this.metroButton1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
        this.metroButton1.MinimumSize = new System.Drawing.Size(100, 30);
        this.metroButton1.Name = "metroButton1";
        this.metroButton1.Padding = new System.Windows.Forms.Padding(3);
        this.metroButton1.Size = new System.Drawing.Size(171, 111);
        this.metroButton1.TabIndex = 0;
        this.metroButton1.Text = "metroButton1";
        this.metroButton1.UseVisualStyleBackColor = false;

提前致谢

1 个答案:

答案 0 :(得分:3)

是的,在自定义控件方面,Visual Studio可能会令人讨厌。但是你的代码确实存在缺陷。你已经在你的构造函数中添加了当布尔值为true时,它是单向设计的,当它是假的时候,它被设计得更加麻烦。但是,我想你在布尔值改变时想要重置设计。以下是我将如何改变这一点。

public class MetroButton : System.Windows.Forms.Button
{
    private bool highlight;
    public bool Highlight
    { 
        get
        {
            return highlight;
        }
        set
        {
            highlight = value;
            if (highlight)//This is the same as "if(highlight == true)" but the last part is redundant in most programming languages.
            {

                FlatAppearance.BorderColor = Color.Gray;
                FlatAppearance.BorderSize = 2;
                FlatAppearance.MouseOverBackColor = Color.WhiteSmoke;
            }
            else 
            {
                base.FlatAppearance.BorderColor = Color.FromArgb(0, 174, 219);
                base.FlatAppearance.BorderSize = 3;
                base.FlatAppearance.MouseOverBackColor = Color.WhiteSmoke;
            }
        }
    }
    public MetroButton()
    {
        base.BackColor = Color.White;
        base.ForeColor = Color.Black;
        FlatStyle = FlatStyle.Flat;
        Highlight = true;//Just setting a default.
    }

}

现在,只要"值突出显示"更改后,将运行if语句中的代码。