Solved

Locking a textbox

Posted on 2011-09-30
12
504 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Publish Error 2 34
Very Large data in MYSQL 7 75
DataGridView / get bound table name? 8 28
VB.net -  GroupBox / Panel DataBinding and behavior 3 15
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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