gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:36 24 4
gpt4 key购买 nike

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

我想根据该 bool 属性加载两个设计,但父表单已经在 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;

提前致谢

最佳答案

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

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 语句中的代码。

关于c# - 根据运行时的新自定义属性初始化自定义 C# 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36740928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com