简体   繁体   English

C#中的部分只读文本框

[英]Partially read-only textbox in c#

I've got a list of illegal positions and characters that can't move from those positions. 我列出了非法职位和不能从这些职位移走的字符。 How would I prevent those from being modified in the TextChanged event? 如何防止在TextChanged事件中对其进行修改? Every solution I've come up with has been extremely hacky and unreliable, or has relied on the KeyDown event, which doesn't prevent the user from deleting the read-only text in other ways (select the text and delete it, or just press the backspace key). 我提出的每个解决方案都非常骇人且不可靠,或者依赖于KeyDown事件,这不会阻止用户以其他方式删除只读文本(选择文本并删除它,或者只是按Backspace键)。

I've thought of doing something like this: 我想过要做这样的事情:

//CharPos is a class with an int (CharPos.Position) and a char that 
//should be at that int's position (CharPos.Ch)
foreach (CharPos p in IllegalPositions)
{
    console.Text = console.Text.Remove(p.Position, 1);
    console.Text = console.Text.Insert(p.Position, p.Ch.ToString());
}

But it completely messes up and goes on an infinite loop. 但是它完全混乱了,并进入了无限循环。 And even if I stopped it from doing that, it wouldn't work if you changed the character count of the text, by using the delete key for example. 即使我停止了这样做,但是如果您通过例如使用Delete键更改了文本的字符数,也将无法使用。 Maybe I could use regex somehow? 也许我可以某种方式使用正则表达式? (I dunno, I have no experience at all with regex). (我不知道,我完全没有使用正则表达式的经验)。

Can you combine multiple TextBoxes (for editable parts) with TextBlocks (for readonly parts)? 您可以将多个TextBoxes(用于可编辑部件)与TextBlocks(用于只读部件)组合吗? You would just need to play with styling of the two to match 您只需要玩两个的样式即可匹配

Could you please check the link below: 您能否检查下面的链接:

Disabling or making it readonly a part of the values in a Text Box (.net) 禁用或使其只读为文本框(.net)中值的一部分

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM