问题
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