By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

TextBox Readonly OnPaint wrong font! Please help!

P: n/a
H-S
Please help. This is a real puzzler!

Originally posted on microsoft.public.dotnet.framework.windowsforms but
no answer found!

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. As it is read-only the ForeColor
property has no effect.

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? - Some code demonstrating the problem is below - please try
it.

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.

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";
}
}

}

Dec 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi

If I am understanding correct, you have to add the following code in
your derived class:

protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
Invalidate();
}

Hope that helps,
Adrian
Dec 21 '06 #2

P: n/a
H-S
Adrian,

Thanks for the post, I just tried this (copied and pasted into txtBx
class posted code), it does not seem to work, any more ideas?

Thanks

Rob.
Adrian Matei wrote:
Hi

If I am understanding correct, you have to add the following code in
your derived class:

protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
Invalidate();
}

Hope that helps,
Adrian
Dec 21 '06 #3

P: n/a
I've been playing around with this and it's very annoying. It appears
that there's another painting operation going on when you select the
text. I started looking at overidding WndProc but couldn't determine
which m.Msg to suppress

Then I looked at making the TextBox not readonly (as this forecolor is
usable) but there's too many ways for a user to alter the Text.

Must the user be able to select something in the TextBox or could you
just use a Label or similar that looks like a TextBox?

NB, don't forget to call Dispose on your Brushes after use
drawbrush.Dispose() - even better use a class level brush which gets
disposed in your control's Dispose event
On Dec 21, 12:00 pm, "H-S" <RHarwood-Sm...@iee.orgwrote:
Adrian,

Thanks for the post, I just tried this (copied and pasted into txtBx
class posted code), it does not seem to work, any more ideas?

Thanks

Rob.

Adrian Matei wrote:
Hi
If I am understanding correct, you have to add the following code in
your derived class:
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
Invalidate();
}
Hope that helps,
Adrian- Hide quoted text -- Show quoted text -
Dec 21 '06 #4

P: n/a
I normally use a panel instead of the text box if all I want to do is
display data. This eliminates all the extra code that MS throws into
things.

Regards,
John

"H-S" <RH************@iee.orgwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
Please help. This is a real puzzler!

Originally posted on microsoft.public.dotnet.framework.windowsforms but
no answer found!

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. As it is read-only the ForeColor
property has no effect.

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? - Some code demonstrating the problem is below - please try
it.

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.

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";
}
}

}

Dec 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.