Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 9th, 2003, 10:19 AM
Authorized User
 
Join Date: Aug 2003
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default How to change foreground color of disabled control

Hello,

I am developing a VB.NET Windows form. I have some textboxes disabled (enabled=false), but the gray background and grey text make it hard to read. Does anyone know how to change the foreground text color for a control (textbox) that is disabled?

Thanks in advance for any comments.
 
Old September 9th, 2003, 11:10 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I just looked through the member list for the System.Windows.Forms.Textbox control and saw there is a ForeColor and BackColor property. However, I don't think you will be able to change the affects that setting it to disable causes. In old VB I think there was some property like "Editable" but I don't see anything like that in the .Net docs. Perhaps there's another way too "disable" the button without using the enable property. Maybe you can handle the "GotFocus" event of that control and set the focus to another control so that a user can never access it. Dirty but affective. :)

Peter
 
Old September 9th, 2003, 02:50 PM
Authorized User
 
Join Date: Aug 2003
Posts: 18
Thanks: 0
Thanked 1 Time in 1 Post
Default

Peter,

Thanks for your thoughts. I think what I am going to do is use the READONLY property. It actually seems to behave similarly to the ENABLED property in that it grays the background and doesn't allow editing. It has a different foreground color, though, which is more readable.

I am still curious as to how you set the disabled control color, however, so if someone knows please share.

Thanks,

Hubman
 
Old September 9th, 2003, 03:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It's a bit weird how the Textbox behaves. When it is disabled, you can change its BackColor. but not its ForeColor:

TextBox1.Enabled = False
TextBox1.BackColor = Color.White

This will cause the BackGround to be white, but with a Grey text in it (on my Color Scheme).

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old September 9th, 2003, 03:30 PM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hubman:

Instead of disabling a control, I change its background and/or foreground colours and keep it enabled.
 
Old September 9th, 2003, 03:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, that would miss the entire point, wouldn't it? The initial idea was to disable it.... ;)

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 8th, 2003, 09:28 AM
Authorized User
 
Join Date: Oct 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you set the ReadOnly property in the properties window to true the backcolor will grey out and the forecolor will go to Black. I tried changing just the forecolor of my textbox and was unable to do so. However, if you change the backcolor first then change the forecolor it will work and the text will change color.

Note: I used System.Drawing.Color.Gray and .LightGray for the back color but neither matches the original greyed out color of the textbox. I then tried setting backcolor to System.Drawing.SystemColor.Control (the default color for most controls) and then set the .Forecolor to another color, System.Drawing.Color.Red and it worked. It may have just been my darn eyes but there seemed to be a very very slight difference in the hue of the changed box and a textbox where ReadOnly was set to true and the colors unchanged. Could be me though.

I hope this helps and good luck

Risu

 
Old December 3rd, 2003, 09:49 AM
Registered User
 
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

why not "replace" the textbox (or combo, datetimepicker etc.) field with labels if you want to disable them. Labels are not editable. Label colors can be adapted. Just place a label over the control you want to disable and hide the control. For enabling hide the label and show the control.

Here is a code snippet:
*** for disabling ***

Private Sub Disable_Control(ByVal ControlObject As Control)
    Try
      Dim iCnt As Integer
      Dim oEditLabel As Control

      'Check if editlabel doesn't yet exist
      For iCnt = 0 To Me.Controls.Count - 1
        If Me.Controls(iCnt).Name = "elbl" & ControlObject.Name Then
          'editlabel already exists
          ControlObject.Visible = False
          Me.Controls(iCnt).Text = ControlObject.Text
          Me.Controls(iCnt).Visible = True
          Exit For
        ElseIf iCnt = Me.Controls.Count - 1 Then
          'editlabel doesn't yet exist
          Dim oLabel As New Label()
          oLabel.Width = ControlObject.Width
          oLabel.Height = ControlObject.Height
          oLabel.Location = ControlObject.Location
          oLabel.Name = "elbl" & ControlObject.Name
          oLabel.Text = ControlObject.Text
          ControlObject.Visible = False
          Me.Controls.Add(oLabel)
        End If
      Next
    Catch ex As Exception
      MsgBox(ex.ToString)
    End Try
  End Sub 'Disable_Control

*** for enabling ***

 Private Sub Enable_Control(ByVal ControlObject As Control)
    Try
      Dim oEditLabel As Control

      For Each oEditLabel In Me.Controls
        If TypeOf (oEditLabel) Is Label Then
          If oEditLabel.Name = "elbl" & ControlObject.Name Then
            'found representing editlabel
            oEditLabel.Visible = False
            ControlObject.Visible = True
            Exit For
          End If
        End If
      Next
    Catch ex As Exception
      MsgBox(ex.ToString)
    End Try
  End Sub 'Enable_Control

Hope this will be usefull for anyone!

Nyree

 
Old September 18th, 2004, 01:56 PM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Rekless Send a message via AIM to Rekless Send a message via MSN to Rekless Send a message via Yahoo to Rekless
Default

Seems like a lot of code to do something so easy. Seems to me you would want to use the least amount of code as possible for efficiency reasons. Just use:

textbox1.BackColor = System.Drawing.SystemColors.Control

like risu said above. It works fine.

Rekless

Very funny Scotty, now beam down my clothes...
 
Old December 3rd, 2004, 10:59 AM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Why not just using the Locked attribute of the textbox control (setting it to true) and keeping the Enabled attribute set to true? That seems to work fine with VB6: User is not able to change contents of textbox control and foreground color remains the same.

Wilko







Similar Threads
Thread Thread Starter Forum Replies Last Post
re: color change jack_in_black ASP.NET 2.0 Professional 0 July 11th, 2006 07:24 AM
TD Color Change echovue Javascript How-To 1 March 3rd, 2006 03:40 AM
how to change all control color ashish2001mca .NET Framework 2.0 1 February 23rd, 2005 12:47 AM
change background color of toolbar control myth12345 VB How-To 1 January 6th, 2005 12:25 AM
Color change possability? rolandatem VB How-To 2 November 19th, 2004 03:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.