首页 > 解决方案 > Winforms - 下拉项目中的提示横幅文本框问题

问题描述

我正在尝试(出于某种原因)使用提示横幅文本框代替 WinForms 下拉项中的标准文本框。

我已将其添加到可以将新控件对象添加到下拉选项列表中的状态,但如果我这样做了,它不仅会锁定它的属性菜单,还会锁定同一下拉列表中的任何其他选项'。如果我在表单的其他地方使用它,它就没有这个问题——就在它在下拉列表中使用时。

我在下面发布了两段代码:

  1. 我的 CueTextBox 类(扩展 TextBox)
  2. 我的 ToolStripCueTextBox 类(从 ToolStripControlHost 派生以供设计人员使用)

    **//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())
        {
        }
    }
    

我是新来的,但任何帮助将不胜感激!

标签: c#winforms

解决方案


推荐阅读