steve4king
IS-IT--Management
I've been fighting with this for a while now.
I wanted to add a nullValueDisplay string to a TextBox. (among other things) This way if the field is empty, it will say "(null)".
Simple right?
Well the problem is, in .net 3.5(and I don't know how long prior) TextBox does not use OnPrint by default. It uses some other inaccessible draw method.
Forcing:
causes OnPaint to fire, but results in flickering at best.. at worst, the contents of the control do not appear at all.
did not resolve the problem.
I found that RichTextBox did not have this issue, so I extended that as well with all of my new features. Problem solved right?!
Well.. not so much because now I need to figure out a more accurate method for calculating the field length(in pixels). Worse than that is that the cursor ends up in the wrong position. Usually half, to a full character behind, but potentially in-front of the text being typed. (So while the cursor is actually at the end of the word it will draw like this, "Grandma and Grand|pa")
Anyone dealt with these issues before?
Here's a snippet of my code:
I wanted to add a nullValueDisplay string to a TextBox. (among other things) This way if the field is empty, it will say "(null)".
Simple right?
Well the problem is, in .net 3.5(and I don't know how long prior) TextBox does not use OnPrint by default. It uses some other inaccessible draw method.
Forcing:
Code:
SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
Code:
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
I found that RichTextBox did not have this issue, so I extended that as well with all of my new features. Problem solved right?!
Well.. not so much because now I need to figure out a more accurate method for calculating the field length(in pixels). Worse than that is that the cursor ends up in the wrong position. Usually half, to a full character behind, but potentially in-front of the text being typed. (So while the cursor is actually at the end of the word it will draw like this, "Grandma and Grand|pa")
Anyone dealt with these issues before?
Here's a snippet of my code:
Code:
public class RichTextBoxExt : RichTextBox
{
#region private variables
private bool bFlow = false;
private bool bAutoSize = false;
private string sNullDisplayText = "";
#endregion
#region Public properties
/// <summary>
/// Text to display if the field is null.
/// </summary>
[Browsable(true)]
public virtual string NullDisplayText
{
get { return sNullDisplayText; }
set { sNullDisplayText = value; }
}
public RichTextBoxExt()
: base()
{
//SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(sNullDisplayText))
{
e.Graphics.DrawString(sNullDisplayText, this.Font, new System.Drawing.SolidBrush(System.Drawing.Color.LightGray), this.ClientRectangle);
}
else
{
e.Graphics.DrawString(this.Text, this.Font, new System.Drawing.SolidBrush(System.Drawing.Color.Black), this.ClientRectangle);
}
}
}