How to stop disabled text boxes being greyed out

  • Thread starter Thread starter John Austin
  • Start date Start date
J

John Austin

I have an app that displays about 20 items of data in text boxes. Very
occasionally I need to allow these to be used for data entry, but the bulk of
the time they are solely for information. They all sit in a GroupBox that is
normally disabled, but is enabled when data can be edited. The users complain
that when the GroupBox.Enabled=False, the greyed out text boxes are hard to
read. I would like to find a way to disable the GroupBox whilst leaving the
text boxes clearly visible.
 
You might try setting each textbox's ReadOnly property to True, instead of
disabling them. Then when you wanted to enable (make them non ReadOnly), you
could:

For Each ctrl As Control In GroupBox1.Controls
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).ReadOnly = False
End If
Next
 
John
Although it is possible to use the Readonly property to lock the contents of
a Texbox. As a user I would expect a normal looking textbox to be editable
only to find that it is not. You may want to work with your uses to see how
they feel about this. Sometimes could be quite frustrating unless you show
them some visual cue as to when it is disabled and when enabled for editing.

Regards,

Trevor Benedict
MCSD
 
Yes, I do a similar thing to set the text boxes BackColor in
GroupBox1.EnabledChanged so that it is obvious to the user when they can and
can't type in the boxes. The thing I don't like about setting ReadOnly is
that if the user can get the cursor in the box, they then phone up and ask
why they can't type anything. I really want to make the Text Boxes look like
labels when they can't type in them, and like text boxes when they can.

Thanks for the suggestion.
 
I agree, but a ReadOnly textbox is not normal looking. It has a 'control'
color background. Also, if you clue your users by changing the background
when they enter it, you can skip that for a readonly.
 
Well, you could alwayws switch between labels and textboxes in code. The
reason that a readonly textbox can be 'entered' is to allow the user to see
any text that may extend beyond the limits. In you don't want them to enter
it when it is readonly add the following to each of the enter events.

If TextBox1.ReadOnly = True Then
Me.SelectNextControl(TextBox1, True, True, True, True)
End If


May be better to just use 'addhandler' here.

Private Sub MoveAlong(Byval sender....)
Me.SelectNextControl(Sender, True....)
End Sub

In any case, you get the idea. Also, if you really want them to look like
labels, then when you set their ReadOnly property to true, also set their
border style to 'None'. Then they will look exactly like labels and with the
above code, can't be 'entered'.
 
The only real problem is that users complain that they have difficulty
reading greyed out text boxes.
The objective of 'greying out' is a visual cue to show the user that the
text box is disabled, not to prevent them reading it easily.
A better solution from Microsoft would be to treat Enabled=False the same
way as ReadOnly=True, if system colours are used, apply grey; If custom
colours are used leave it to the programmer.
 
If you look back on the 27th, you will see that I showed you how to make the
textboxes look exactly like lables - just change their border style as well
as make them R/O and move the focus 'along' when they get entered. Read
that earlier post and see if that does not do exactly what you were looking
for.
 
Thanks for your suggestion Terry, it certainly is a reasonable work around.
Jeffrey Tan of Microsoft have also suggested a way of diasabling the GroupBox
without greying out using "EnableWindow(hwnd, Enable)":

Imports System.Runtime.InteropServices
Public Class Form1
Dim BoxEnabled As Boolean = True
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function EnableWindow(ByVal hWnd As IntPtr, ByVal enable
As Boolean) As Boolean
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
BoxEnabled = Not BoxEnabled
EnableWindow(GroupBox1.Handle, BoxEnabled)
End Sub
End Class
 
Well that certainly takes care of the greyed out text being hard to read, but
it still leaves you with textboxes that look like you should be able to edit,
which I thought was part of your concern. Did you have another thread going
for this discussion? I am curious now as to why the API call acts
differently then setting the enabled property of the groupbox.
 
Back
Top