TextBox Readonly OnPaint wrong font! Please help!

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;
 
E

Eric Moreau

Hi

I have the same problem. If you find the solution elsewhere, will you please
share it here?

--


HTH

Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
S2i web inc. (www.s2i.com)
http://emoreau.s2i.com/
 
H

H-S

Here is a small bit of test code that exhibits the problem, any help
would be great!

To test; build the code and run it. To start with select the text in
the text box, all seems well. Then hit the button and see that the
text changes and is formatted in the prescribed new style, again all is
well. Then select the new text - the font mysteriously changes.

//File #1 includes overriden textbox class and a test form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{

public class txtBx : TextBox
{

private string strPreviousValue = null;

public bool valueChanged()
{
return (this.Text != this.strPreviousValue);
}

/// <summary>
/// Call if control is READONLY to support painting. Not to be
used when not read only cos painting goes wierd!
/// </summary>
public void doManualPaint()
{
this.SetStyle(ControlStyles.UserPaint, true);
}


/// <summary>
/// Manually do painting - support coloured read-only text when
value changed. e.g. via an accompanying select button.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{

if (this.valueChanged())
{
SolidBrush drawbrush = new
SolidBrush(System.Drawing.Color.Brown);
e.Graphics.DrawString(this.Text, this.Font, drawbrush,
0, 0, StringFormat.GenericTypographic);
//Quit done formatting for value-changed situation.
return;
}


//ELSE - normal case.
SolidBrush normalDrawbrush = new
SolidBrush(SystemColors.ControlText);
e.Graphics.DrawString(this.Text, this.Font,
normalDrawbrush, 0, 0, StringFormat.GenericTypographic);
return;

// base.OnPaint(e);


}

internal void populateWithString(string strText)
{
this.strPreviousValue = strText;
this.Text = strText;
}
}

/// <summary>
/// Form demonstrating wierd read-only textbox paint on selection
behaviour.
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private txtBx txtBxWithFontProblem;
private Button btnChangesText;

private void InitializeComponent()
{
//Create readonly control
txtBxWithFontProblem = new txtBx();
this.Controls.Add(txtBxWithFontProblem);
txtBxWithFontProblem.Location = new Point(50, 50);
txtBxWithFontProblem.ReadOnly = true;
txtBxWithFontProblem.populateWithString("Original Value");
txtBxWithFontProblem.doManualPaint();

//Create test button.
btnChangesText = new Button();
btnChangesText.Text = "Click Me To Change Text";
this.Controls.Add(btnChangesText);
btnChangesText.Location = new Point(50, 100);
btnChangesText.Size = new Size(120, 32);
btnChangesText.Click += new
EventHandler(btnChangesText_Click);


}


/// <summary>
/// Changes text, text should change colour.
/// Then on selecting read-only text (e.g. to copy it) text
formatting should not change but it does!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btnChangesText_Click(object sender, EventArgs e)
{

txtBxWithFontProblem.Text = "New Text";
}
}
}


//File #2 launches test form:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top