使用C#在Windows应用程序中只读的文本框的一部分 [英] part of textbox readonly in windows Application Using C#
本文介绍了使用C#在Windows应用程序中只读的文本框的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在基于Windows的应用程序上工作,我的要求是文本框控件的某些部分应为只读.其余字段应为他可以输入.
示例:
contentSetName(应为只读).ProcessName(用户将输入)在一个文本框中.
请帮助我.
Hi all,
I am working on Windows based application,My Requirement is some part of the textbox control should be read only .Rest of of field should be he can enter.
Example:
contentSetName(Shoud be ReadOnly).ProcessName(User ll enter ) In one Textbox.
Please Help me.
推荐答案
您应该在用户可以更改文本的所有可能点上添加验证,例如在KeyUp和拦截粘贴事件中.但是您可以找到合适的被屏蔽的编辑控件,并对其进行一些自定义以满足您的需求.
从这里开始:
自定义控件库(MaskEdit,带有进度面板的Statusbar等) [ ^ ]
扩展文本框控件以针对正则表达式进行验证 [ ^ ]
http://msdn.microsoft.com/en-us/library/1fkc1cz6 [ ^ ]
You should add a validation on all possible points where user can change text, like in KeyUp and intercept paste events. But you can find a proper masked edit control and customize it a little bit to fit your needs.
Start here:
Custom Controls Library (MaskEdit, Statusbar with Progress Panel, and more)[^]
Extend the Textbox Control to Validate Against Regular Expressions[^]
http://msdn.microsoft.com/en-us/library/1fkc1cz6[^]
它是有点困难,我个人不建议我现在要做什么.但是如果您仍然必须这样做,他们可以这样做:
1.处理textBox2_TextChanged
事件
2.将textbox
的Text
属性设置为"contentSetName".
3.然后输入以下代码:
It is a little difficult and personally i do not not recommended what I am about to do now. but if you still have to do it them do it like this:
1. handletextBox2_TextChanged
event
2. have theText
property oftextbox
set to "contentSetName."
3. then have this code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
string newtext = textBox2.Text.Replace("contentSetName.", "");
textBox2.Text = "contentSetName." + newtext;
}
注意:更好的方法可能是使用maskedtextbox
代替.还要考虑一下.
Note: The better way could be to use a maskedtextbox
instead. think about that too.
更好的解决方案是
定义文本框的开始和结束位置.然后在发生按键事件后,如果在定义区域上按下了按键,则取消按键提示...
The better solution will be
define a start and end position for your textbox. then after in keypress event if the key is pressed on the defined area then cancel the keyprees...
这篇关于使用C#在Windows应用程序中只读的文本框的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文