O
Olaf Krumnow
I'm trying to override the Font property of some controls.
For a Label this works as expected, but the TextBox ignores
the value returned from the subclass. This small example
shows this effect. I set the Font ot the form to 6pt Sans Serif
so normally all controls would be using this font.
Now I have two classes, one inheriting from Label and one from
TextBox. Both override the Font property and return a 16pt Comic
font.
When running, the Label uses the Comic font but the TextBox
doesn't. Why? And how can I get what I want?
Regards Olaf
-- source --
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication12
{
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
this.ClientSize = new System.Drawing.Size(292, 273);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 6F);
TB tb = new TB();
tb.Text = "Hello";
this.Controls.Add(tb);
LB lb = new LB();
lb.Text = "Hello";
lb.SetBounds(0, 20, 100, 30);
this.Controls.Add(lb);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
internal class TB: System.Windows.Forms.TextBox
{
public override Font Font
{
get { return new Font("Comic Sans MS", 16); }
}
}
internal class LB: System.Windows.Forms.Label
{
public override Font Font
{
get { return new Font("Comic Sans MS", 16); }
}
}
}
For a Label this works as expected, but the TextBox ignores
the value returned from the subclass. This small example
shows this effect. I set the Font ot the form to 6pt Sans Serif
so normally all controls would be using this font.
Now I have two classes, one inheriting from Label and one from
TextBox. Both override the Font property and return a 16pt Comic
font.
When running, the Label uses the Comic font but the TextBox
doesn't. Why? And how can I get what I want?
Regards Olaf
-- source --
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication12
{
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
this.ClientSize = new System.Drawing.Size(292, 273);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 6F);
TB tb = new TB();
tb.Text = "Hello";
this.Controls.Add(tb);
LB lb = new LB();
lb.Text = "Hello";
lb.SetBounds(0, 20, 100, 30);
this.Controls.Add(lb);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
internal class TB: System.Windows.Forms.TextBox
{
public override Font Font
{
get { return new Font("Comic Sans MS", 16); }
}
}
internal class LB: System.Windows.Forms.Label
{
public override Font Font
{
get { return new Font("Comic Sans MS", 16); }
}
}
}