[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Textbox forecolor when disabled

Posted on 2005-05-16
14
Medium Priority
?
5,825 Views
Last Modified: 2008-01-09
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.

Any solutions ?
0
Comment
Question by:Éric Moreau
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 14

Expert Comment

by:ptakja
ID: 14015627
How about handling the EnabledChanged event?

Inside that event, simply change the colors to what you want.
0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 14015653
We can change the BackColor without a problem like this:

    Private Sub TextBox3_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.EnabledChanged
        If TextBox3.Enabled Then
            TextBox3.BackColor = Color.White
            TextBox3.ForeColor = Color.Red
        Else
            TextBox3.BackColor = Color.AliceBlue
            TextBox3.ForeColor = Color.Red
        End If
    End Sub

But the ForeColor is not set to Red when the Control is disabled. It stays grayish. That is exactly what I want to fix!
0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 14015655
I have found http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c94c.aspx#q866q but was not able to get it working properly.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:arif_eqbal
ID: 14016198
hi emoreau
the code just inherits from the usual TextBox control and modifies the paint event to show the forecolor in whatever color you want.

But there's on problem though, you can not drag drop this new textbox on your form because its not a control yet
So what you do... create a Custom Control

Open a new VB project and select "Windows Control Library"
Name the project say ProjCustomTextBox
When the designer opens just go to solution Explorer and rename your physical file to a more meaningful name like "MyTextBox"

do not do anything here, switch to code view "Select All" and delete whatever pre-generated code is there and paste these lines there

Public Class MyTextBox
    Inherits TextBox

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim drawBrush As SolidBrush = New SolidBrush(ForeColor)
        e.Graphics.DrawString(Text, Font, drawBrush, 0.0F, 0.0F)
    End Sub

    Public Sub New()
        MyBase.New()
        Me.SetStyle(ControlStyles.UserPaint, True)
    End Sub
End Class

that's it...
build this and create the DLL
now go to your project Right Click on your ToolBox and say Add/Remove Controls
In the .NEt Component list browse to the DLL file and that's it just drag and drop now ....

0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 14017068
add a button to the form that is using this inherited control that toggles on and off the enabled property. Type in this inherited textbox (when it is enabled) to discover that it doesn't let you delete characters, it changes the font, ... The inherited textbox does not work correctly!
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 14017154
Is It ???
I just used the code from the link posted by you
I'll just check
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 14017200
Yeps,
there's some strange behaviour, now there must be something in the Paint Event code I feel

I'll just try to see if I can do something

SyncFusion is a reliable site, they must have tested the code before putting it in there ??
0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 14017729
I use SyncFusion at least once a week and found reliable solutions up to now.

If it's like me, the first time I tried this snippet of code, I saw it working but only when I started toggling it, I saw side effects.
0
 
LVL 8

Accepted Solution

by:
wguerram earned 1500 total points
ID: 14018640
Here is a working version

Public Class MyTextBox
    Inherits TextBox

    <Description("Returns a window handler."), _
    DllImport("user32.dll", _
    SetLastError:=True, CharSet:=CharSet.Auto, ExactSpelling:=True, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
    End Function

    <Description("frees a window handler."), _
    DllImport("user32.dll", _
    SetLastError:=True, CharSet:=CharSet.Auto, ExactSpelling:=True, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
    End Function

    Const WM_PAINT = &HF

#Region "WM_PAINT"

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_PAINT
                MyBase.WndProc(m)

                If Me.Enabled = False Then
                    Dim hDC As IntPtr = GetWindowDC([Handle])
                    Dim gdc As Drawing.Graphics = Drawing.Graphics.FromHdc(hDC)

                    Dim drawBrush As SolidBrush = New SolidBrush(ForeColor)

                    gdc.DrawString(Text, Font, drawBrush, 1.6F, 2.5F)

                    gdc.Dispose()
                    gdc = Nothing
                    ReleaseDC(m.HWnd, hDC)
                End If
            Case Else
                    MyBase.WndProc(m)
        End Select
    End Sub

#End Region

    Public Sub New()
        MyBase.New()

        SetStyle(Windows.Forms.ControlStyles.DoubleBuffer, True)
    End Sub
End Class
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 14053997
Hi  emoreau;

I read this when you first posted it. This is a problem I personally can understand because I am visually impaired myself as your user is. In doing some research and some code snippets from the net I put this together and it seems to work fine. At design time you will be able to set the BacColorDisabled and ForeColorDisabled from the properties window or at run time through code. If you add a new class to the project then add this code. Then in your forms make the following changes:

Friend WithEvents TextBox?? As System.Windows.Forms.TextBox
To
Friend WithEvents TextBox?? As TextboxExtended
or whatever you decide to call the new class, I called it TextboxExtended.
The second change is:
Me.TextBox?? = New System.Windows.Forms.TextBox
To
Me.TextBox?? = New ProjectNameOrNamespace.TextboxExtended

These two changes need to be made for every textbox.

That’s it.


============================================
TextboxExtended Class Begin
============================================
Option Strict On

Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class TextboxExtended
    Inherits System.Windows.Forms.TextBox

    ' Color used for background and foreground when
    ' control is diabled
    Private _BackColorDisabled As Color = Color.Beige
    Private _ForeColorDisabled As Color = Color.Black

    <Category("Appearance"), _
    Description("Sets the BackColor to Non-Standard when Disabled"), _
    DefaultValue(GetType(Color), "Beige")> _
    Public Property BackColorDisabled() As Color
        Get
            Return _BackColorDisabled
        End Get
        Set(ByVal Value As Color)
            _BackColorDisabled = Value
        End Set
    End Property

    <Category("Appearance"), _
    Description("Sets the BackColor to Non-Standard when Disabled"), _
    DefaultValue(GetType(Color), "Black")> _
    Public Property ForeColorDisabled() As Color
        Get
            Return _ForeColorDisabled
        End Get
        Set(ByVal Value As Color)
            _ForeColorDisabled = Value
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        ' Set the TextBrush with the color to paint disabled control text
        Dim TextBrush As SolidBrush = New SolidBrush(Me._ForeColorDisabled)

        ' Is the Control Disabled
        If Not Me.Enabled Then
            ' The control is diabled use BackColorDisabled porperty for background
            Dim BackBrush As New SolidBrush(_BackColorDisabled)
            e.Graphics.FillRectangle(BackBrush, 0.0F, 0.0F, Me.Width, Me.Height)
        End If

        ' Panint the text with the ForeColorDisabled color
        e.Graphics.DrawString(Me.Text, Me.Font, TextBrush, 0.0F, 0.0F)

    End Sub

    Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)

        MyBase.OnEnabledChanged(e)
        If Not Me.Enabled Then
            Me.SetStyle(ControlStyles.UserPaint, True)
        Else
            Me.SetStyle(ControlStyles.UserPaint, False)
        End If

    End Sub
End Class

============================================
TextboxExtended Class End
============================================

I hope this is of some help

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 14054017
Sorry  emoreau;

One somall type O.

For the Public "Property ForeColorDisabled() As Color" Property the attribute should read "Description("Sets the ForeColor to Non-Standard when Disabled"), _"  and not "Description("Sets the BackColor to Non-Standard when Disabled"), _"

That message shows up in the property box at design time at the bottom.

Fernando
0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 14055157
FernandoSoto, I have posted almost this same solution a couple of hours before you at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21432351.html

The reason why I haven't closed this question yet is that I want to test wguerram comment.
0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 14055785
I have accepted wguerram solution because it is working like I requested (and it is not a close copy of what I have already posted http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21432351.html)
0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 14055787
Now I have to work on DateTimePicker, Combos, RadioButtons, CheckBoxes, ...
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question