Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Locking a textbox

Posted on 2011-09-30
12
Medium Priority
?
510 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:BigOldDog
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 2

Expert Comment

by:ramkihardy
ID: 36890911
Me.TextBox1.Enabled=False..
Use this It will work..
Regards ramki..
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36891117
Another alternative is to set the ReadOnly property to true.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36891134
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).
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:BigOldDog
ID: 36891661
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
0
 
LVL 2

Expert Comment

by:ramkihardy
ID: 36891778
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
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36891783
is a web or windows application? I think Locked is a web property.
0
 
LVL 2

Expert Comment

by:ramkihardy
ID: 36891880
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
0
 
LVL 2

Expert Comment

by:ramkihardy
ID: 36891914
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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36891931
>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?
0
 
LVL 2

Expert Comment

by:ramkihardy
ID: 36891985
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....
0
 
LVL 2

Accepted Solution

by:
ramkihardy earned 150 total points
ID: 36892000
Here you can attain the locked state by using this in BUTTON CLICK EVENT

myTextBox.ReadOnly = True

And if you don't want it gray add this afterwards
myTextBox.BackColor = SystemColors.Window
0
 

Author Closing Comment

by:BigOldDog
ID: 36893066
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?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question