0

I have a custom control that inherits from TextBox. I've added a Cue property so that cue text is displayed. To display cue text I call:

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

I need to disable the textbox and the cue text should remain visible. I've tried:

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

The above code makes the cue text disappear. I've tried using Enabled but it has the same effect as above.

Is there an alternative to disable the textbox and the cue text is still shown?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • 2
    The whole point of a cue banner is to give the user a hint about what they should type into a textbox. If they can't type into your textbox, what is the point of the cue banner? If you need text to be visible inside of a disabled textbox, set its Text property and then its Enabled property. The text will appear grayed out/disabled, somewhat like the cue banner. – Cody Gray - on strike Jun 20 '16 at 14:02
  • [Here](http://stackoverflow.com/a/36534068/3110834) is an implementation of a `TextBox` which supports showing hint (or watermark or cue): **•** It also shows the hint when `MultiLine` is true. **•** It's based on handling `WM_PAINT` message and drawing the hint. So you can simply customize the hint and add some properties like hint color, or you can draw it right to left or control when to show the hint or decide when to show the hint. – Reza Aghaei Jun 20 '16 at 14:25
  • As a customization in the linked `TextBox`, instead of `!this.Focused`, you can use `(!this.Focused|| this.ReadOnly)`. This way, the hint will be shown in `ReadOnly` text box. – Reza Aghaei Jun 20 '16 at 14:27

1 Answers1

-1

You could handle events to prevent the content being changed - maybe try something along these lines :

    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 */};
Ben Jackson
  • 1,108
  • 6
  • 9