Link to home
Start Free TrialLog in
Avatar of BigOldDog
BigOldDog

asked on

Locking a textbox

I am changeing to VB 2010 Express from VB5 and I have run into a problem with locking a text box from my program.

Imagime a form with a button and a text box on it. When the button is clicked the text box should be locked to prevent data entry.

In the buttons Click event  I tried to enter
Me.TextBox1.Locked = True
But apparently this property is not available at runtime, it's there in TextBox1's property sheet at Design time.
Iguess I must be doing something wrong.
Avatar of ramkihardy
ramkihardy

Me.TextBox1.Enabled=False..
Use this It will work..
Regards ramki..
Avatar of Nasir Razzaq
Another alternative is to set the ReadOnly property to true.
Both Enabled=False and ReadOnly=True will prevent data entry. If you use the ReadOnly, you can still high light the content and do a CTRL-C (copy).
Avatar of BigOldDog

ASKER

Enabled = False is no good as it "grays" out the text.
I guess I could set ForeColor to correct this, though I do not know if this will work on a disabled control.

I have not tried ReadOnly = True so I cannot say if that would be OK.

The question remains, why is "Locked" not available? All the help documentation seems to say it should be
In button clicked event write

 this.txt.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            txt.readonly=true;
            e.Handled = true;
        }

Regrds
ramki
is a web or windows application? I think Locked is a web property.
Ignore the previous post..

In button clicked event write

 this.txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
Then
private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            txt.readonly=true;
            e.Handled = true;
        }

Regrds
ramki
Otherwise the easier option is...

Set the ReadOnly property to true and set the Background color to the color you want. When you set the ReadOnly property it changes the background color to Control. This can be changed to whatever you want.

Regards ramki
>The question remains, why is "Locked" not available? All the help documentation seems to say it should be

There is no locked property on Winforms Textbox. What documentation are you referring to?
Actually the fact is....

In VB.NET, the locked property doesn't mean the same as it did in VB6.  Now it means that the control cannot be moved as design time.  

I am referring the latest MSDN....
ASKER CERTIFIED SOLUTION
Avatar of ramkihardy
ramkihardy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK.
TextBox1.ReadOnly = True
Textbox1.BackColor = Color.Yellow
(this happened to be the BackColor of the  text box) works, and comes very close to replicating the old VB6 "Locked" property.
When I checked the help documents that I looked at on line I see my error, is there any way to set help to only show Net framework4 information?