Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Extending RichTextBox - Issues with UserPaint=true

Extending RichTextBox - Issues with UserPaint=true

Extending RichTextBox - Issues with UserPaint=true

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.   



SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
causes OnPaint to fire, but results in flickering at best.. at worst, the contents of the control do not appear at all.  


SetStyle(ControlStyles.AllPaintingInWmPaint, true);
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:


public class RichTextBoxExt : RichTextBox
        #region private variables
        private bool bFlow = false;
        private bool bAutoSize = false;

        private string sNullDisplayText = "";



        #region Public properties
        /// <summary>
        /// Text to display if the field is null.
        /// </summary>
        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)
            if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(sNullDisplayText))
                e.Graphics.DrawString(sNullDisplayText, this.Font, new System.Drawing.SolidBrush(System.Drawing.Color.LightGray), this.ClientRectangle);
                e.Graphics.DrawString(this.Text, this.Font, new System.Drawing.SolidBrush(System.Drawing.Color.Black), this.ClientRectangle);


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close