Pro VB.NET 2002/2003For 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 .
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?
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. :)
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.
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.
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
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:
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.