Solved

Locking a textbox

Posted on 2011-09-30
12
501 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
Comment Utility
Me.TextBox1.Enabled=False..
Use this It will work..
Regards ramki..
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Another alternative is to set the ReadOnly property to true.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
 

Author Comment

by:BigOldDog
Comment Utility
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
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
is a web or windows application? I think Locked is a web property.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:ramkihardy
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now