In a textbox, protect the first words, but allow adding/editing to text past those words

梦想与她 提交于 2020-04-13 05:38:09

问题


So I have a textbox in C# (Using .NET forms) where I am going to accept a users string for some input.

This string already has text (arguments) at the beginning that will exist at the beginning of the string no matter what. It must be there. I want them to be aware of this, but not be able to delete the words from the textbox (so they wont think theyve deleted it already when its going to be there anyways)

So these first arguments must not be able to be deleted or edited.

Any text after these arguments can be added or modified freely as normal.

Is this possible in C#?


回答1:


Assuming WinForms, you can use a RichTextBox control instead. Set the Multiline=False property and here is an example to lock the first characters:

richTextBox1.Text = "LOCKED";
richTextBox1.SelectAll();
richTextBox1.SelectionProtected = true;

or this, which will only lock the first six characters "LOCKED", but allow the user to change the rest of the sentence:

richTextBox1.Text = "LOCKED information";
richTextBox1.Select(0, 6);
richTextBox1.SelectionProtected = true;


来源:https://stackoverflow.com/questions/9880144/in-a-textbox-protect-the-first-words-but-allow-adding-editing-to-text-past-tho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!