【问题标题】:How to add placeholder text to ToolStripTextBox?如何将占位符文本添加到 ToolStripTextBox?
【发布时间】:2018-11-27 19:35:55
【问题描述】:

在 WinForms 项目中,我知道如何将 placeholder text 添加到常规文本框。但 ToolStripTextBox 似乎不是常规文本框。一方面,它不公开句柄(这是通过 Win API 设置占位符文本所需要的)。

那么,如何在 ToolStripTextBox 上设置占位符文本或获取其 .Handle 属性?

【问题讨论】:

    标签: c# .net winforms toolstrip


    【解决方案1】:

    ToolStripTextBox 内部有一个ToolStripTextBoxControl,它派生自TextBox,您可以使用其TextBox 或其Control 属性访问托管控件。所以你可以写这样的代码:

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    [ToolboxBitmap(typeof(ToolStripTextBox))]
    public class MyToolStripTextBox : ToolStripTextBox
    {
        private const int EM_SETCUEBANNER = 0x1501;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SendMessage(IntPtr hWnd, int msg,
            int wParam, string lParam);
        public MyToolStripTextBox()
        {
            this.Control.HandleCreated += Control_HandleCreated;
        }
        private void Control_HandleCreated(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cueBanner))
                UpdateCueBanner();
        }
        string cueBanner;
        public string CueBanner
        {
            get { return cueBanner; }
            set
            {
                cueBanner = value;
                UpdateCueBanner();
            }
        }
        private void UpdateCueBanner()
        {
            SendMessage(this.Control.Handle, EM_SETCUEBANNER, 0, cueBanner);
        }
    }
    

    【讨论】:

    【解决方案2】:

    我自己没试过。

    Remarks 部分表明您可以直接操作TextBox 控件。

    ToolStripTextBox 是为托管在 ToolStrip 中而优化的文本框。托管控件的属性和事件的子集在 ToolStripTextBox 级别公开,但底层 TextBox 控件可通过 TextBox 属性完全访问

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 2013-01-18
      相关资源
      最近更新 更多