[CF.Skills]Windows Mobile上设置只读但是可选择内容的textBox

本文介绍了一种在.NET Compact Framework下实现只读但可选中内容的TextBox的方法,通过处理KeyPress事件阻止输入,同时保持其他功能可用。

本文演示了如何设置一个只读,但是可以让用户选择,并可以设置其它属性的Textbox,没有用到ReadOnly属性,而是用到了另一个小技巧。
Keywords
.NET Compact Framework,Windows Mobile,TextBox,KeyPress,C#

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

代码如下:

         public  Form1()
        
{
            InitializeComponent();
            textBox1.KeyPress 
+= new KeyPressEventHandler(textBox1_KeyPress);
        }


        
void  textBox1_KeyPress( object  sender, KeyPressEventArgs e)
        
{
            e.Handled 
= true;
        }
运行效果如下:


完整的例子: 点此处下载

Enjoy!

黄季冬
解决 Windows.Forms.TextBox 文字上下不居中问题,有以下几种方法: - **添加 Panel 容器**:在 TextBox 外层添加 System.Windows.Forms.Panel,同时注意设置 TextBox 和 Panel 的 Location、Size 属性,并把 TextBox 添加到 Panel 中。这样通过 Panel 来辅助布局,可能使文字视觉上达到上下居中的效果[^1]。 - **设置 Padding 属性**:为 TextBox 增加 Padding 属性,通过调整上下的内边距,让文字在 TextBox 内上下居中。示例代码如下: ```csharp using System; using System.Windows.Forms; namespace TextBoxPaddingExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 设置 TextBox 的 Padding 属性 textBox1.Padding = new Padding(0, 10, 0, 10); } } } ``` - **自定义 TextBox 类并设置 Rect**:可以自定义一个继承自 TextBox 的类,通过处理一些消息来设置文字的显示布局。示例代码如下: ```csharp using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TextBoxEx { public partial class TextBoxEx : TextBox { // 设置 Rect 消息 private const int EM_SETRECT = 179; // 获取 Rect 消息 private const int EM_GETRECT = 178; [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, ref RECT lp); [StructLayout(LayoutKind.Sequential)] private struct RECT { public int Left; public int Top; public int Right; public int Bottom; } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); RECT rect; SendMessage(Handle, EM_GETRECT, IntPtr.Zero, ref rect); // 调整上下边距以实现垂直居中 rect.Top += 5; rect.Bottom -= 5; SendMessage(Handle, EM_SETRECT, IntPtr.Zero, ref rect); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值