Windows Develop Bookmark and Share   
 index > Windows Forms General > Preventing User from entering Text into a TextBox Control
 

Preventing User from entering Text into a TextBox Control

Hi all,

I have a problem where I have a Form that has several TextBox Controls where the user needs to naviagte through them using the keyword. This is cool, and it works great, but what i don't want is the user to be able to edit the Text within these TextBox Controls (hitting enter on the TextBox Control will open up a new Form where data is entered, and when this Form is closed, the Text goes into the TextBox). Now, if I was to make the TextBox Control ReadOnly, then it gives the TextBox Control an ugly Grey BackColor, which is definately not what I want.

I can't make the TextBox Control disabled as then my users will not be able to naviagte to the Control as disabled Controls don't get Focus().

So I am wondering whether I can catch the keydown event on the Control, preserve the Text that is in the Control, and at some point in the Event chain, put the Original Text back in the TextBox Control overwriting when the user has entered - but I am not sure exatcly where I should reassign the Text.

Does anyone know where and when I should do this, or better still, if there is another method of preventing user input in a TextBox Control considering the circumstances that I am in.

Thanks

Tryst
Tryst  Thursday, May 04, 2006 9:02 AM
if you set it to read only, it changes its backgroundcolor automatically, but you can change it back yourself? (backcolorproperty)
inge_03  Thursday, May 04, 2006 9:11 AM

If youhandle the KeyPress event you can set Handled property to truein the KeyPressEventArgs. This will effectivly deny any input. You can then add special code to handle when enter is pressed by checking the KeyChar property of the eventargs.

Andreas Johansson  Thursday, May 04, 2006 9:32 AM
Hi, and thanks for the replies.

inge_03 - you aren't able to set the BackColor property of Controls that are ReadOnly.

Andreas Johansson - I have the following code, yet characters are still be entered into the TextBox Control.

private void txtSnagDetailsActionBy_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyCode == Keys.Down )
cbSnagType.Focus();
else if( e.KeyCode == Keys.Up )
cmbSnagDetailsStatus.Focus();
else if( e.KeyCode == Keys.Enter )
{
OpenActionByForm();
((Control)sender).Focus();
}
e.Handled = true;
}

What needs changing?

Thanks

Tryst
Tryst  Thursday, May 04, 2006 10:15 AM

Skip my suggestion. inge_03 suggestion toset BackColor on a ReadOnly control works for me.

txtSnapDetailsActionBy.BackColor = System.Drawing.SystemColors.Window;
txtSnapDetailsActionBy.ReadOnly = true;

Andreas Johansson  Thursday, May 04, 2006 10:57 AM
Nah, this is not working for.

I am using CF 1.0, so maybe this is the problem.

So is it best for me to try and utilise the handled() method of an event?

Thanks

Tryst
Tryst  Thursday, May 04, 2006 2:36 PM

In my first post I suggested to handle the KeyPress event, you handle the KeyDown. If I use your code and add a handler to KeyPress that just set e.Handled = True I get what you seem to want.

I have no clue if CF will be similar but I did observe that characters got into the textbox with only the KeyDown event handled.

Andreas Johansson  Thursday, May 04, 2006 2:39 PM
Hi all,

just raising this up again as I have now migrated to VS 2005 and still can't seem to be able to change the back-color of ReadOnly Controls - even after going through some of the suggestions above.

Is this at all possible, or not?

Thanks

Tryst
Tryst  Tuesday, May 09, 2006 11:54 AM

Hi Tryst,

For smart device question, I suggest you post in the concerned forum.
Smart Device Development
http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=11&SiteID=1
If you still have any concern, please feel free to post here.

Best regards,
Peter Huang

Peter Huang - MSFT  Thursday, May 11, 2006 8:22 AM

You can use google to search for other answers

Custom Search

More Threads

• Internet explorer in WIndow Form
• Hyperlinks in a Datagrid
• window positioning on large screens
• Creating/Editing Office documents in .NET Windows App
• C# in vb.net application
• Convert CWnd to .NET control
• C# Checking a Box From Another Form HELP!
• How to do tracing logs ??
• Character selection in a RichTextBox
• populating an array via another array