TextBox ReadOnly ForeColor problem.

grip003

Regular
Joined
Sep 2, 2004
Messages
89
Location
North Carolina
Hey everyone.
I have written a form in which there are a lot of textboxes that store information. However, I don't allow changes unless the user presses Edit. This is done by setting the ReadOnly property to true until the user presses Edit, which sets it to false. However, I need the ability to change the forecolor of the textboxes to be something other than black while readonly is set to true. Is there any way to do this? Right now, everything looks black until I hit Edit, in which all the textboxes take on the forecolor that I assigned them. If the enabled property can be used somehow, that would also work for my application.
 
grip003 said:
Hey everyone.
I have written a form in which there are a lot of textboxes that store information. However, I don't allow changes unless the user presses Edit. This is done by setting the ReadOnly property to true until the user presses Edit, which sets it to false. However, I need the ability to change the forecolor of the textboxes to be something other than black while readonly is set to true. Is there any way to do this? Right now, everything looks black until I hit Edit, in which all the textboxes take on the forecolor that I assigned them. If the enabled property can be used somehow, that would also work for my application.

If I am understanding you correctly, when your application starts your textbox's forecolor is black, and you want that a different color?
 
When the ReadOnly or Enabled properties are set to true, you cannot change the display color.

One thing you can do though is to handle the keypresses.

Create a private Boolean called bEditMode that you set to 'True' when in edit mode, and 'False' when not in edit mode. Then use following code.

At the end of the Sub declaration you see "Handles TB1.KeyPress". Add handles for all your textboxes here, eg: "Handles TB1.KeyPress, TB2.KeyPress, ...

Visual Basic:
Private Sub TB1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB1.KeyPress
   If Not bEditMode Then e.Handled = True
End Sub
 
Hey everyone, thanks for the replies. From what I have been reading, the ForeColor can not be changed when the ReadOnly property is set to true. I need to keep the text boxes as ReadOnly, but I need a way to show the user that something is different. My first idea was ForeColor, but maybe there is something else I can use, maybe something with the BorderStyle?
 
You can change the backcolor of the textbox, and yes, it can be changed in readonly status. That would be the only way I can think of right off the bat without using the keypress method suggested earlier.
 
Have you tried setting the enable flag instead of the readonly, that might allow you to change the color.
 
Back
Top