Emulating disabled state.

edd1em

Active member
Joined
Jul 21, 2004
Messages
28
Programming Experience
3-5
Hello all, this is my first time posting on this here site.
I have a form with lots of text boxes, they can only be edited after the edit button has been clicked, otherwise, they should be disabled.
But, I was informed that the user will have trouble reading the information with the color scheme that MS provides for the disabled state.
So I have found that by setting:
ctrl.enabled=true
ctrl.locked=true
One should be able to set the fore/back colors as well as the font, and the user will not be able to edit the text.
Now the only problem that I am having is that ctrl.locked does not work, neither does me.txtCode.locked.....
Please help if you can,
Thank You
-Edward
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
This might help you, it sets the forecolor of a TextBox to white making it easier to see when the textbox is disabled.
VB.NET:
[size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ButtonDisable_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ButtonDisable.Click

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.Enabled = [/size][size=2][color=#0000ff]False[/color][/size]
[size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.ForeColor = System.Drawing.Color.White
[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size]
 

edd1em

Active member
Joined
Jul 21, 2004
Messages
28
Programming Experience
3-5
I am not sure if you tested this code, it did not work for me.
I do not think you can set the fore/back colors of a disabled control.

Basicaly my question is how to set the locked property at run time.
-Edward
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
You can set the fore/back colour of a disabled control, I just tried that to make sure. The locked property however is not used to stop people typing into a control. It is used to stop you from moving the control, in my example that is a textbox.
 

edd1em

Active member
Joined
Jul 21, 2004
Messages
28
Programming Experience
3-5
I copied your code, and it did nothing except for disabling the text box.
Locked is also *supposed* to be read only, * is for unconfirmed, because it did not do anything for me.

But if the code posted works for you, do you think im doing something different from you? All I did was start a new project, add a button, add a textbox, copied your code into the click event.

Thanks for your time,
-Edward
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Let me just get this correct in my head so we both know that we are talking about the same thing.
You have lots of textboxes on a form which are disabled at run-time, I.E. they cannot be typed into.
The only time they can be typed into is if the user presses an edit button thereby enabling the textboxes.

If that is the case then this is what you need to do.
1) In the properties of the textboxes change the enabled state to false, you will not be able to type into them now until they are enabled again.
2) create an edit button and double click
3) In the buttons click event you need to type this code
TextBox1.Enabled = True

Do this for each textbox you have and each time the user clicks the edit button the textboxes will become enabled.

You may also want another button to disable the buttons again after they have been edited.
 

edd1em

Active member
Joined
Jul 21, 2004
Messages
28
Programming Experience
3-5
The problem i am having is the color scheme of a disabled textbox is not good enough for my boss, which is the user.

All I need to do is change the font color of a disabled textbox.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
To be honest I don't think you can change the colour of text in a disabled textbox, I don't know what else you could try.
 

edd1em

Active member
Joined
Jul 21, 2004
Messages
28
Programming Experience
3-5
Thats what I said, thats why i need to emulate it, thats why locked would work...

That code that you posted does not work either.

-Edward
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
The locked property is for design time only. If a control's locked property is set to true, you can't drag the control with the mouse in the designer. The locked property has no effect on the control at run time.

Instead of setting the Enabled property to False, set the ReadOnly property to True. Setting the ReadOnly property to true will still allow the user (your boss) to select the text in the textBox, but won't allow the text to be altered. It also changes the BackColor to the Control system color (you can change it if necessary), but the ForeColor remains unaffected.
 

edd1em

Active member
Joined
Jul 21, 2004
Messages
28
Programming Experience
3-5
Thank you paszt!
I only have one problem, i have a loop, that loops through all the controls in a groupbox.

here is the code:

For Each ctrl In ctrlContainer.Controls
If Not TypeOf ctrl Is Label Then
If blBlue Then
ctrl.Enabled = True
ctrl.BackColor = System.Drawing.Color.FromArgb(CType(214, System.Byte), CType(223, System.Byte), CType(245, System.Byte))
Else
ctrl.Enabled = False
ctrl.BackColor = ctrlContainer.BackColor
ctrl.ForeColor = System.Drawing.Color.FromArgb(CType(0, System.Byte), CType(0, System.Byte), CType(0, System.Byte))
End If
End If
Next ctrl

But I am not able to set the read only property of a ctrl....
In other words:
ctrl.readonly=true/false <-- Does not work

Any suggestions?
-Edward
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
That is because the ReadOnly property is a member of the textBox class and not the Control base class.

Instead of checking if the control is not a label, check to see if the control is a textBox. If it is, create another variable of type TextBox, set it equal to the current control in the loop, then set the readOnly property.

Here is some sample code that will set the readOnly property for all textBoxs within a specified control, even if the control contains controls that can have child controls (such as a form with panels or groupboxs):

VB.NET:
Sub MakeReadonly(ByVal ctrlMain As Control, ByVal bReadOnly As Boolean)
    Dim ctrl As Control
    For Each ctrl In ctrlMain.Controls
        If ctrl.HasChildren Then MakeReadonly(ctrl, bReadOnly)
        If TypeOf ctrl Is TextBox Then
            Dim txtBox As TextBox = ctrl
            txtBox.ReadOnly = bReadOnly
        End If
    Next
End Sub
Setting the textBox's readonly property to True will also change the BackColor to the System Control color.
 
Last edited:

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
I like this code and how it works, but what about comboboxes?
How can I make them read only and not disabled, so the background is still white, like a textbox.

And numeric updowns I can make read only, but the user can still use the updown controls. I know how to set the keyboard to false, but how do I set the updown controls to disabled, or whatever?
 
Top Bottom