473,750 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TextBox Readonly OnPaint wrong font! Please help!

H-S
Please help. This is a real puzzler!

Originally posted on microsoft.publi c.dotnet.framew ork.windowsform s 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(C ontrolStyles.Us erPaint,
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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion1
{

public class txtBx : TextBox
{

private string strPreviousValu e = null;

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

/// <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(C ontrolStyles.Us erPaint, 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(PaintEv entArgs e)
{

if (this.valueChan ged())
{
SolidBrush drawbrush = new
SolidBrush(Syst em.Drawing.Colo r.Brown);
e.Graphics.Draw String(this.Tex t, this.Font, drawbrush,
0, 0, StringFormat.Ge nericTypographi c);
//Quit done formatting for value-changed situation.
return;
}

//ELSE - normal case.
SolidBrush normalDrawbrush = new
SolidBrush(Syst emColors.Contro lText);
e.Graphics.Draw String(this.Tex t, this.Font,
normalDrawbrush , 0, 0, StringFormat.Ge nericTypographi c);
return;

// base.OnPaint(e) ;

}

internal void populateWithStr ing(string strText)
{
this.strPreviou sValue = strText;
this.Text = strText;
}
}

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

private txtBx txtBxWithFontPr oblem;
private Button btnChangesText;

private void InitializeCompo nent()
{
//Create readonly control
txtBxWithFontPr oblem = new txtBx();
this.Controls.A dd(txtBxWithFon tProblem);
txtBxWithFontPr oblem.Location = new Point(50, 50);
txtBxWithFontPr oblem.ReadOnly = true;
txtBxWithFontPr oblem.populateW ithString("Orig inal Value");
txtBxWithFontPr oblem.doManualP aint();

//Create test button.
btnChangesText = new Button();
btnChangesText. Text = "Click Me To Change Text";
this.Controls.A dd(btnChangesTe xt);
btnChangesText. Location = new Point(50, 100);
btnChangesText. Size = new Size(120, 32);
btnChangesText. Click += new
EventHandler(bt nChangesText_Cl ick);

}

/// <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)
{

txtBxWithFontPr oblem.Text = "New Text";
}
}

}

Dec 21 '06 #1
4 4610
Hi

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

protected override void OnEnter(EventAr gs 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(EventAr gs 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.Dispo se() - 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.orgwr ote:
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(EventAr gs 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.goog legroups.com...
Please help. This is a real puzzler!

Originally posted on microsoft.publi c.dotnet.framew ork.windowsform s 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(C ontrolStyles.Us erPaint,
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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion1
{

public class txtBx : TextBox
{

private string strPreviousValu e = null;

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

/// <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(C ontrolStyles.Us erPaint, 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(PaintEv entArgs e)
{

if (this.valueChan ged())
{
SolidBrush drawbrush = new
SolidBrush(Syst em.Drawing.Colo r.Brown);
e.Graphics.Draw String(this.Tex t, this.Font, drawbrush,
0, 0, StringFormat.Ge nericTypographi c);
//Quit done formatting for value-changed situation.
return;
}

//ELSE - normal case.
SolidBrush normalDrawbrush = new
SolidBrush(Syst emColors.Contro lText);
e.Graphics.Draw String(this.Tex t, this.Font,
normalDrawbrush , 0, 0, StringFormat.Ge nericTypographi c);
return;

// base.OnPaint(e) ;

}

internal void populateWithStr ing(string strText)
{
this.strPreviou sValue = strText;
this.Text = strText;
}
}

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

private txtBx txtBxWithFontPr oblem;
private Button btnChangesText;

private void InitializeCompo nent()
{
//Create readonly control
txtBxWithFontPr oblem = new txtBx();
this.Controls.A dd(txtBxWithFon tProblem);
txtBxWithFontPr oblem.Location = new Point(50, 50);
txtBxWithFontPr oblem.ReadOnly = true;
txtBxWithFontPr oblem.populateW ithString("Orig inal Value");
txtBxWithFontPr oblem.doManualP aint();

//Create test button.
btnChangesText = new Button();
btnChangesText. Text = "Click Me To Change Text";
this.Controls.A dd(btnChangesTe xt);
btnChangesText. Location = new Point(50, 100);
btnChangesText. Size = new Size(120, 32);
btnChangesText. Click += new
EventHandler(bt nChangesText_Cl ick);

}

/// <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)
{

txtBxWithFontPr oblem.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
2777
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 the Windows.Forms Form Designer. this.SetStyle(ControlStyles.UserPaint,true);
8
3522
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 is grey. I have a class set as: ..table2Label{ border-style:none; background-color:#F6F6F6; }
2
3620
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. The problem I am having is when the button event is clicked, the text typed in the textbox doesn't get saved with the textbox. I might be way off base with even trying this. If anyone could give me some idea on what is going wrong or another...
3
9054
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 not been able to get a a perfect textbox. SO what I need is a textbox on which I have the control of the BackColor and the ForeColor when Disabled.
1
11477
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 <tr class="myRowClass"> <td align="right">Account Username</td> <td align="left"> <div id="loginMsg" name="loginMsg"><input
3
2656
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 RTF as my superscript and subscript's are always numbers and are defined as specific characters. ie. char(192) = superscript 1 Does anyone know how to inherit and change the onPaint method of the textbox? I have created my own controlt o try...
8
5094
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
6026
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) - http://gidsfiles.googlepages.com/LinedTextBox_withText.jpg The text turns BOLD and the lines kinda get erased because of the text. perhaps i could overide or handle the onKeydown or somethin and intercept the text to be entered and then draw it myself. Any suggestions????????
0
2399
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 View I want the form populated from there profile on the sql server. The code to save the profile works fine. But when the user logs back in they data doesn't load back to the form. The multiview is located inside the LoginView's Logged-In View ....
10
18044
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
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.