【问题标题】:Initialize custom c# control depending on new custom property on runtime根据运行时的新自定义属性初始化自定义 C# 控件
【发布时间】:2016-04-20 10:26:09
【问题描述】:

我创建了一个新的自定义按钮和一个新的布尔属性。 当我在 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;

提前致谢

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    是的,当涉及到自定义控件时,Visual Studio 可能会令人讨厌。但是你的代码确实有缺陷。您已经在构造函数中设置了当布尔值为真时,它被设计成一种方式,而当它为假时,它被设计成另一种方式。但是,我想您希望在更改布尔值时重置设计。以下是我将如何改变它。

    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.
        }
    
    }
    

    现在,只要“Highlight”的值发生变化,if 语句中的代码就会运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2019-11-13
      • 2014-11-26
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      相关资源
      最近更新 更多