禁用文本输入而不使用Enabled或ReadOnly

时间:2016-06-20 13:36:18

标签: c# winforms

我有一个继承自TextBox的自定义控件。我添加了Cue属性,以便显示提示文本。为了显示提示文本,我打电话:

SendMessage(this.Handle, 0x1501, (IntPtr)1, myCueText);

我需要禁用文本框,并且提示文本应保持可见。我试过了:

textBox.ReadOnly = true;
textBox.BackColor = SystemColors.Window;

上面的代码使提示文本消失。我尝试过使用Enabled但它与上面的效果相同。

是否有其他方法可以禁用文本框并且仍然显示提示文字?

1 个答案:

答案 0 :(得分:-1)

您可以处理事件以防止内容被更改 - 也许可以尝试以下几点:

    this.textBox1.TextChanged += (object src, EventArgs a) => { TextBox tb = src as TextBox; if (tb.Focused) { tb.Text = tb.Tag as string; /* do not allow interactive changes, restore previous text */} };
    this.textBox1.Enter += (object src, EventArgs a) => { TextBox tb = src as TextBox; tb.Tag = tb.Text; /* keep a back-up of the original text on focus so we can restore it on change */ };
    this.textBox1.KeyDown += (object src, KeyEventArgs a) => { a.SuppressKeyPress = true; /* explicitly prevent keystrokes to avoid any flicker - might want to tweak this to allow cursor and related keys */};