如何使用基础 class 设置控件和 WinForm 样式?
How to Set Control and WinForm Styles using a base class?
我正在尝试创建一个基础 class 来更改现有表单的用户界面。
我刚刚开始开发 class 并具有以下代码:
public class UI_1:Form
{
public UI_1()
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Font = new System.Drawing.Font("Segoe UI", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BackColor = System.Drawing.SystemColors.HotTrack;
}
}
public partial class Form_LoginNew : UI_1
{
public Form_LoginNew()
{
}
}
我将继承此基础 class 到我现有的表格 (Form_LoginNew),如上所示。在测试时,"FormBorderStyle" 是 set/updated...但是 "BackColor" 和 "Font" 没有改变。为什么它不起作用??
此外,请告诉我如何使用此基础 class 更改(按钮、标签等)的控件样式。请记住表单和控件已经存在。另外,我不能使用 WPF。
创建原始表单时,它们很可能是使用设计器构建的。
以 BackColor
为例,如果这已在设计器中更改,这将发生在派生 class 的 InitializeComponent
中(在您的基础 class 之后构造函数被调用)因此无论你在那里设置什么都会被有效地忽略。
你可以在你的基地做这样的事情class
public override System.Drawing.Color BackColor
{
get
{
return System.Drawing.SystemColors.HotTrack;
}
set
{
// Don't do anything here
}
}
如果您尝试这样做,您会发现无法在设计器中为派生表单更改 BackColor
,因为 setter 不执行任何操作。
关于你的问题
Also, please let me know how to change Control Styles of (button, labels..etc) using this base class. Please, remember the forms and controls are already existing . Also, i cannot go with WPF.
您可以创建自己的 ControlsCollection,如下所示:
public class MyControlCollection : Control.ControlCollection
{
public MyControlCollection(Control owner) : base(owner)
{
}
public override void Add(Control value)
{
// Modify whatever type of control you want to here
if (value is Button)
{
// As an example, I will set the BackColor of all buttons added to the form to red
Button b = (Button)value;
b.BackColor = Color.Red;
}
base.Add(value);
}
}
要在您的基础 class 中使用它,您需要做的就是覆盖 CreateControlsInstance
方法:
protected override Control.ControlCollection CreateControlsInstance()
{
return new MyControlCollection(this);
}
我建议不要尝试使用继承来设置样式,而是推荐一组可以为您应用样式的扩展方法。
当我尝试创建一个继承表单 FormThatNeedsStyle
的表单时,我在 FormThatNeedsStyle
的构造函数中设置的样式有效(甚至在设计器中)。
public partial class FormThatNeedsStyle : Form
{
public FormThatNeedsStyle()
{
InitializeComponent();
this.Style();
}
}
// Extension method class to apply styles
public static class Styles
{
public static void Style(this Form form)
{
form.BackColor = Color.HotPink;
foreach (var control in form.Controls)
{
// Apply desired styles to controls within the form
if (control is Button)
(control as Button).Style();
}
}
public static void Style(this Button button)
{
button.FlatStyle = FlatStyle.Flat;
}
}
我正在尝试创建一个基础 class 来更改现有表单的用户界面。 我刚刚开始开发 class 并具有以下代码:
public class UI_1:Form
{
public UI_1()
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Font = new System.Drawing.Font("Segoe UI", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BackColor = System.Drawing.SystemColors.HotTrack;
}
}
public partial class Form_LoginNew : UI_1
{
public Form_LoginNew()
{
}
}
我将继承此基础 class 到我现有的表格 (Form_LoginNew),如上所示。在测试时,"FormBorderStyle" 是 set/updated...但是 "BackColor" 和 "Font" 没有改变。为什么它不起作用??
此外,请告诉我如何使用此基础 class 更改(按钮、标签等)的控件样式。请记住表单和控件已经存在。另外,我不能使用 WPF。
创建原始表单时,它们很可能是使用设计器构建的。
以 BackColor
为例,如果这已在设计器中更改,这将发生在派生 class 的 InitializeComponent
中(在您的基础 class 之后构造函数被调用)因此无论你在那里设置什么都会被有效地忽略。
你可以在你的基地做这样的事情class
public override System.Drawing.Color BackColor
{
get
{
return System.Drawing.SystemColors.HotTrack;
}
set
{
// Don't do anything here
}
}
如果您尝试这样做,您会发现无法在设计器中为派生表单更改 BackColor
,因为 setter 不执行任何操作。
关于你的问题
Also, please let me know how to change Control Styles of (button, labels..etc) using this base class. Please, remember the forms and controls are already existing . Also, i cannot go with WPF.
您可以创建自己的 ControlsCollection,如下所示:
public class MyControlCollection : Control.ControlCollection
{
public MyControlCollection(Control owner) : base(owner)
{
}
public override void Add(Control value)
{
// Modify whatever type of control you want to here
if (value is Button)
{
// As an example, I will set the BackColor of all buttons added to the form to red
Button b = (Button)value;
b.BackColor = Color.Red;
}
base.Add(value);
}
}
要在您的基础 class 中使用它,您需要做的就是覆盖 CreateControlsInstance
方法:
protected override Control.ControlCollection CreateControlsInstance()
{
return new MyControlCollection(this);
}
我建议不要尝试使用继承来设置样式,而是推荐一组可以为您应用样式的扩展方法。
当我尝试创建一个继承表单 FormThatNeedsStyle
的表单时,我在 FormThatNeedsStyle
的构造函数中设置的样式有效(甚至在设计器中)。
public partial class FormThatNeedsStyle : Form
{
public FormThatNeedsStyle()
{
InitializeComponent();
this.Style();
}
}
// Extension method class to apply styles
public static class Styles
{
public static void Style(this Form form)
{
form.BackColor = Color.HotPink;
foreach (var control in form.Controls)
{
// Apply desired styles to controls within the form
if (control is Button)
(control as Button).Style();
}
}
public static void Style(this Button button)
{
button.FlatStyle = FlatStyle.Flat;
}
}