H
H-S
Here is a puzzler!
I have a read-only textBox which shows the results of a selection on
another form. When the selection changes from the saved data, I wish
to show it a different colour.
So I have set the user style: this.SetStyle(ControlStyles.UserPaint,
true);
and overridden the OnPaint method (code below):
This works until I left click the control, then the font changes from
my brown or black microsoft sans-serif 8.25pt to another font (looks
like Arial Bold 10pt). I can't find a way of stoping this from
happening. The control is readonly rather than disabled as I need to
support a context menu strip.
Is something else painting my control when it is selected? How do I
fix this?
protected override void OnPaint(PaintEventArgs e)
{
//Tried defining font rather than using this.Font - makes no
difference.
Font fTextFont = new Font("Microsoft Sans Serif",
(float)8.25, FontStyle.Regular);
//Call to internal function to determine if value has changed.
if (this.valueChanged())
{
SolidBrush drawbrush = new
SolidBrush(System.Drawing.Color.Brown);
e.Graphics.DrawString(this.Text, fTextFont, drawbrush,
0, 0, StringFormat.GenericTypographic);
return;
}
//Normal case
SolidBrush BackBrush = new SolidBrush(this.BackColor);
e.Graphics.FillRectangle(BackBrush, ClientRectangle);
SolidBrush normalDrawbrush = new
SolidBrush(SystemColors.ControlText);
e.Graphics.DrawString(this.Text, fTextFont,
normalDrawbrush, 0, 0, StringFormat.GenericTypographic);
return;
I have a read-only textBox which shows the results of a selection on
another form. When the selection changes from the saved data, I wish
to show it a different colour.
So I have set the user style: this.SetStyle(ControlStyles.UserPaint,
true);
and overridden the OnPaint method (code below):
This works until I left click the control, then the font changes from
my brown or black microsoft sans-serif 8.25pt to another font (looks
like Arial Bold 10pt). I can't find a way of stoping this from
happening. The control is readonly rather than disabled as I need to
support a context menu strip.
Is something else painting my control when it is selected? How do I
fix this?
protected override void OnPaint(PaintEventArgs e)
{
//Tried defining font rather than using this.Font - makes no
difference.
Font fTextFont = new Font("Microsoft Sans Serif",
(float)8.25, FontStyle.Regular);
//Call to internal function to determine if value has changed.
if (this.valueChanged())
{
SolidBrush drawbrush = new
SolidBrush(System.Drawing.Color.Brown);
e.Graphics.DrawString(this.Text, fTextFont, drawbrush,
0, 0, StringFormat.GenericTypographic);
return;
}
//Normal case
SolidBrush BackBrush = new SolidBrush(this.BackColor);
e.Graphics.FillRectangle(BackBrush, ClientRectangle);
SolidBrush normalDrawbrush = new
SolidBrush(SystemColors.ControlText);
e.Graphics.DrawString(this.Text, fTextFont,
normalDrawbrush, 0, 0, StringFormat.GenericTypographic);
return;