0

I'm attempting (for reasons) to use a cue banner textbox in place of the standard textbox in a WinForms dropdown item.

I've gotten it to a state where I can add the new control object to the list of dropdown options, but if I do, it locks up the properties menu for not only it, but also any other options on that same dropdown 'list'. It does not have this issue if I use it elsewhere in the form - just when its used in a dropdown.

I'm posting two pieces of code below:

  1. My CueTextBox class (extend TextBox)
  2. My ToolStripCueTextBox class (derive from ToolStripControlHost for designer availability)

    **//1 - Class for box**
    public class CueTextBox : TextBox
    {
        private string mCue;
        public string Cue
        {
            get => mCue;
            set
            {
                mCue = value;
                Invalidate();
            }
        }
    
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
    
            const int WM_PAINT = 0xF;
            if (m.Msg == WM_PAINT)
            {
                if (!Focused && string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(Cue))
                {
                    using (var graphics = CreateGraphics())
                    {
                        TextRenderer.DrawText(
                            dc: graphics,
                            text: Cue,
                            font: Font,
                            bounds: ClientRectangle,
                            foreColor: SystemColors.GrayText,
                            backColor: Enabled ? BackColor : SystemColors.Control,
                            flags: TextFormatFlags.Top | TextFormatFlags.Left);
                    }
                }
            }
        } 
    
    **//2 - Class for toolstrip**
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    public class ToolStripCueTextBoxItem : ToolStripControlHost
    {
        public ToolStripCueTextBoxItem() : base(new CueTextBox())
        {
        }
    }
    

I'm new here, but any help would be greatly appreciated!

Erik B
  • 1
  • [How to add placeholder text to ToolStripTextBox?](https://stackoverflow.com/q/50918225/3110834) – Reza Aghaei Mar 12 '19 at 07:54
  • Chasing this problem down isn't that useful, you'll have to throw away this code anyway. TextBox is notorious for not using WM_PAINT consistently, a crime that goes back to 1980s hardware. So you might as well [get it right](https://stackoverflow.com/a/4902969/17034). – Hans Passant Mar 12 '19 at 07:55
  • Thanks @HansPassant - I'm pretty sure I came across your work as I was tracking down potential solutions; thanks for pointing me to a more definitive solution. – Erik B Mar 12 '19 at 17:19
  • The problem is not because of custom painting. Even if you host a normal `TextBox` in `ToolStripControlHost` without any custom paint logic the problem is still there. Try this for example `[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)] public class MyToolStripTextBox:ToolStripControlHost { public MyToolStripTextBox():base(new TextBox()) { } }` – Reza Aghaei Mar 12 '19 at 19:11
  • However in general since you want a single line text you don't need custom paint and you can send `EM_SETCUEBANNER` to `TextBox` of the standard `ToolStripTextBox` like the way suggested by Hans. In the first comment, you can find link to an implementation `ToolStripTextBox ` which shows cue banner based on `EM_SETCUEBANNER` idea. – Reza Aghaei Mar 12 '19 at 19:16
  • You can find [a post about a similar issue](https://stackoverflow.com/q/8856394/3110834) with hosting a standard `MaskedTextBox` in toolstrip. – Reza Aghaei Mar 12 '19 at 19:19
  • Thanks @RezaAghaei - hopefully I'll have time to try both approaches and get back with how each worked out for what I'm trying to do. – Erik B Mar 12 '19 at 22:21

0 Answers0