473,376 Members | 987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

TextBox Readonly OnPaint wrong font! Please help!

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
4 4582
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gidi | last post by:
hi, i read in the web about how can i change the foreColor of a textBox when it's set to enabled=false or readonly, and that what was written: public MyTextBox() { // This call is required by...
8
by: tshad | last post by:
I cannot seem to get the asp:textbox to use classes. Style works fine. I am trying to set the textbox to act like a label in some instance so it doesn't have a border, readonly and the background...
2
by: Eric Diana | last post by:
Hello, Im trying to create a dynamic array of textboxes from an array returned by a web service. THe web service returns a list of fields that I need to place on a dynamically created web page....
3
by: Eric Moreau | last post by:
Hi I have a user that is visually impaired and who cannot read textboxes content when they are disabled. I have tried to inherits from the Textbox and overwrite the Paint method but I have...
1
by: laredotornado | last post by:
Hello, I was wondering if there is any cross-browser CSS way to specify that I want the background color of a readonly input text field to be the same as its parent row. Right now, I have ...
3
by: Dave | last post by:
Hey all, Ok, here's another of my fun questions. I want to rewrite the textbox control in VB.NET. I need to implement superscript and subscript within the box. Don't ask why, but I can't use...
8
by: Filipe Marcelino | last post by:
Hi, I'm trying to create a textbox inheriting from the standard textbox. I would like to: 1. repaint the textbox border; 2. define a color for that border; Till now I made this:
4
by: giddy | last post by:
hi when i run this class i made here , this is what it looks like without text - http://gidsfiles.googlepages.com/LinedTextBox_1.jpg WITH TEXT (heres the issue) -...
0
by: Jacob Donajkowski | last post by:
Once the user logs in I want to have the users switch from the Roster View to the Profile View and enter their profile infomation and save it. Then the next time they login and go to the Profile...
10
by: engteng | last post by:
When textbox properties enable = False the font in the textbox become gray color. How do I change the gray color to black color ? Regards, Tee
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.