设置只读但是可选择内容的textBox

http://tech.ddvip.com   2008年09月08日    社区交流

内容摘要:本文演示了如何设置一个只读,但是可以让用户选择,并可以设置其它属性的Textbox,没有用到ReadOnly属性,而是用到了另一个小技巧。

  本文示例源代码或素材下载

  本文演示了如何设置一个只读,但是可以让用户选择,并可以设置其它属性的Textbox,没有用到ReadOnly属性,而是用到了另一个小技巧。

  Keywords

  .NET Compact Framework,Windows Mobile,TextBox,KeyPress,C#

  传统的设置为ReadOnly=true的TextBox不便于修改,而Lable又不允许用户选择上面的内容。这里利用Keypress时不能键入内容这一特点,小小的使用了一个技巧来实现只读但是其他属性又可用的TextBox

  代码如下:

设置只读但是可选择内容的textBox    publicForm1()

设置只读但是可选择内容的textBox设置只读但是可选择内容的textBox    设置只读但是可选择内容的textBox{

设置只读但是可选择内容的textBox      InitializeComponent();

设置只读但是可选择内容的textBox      textBox1.KeyPress+=newKeyPressEventHandler(textBox1_KeyPress);

设置只读但是可选择内容的textBox    }

设置只读但是可选择内容的textBox

设置只读但是可选择内容的textBox    voidtextBox1_KeyPress(objectsender,KeyPressEventArgse)

设置只读但是可选择内容的textBox设置只读但是可选择内容的textBox    设置只读但是可选择内容的textBox{

设置只读但是可选择内容的textBox      e.Handled=true;

设置只读但是可选择内容的textBox    }

  运行效果如下:

设置只读但是可选择内容的textBox

  注:附带完整的例子,可下载查看

来源:互联网    作者:黄季冬 译    责编:豆豆技术应用

正在加载评论...