开发者

Disabling or making it readonly a part of the values in a Text Box (.net)

开发者 https://www.devze.com 2023-03-03 15:45 出处:网络
I have a textbox (multiline) which开发者_JAVA百科 has initial value, now when user tries to append values to it, he should not be able to modify the initial value.Is it possible in any way?So if you h

I have a textbox (multiline) which开发者_JAVA百科 has initial value, now when user tries to append values to it, he should not be able to modify the initial value. Is it possible in any way?


So if you have "lorem ipsum" in the box you want the user to be able to add to but not remove that text? If so then with a RichTextBox you can do this via a selection's .SelectionProtected property which will mark a region as effectively being read only.

rtBox.Select(0, (rtBox.Text = "I am fixed content").Length);
rtBox.SelectionProtected = true;
rtBox.AppendText(Environment.NewLine);


Another option is to use a Masked Textbox. This way, you can have multiple protected areas if you wish.

For example, you would set the mask to:

"This c\annot be ch\anged. But this c\an\: CCCCCCCCCC"

Which would appear as:

"This cannot be changed. But this can: __________"

Enter as many "C"'s as characters that you would like the user to be able to enter. You can also change the prompt character to a space rather than an "_" if you prefer.

For convenience...

Here is a list and description of masking characters

(taken from http://www.c-sharpcorner.com/uploadfile/mahesh/maskedtextbox-in-C-Sharp/).

0 - Digit, required. Value between 0 and 9.
9 - Digit or space, optional.
# - Digit or space, optional. If this position is blank in the mask, it will be rendered as a space in the Text property.
L - Letter, required. Restricts input to the ASCII letters a-z and A-Z.
? - Letter, optional. Restricts input to the ASCII letters a-z and A-Z.
& - Character, required.
C - Character, optional. Any non-control character.
A - Alphanumeric, required.
a - Alphanumeric, optional.
.  - Decimal placeholder.
, - Thousands placeholder.
: - Time separator.
/ - Date separator.
$ - Currency symbol.
< - Shift down. Converts all characters that follow to lowercase.
> - Shift up. Converts all characters that follow to uppercase.
| - Disable a previous shift up or shift down.
\ - Escape. Escapes a mask character, turning it into a literal. "\\" is the escape sequence for a backslash.

All other characters - Literals. All non-mask elements will appear as themselves within MaskedTextBox. Literals always occupy a static position in the mask at run time, and cannot be moved or deleted by the user.


You could use RichTextBox SelectionProtected property.

0

精彩评论

暂无评论...
验证码 换一张
取 消