Winform中将TextBox设置为只读后再设置字体颜色的颜色怎么没有作用啊,我想让TextBox为只读、且颜色设置为Red,怎么才能设置成功( ⊙ o ⊙ )啊?
请不大侠们吝赐教!
先设置颜色,再设为只读
不可以的,你只能在 KeyDown 事件中 Handled = true 就是拦截键盘输入并终止键盘输入的显示来实现只读
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = true;
}
在TextBox的容器一层加如下代码。(估计TextBox是直接放在Form上的了)
1 namespace WindowsFormsApplication4
2 {
3 public partial class Form1 : Form
4 {
5 public Form1()
6 {
7 InitializeComponent();
8
9 this.textBox1.Select(0, 0);
10 }
11
12 protected override void WndProc(ref Message m)
13 {
14 base.WndProc(ref m);
15
16 if (m.Msg == NativeMethods.WM_CTLCOLORSTATIC)
17 {
18 Control control = Control.FromHandle(m.LParam);
19 if (control != null && control is TextBox)
20 {
21 UnsafeNativeMethods.SetTextColor(new HandleRef(control, m.WParam), ColorTranslator.ToWin32(Color.FromArgb(255, 0, 0)));
22 }
23 }
24 }
25 }
26 [System.Security.SuppressUnmanagedCodeSecurity]
27 internal class UnsafeNativeMethods
28 {
29 [DllImport("Gdi32.dll")]
30 public static extern int SetTextColor(HandleRef hdc, int ColorRef);
31
32 }
33
34 internal class NativeMethods
35 {
36 public const int WM_CTLCOLORSTATIC = 0x0138;
37 }
38 }