Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / July 2005

Tip: Looking for answers? Try searching our database.

TextBox readonly background colour.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
el_sid - 30 Jun 2005 18:14 GMT
We have currently several enhanced versions of .NET controls (textbox,
combobox checkbox label etc) so that they automtically comform to our
applications look and feel. Everything seems to work fine except for out
enhanced textbox.  Our textbox (like our other enhanced controls)
automatically sets the control's background colour based on it being
readonly/mandatory/enabled etc and this all seems to work apart from in
readonly mode.  In the readonly mode .NET does not call our implementation of
the BackgroudColor property and instead of usnig the colour we have defined
(yellow for readonly textboxes) it uses the default grey colour for readonly.
I have included a cutdown version of our implementation below and any help
would be appreciated.

public class EnhancedTextBox : TextBox
    {
        #region " Auto-generated code. "

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public EnhancedTextBox()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
        #endregion

        #endregion

        private bool mblnMandatory = false;

        /// <summary>
        /// blnMandatory gets/sets the mandatory flag for the field, forcing the
control to be redrawn when the field is set.
        /// </summary>
       
        [Description("Indicates if the field is mandatory or not and is also used
to dictate the field's background colour."), DefaultValue(false)]
        public bool Mandatory
        {
            get
            {
                return(mblnMandatory);
            }
            set
            {
                mblnMandatory = value;
                // Refresh the control so the background colour is set correctly.
                this.Refresh();
            }
        }
        /// <summary>
        /// ReadOnly gets/sets the mandatory read only flag for the field.
        /// </summary>
        /// <remarks>ReadOnly overrides the base class ReadOnly property and when
being set, it forces the control to be redrawn to update it's
        /// background colour.</remarks>
       
        [Description("Indicates if the field is read-only or not and is also used
to dictate the field's background colour."), DefaultValue(false)]
        public new bool ReadOnly
        {
            get
            {
                return(base.ReadOnly);
            }
            set
            {
                base.ReadOnly = value;
                // Refresh the control so the background colour is set correctly.
                this.Refresh();
            }
        }

        /// <summary>
        /// BackColor gets/sets the background colour to be used for the control.
        /// </summary>
        /// <remarks>BackColor overrides the base class BackColor property and
returns the background colour based on the control's
        /// enabled/mandatory/readonly status.</remarks>
       
        [Description("Indicates the background colour for the control.")]
        public override Color BackColor
        {
            get
            {
                // If the control is enabled then use the correct background colour.
                if(Enabled)
                {
                    if(!base.ReadOnly && !mblnMandatory)
                    {
                        return(SystemColours.gcolEditableTextBackground);
                    }
                    else if(base.ReadOnly)
                    {
                        return(SystemColours.gcolReadOnlyBackground);
                    }
                    else
                    {
                        return(SystemColours.gcolTextMandatoryBackground);
                    }
                }
                    // Else the control is not enabled so return the current background
colour.
                else
                    return(base.BackColor);
            }
            set
            {
                base.BackColor = value;
            }
        }

    }
Robert Conde - 30 Jun 2005 20:19 GMT
I couldn't quite figure out why it works in one instance and not
another...but if you instead set the BackColor when you change
readonly(after you do base.ReadOnly = value) it will work.

> We have currently several enhanced versions of .NET controls (textbox,
> combobox checkbox label etc) so that they automtically comform to our
[quoted text clipped - 139 lines]
>
> }
el_sid - 01 Jul 2005 10:28 GMT
Thanks for the help, that worked.

> I couldn't quite figure out why it works in one instance and not
> another...but if you instead set the BackColor when you change
[quoted text clipped - 143 lines]
> >
> > }
"Jeffrey Tan[MSFT]" - 01 Jul 2005 08:25 GMT
Hi el_sid,

Thanks for your post.

Yes, I think Robert is correct. When we set TextBox to readonly,
TextBox.BackColor will be ignored with Painting.

This is a known issue, which is documented in our internal database.

Actually, .Net TextBox is a simple wrapper of Win32 Edit control, and it
passes the painting issue to the Win32 Edit control. If we use Reflector to
view TextBox.ReadOnly property, we can see that:

public void set_ReadOnly(bool value)
{
     if (this.textBoxFlags[TextBoxBase.readOnly] != value)
     {
           this.textBoxFlags[TextBoxBase.readOnly] = value;
           if (base.IsHandleCreated)
           {
                 base.SendMessage(0xcf, value ? -1 : 0, 0);
           }
           this.OnReadOnlyChanged(EventArgs.Empty);
     }
}
Note: 0xcf stands for EM_SETREADONLY message.

So when we set TextBox to readonly, it simply set TextBox to readonly
state.

However, when the textbox is in readonly state, the system is drawing the
text box and it determins the background color.

To workaround this issue, we can just re-set BackColor in the ReadOnly
property(after base.ReadOnly calling), then it will override windows's
coloring.

public new bool ReadOnly
{
    get
    {
        return(base.ReadOnly);
    }
    set
    {
        base.ReadOnly = value;
                       this.BackColor=SystemColours.gcolReadOnlyBackground;
        // Refresh the control so the background colour is set correctly.
        this.Refresh();
    }
}
===============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

el_sid - 01 Jul 2005 10:28 GMT
Thanks for the help that worked.

> Hi el_sid,
>
[quoted text clipped - 57 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 04 Jul 2005 03:08 GMT
You are welcome

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2012 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.