Solved

Locking a textbox

Posted on 2011-09-30
12
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 50 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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