Reputation: 199
I am trying to create a base class to change user interface for existing forms. I have just started to develop the class and have the following code:
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()
{
}
}
I will be inheriting this base class to my existing form(Form_LoginNew) as shown above. On testing this, the "FormBorderStyle" is set/updated...but the "BackColor" & "Font" aren't changed. Why it isn't working??
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.
Upvotes: 2
Views: 3121
Reputation: 2469
When the original Forms were created, they were most probably built using the designer.
Using BackColor
as an example, if this has been changed in the designer, this will happen in InitializeComponent
of the derived class (after your base class constructor is called) and hence whatever you set there is effectively ignored.
You could do something like this in your base class
public override System.Drawing.Color BackColor
{
get
{
return System.Drawing.SystemColors.HotTrack;
}
set
{
// Don't do anything here
}
}
If you try this, you will find you can't change the BackColor
in the designer for the derived form because the setter doesn't do anything.
Regarding your question
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.
You could create your own ControlsCollection, something like this:
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);
}
}
To use this in your base class, all you need to do is override the CreateControlsInstance
method:
protected override Control.ControlCollection CreateControlsInstance()
{
return new MyControlCollection(this);
}
Upvotes: 2
Reputation: 8972
I would recommend against trying to set style using inheritance and would instead recommend a set of extension methods that can apply style for you.
When I gave this a try and made a form inheriting form FormThatNeedsStyle
, the style I set in the constructor for FormThatNeedsStyle
was working (even in the designer).
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;
}
}
Upvotes: 2