Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 9th, 2003, 10:19 AM
Authorized User
 
Join Date: Aug 2003
Location: Durham, NC, USA.
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.
Reply With Quote
  #2 (permalink)  
Old September 9th, 2003, 11:10 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
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
Reply With Quote
  #3 (permalink)  
Old September 9th, 2003, 02:50 PM
Authorized User
 
Join Date: Aug 2003
Location: Durham, NC, USA.
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
Reply With Quote
  #4 (permalink)  
Old September 9th, 2003, 03:08 PM
Imar's Avatar
Wrox Author
Points: 67,317, Level: 100
Points: 67,317, Level: 100 Points: 67,317, Level: 100 Points: 67,317, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,230
Thanks: 66
Thanked 1,416 Times in 1,396 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.
Reply With Quote
  #5 (permalink)  
Old September 9th, 2003, 03:30 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
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.
Reply With Quote
  #6 (permalink)  
Old September 9th, 2003, 03:33 PM
Imar's Avatar
Wrox Author
Points: 67,317, Level: 100
Points: 67,317, Level: 100 Points: 67,317, Level: 100 Points: 67,317, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,230
Thanks: 66
Thanked 1,416 Times in 1,396 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.
Reply With Quote
  #7 (permalink)  
Old October 8th, 2003, 09:28 AM
Authorized User
 
Join Date: Oct 2003
Location: Bohemia, NY, USA.
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

Reply With Quote
  #8 (permalink)  
Old December 3rd, 2003, 08:49 AM
Registered User
 
Join Date: Dec 2003
Location: , , .
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

Reply With Quote
  #9 (permalink)  
Old September 18th, 2004, 01:56 PM
Registered User
 
Join Date: Sep 2004
Location: Buffalo, NY, USA.
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...
Reply With Quote
  #10 (permalink)  
Old December 3rd, 2004, 09:59 AM
Registered User
 
Join Date: Dec 2004
Location: Eatontown, NJ, USA.
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


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02:40 AM
how to change all control color ashish2001mca .NET Framework 2.0 1 February 22nd, 2005 11:47 PM
change background color of toolbar control myth12345 VB How-To 1 January 5th, 2005 11:25 PM
Color change possability? rolandatem VB How-To 2 November 19th, 2004 02:52 PM



All times are GMT -4. The time now is 06:07 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.