如何将占位符文本添加到ToolStripTextBox? [英] How to add placeholder text to ToolStripTextBox?

查看:213
本文介绍了如何将占位符文本添加到ToolStripTextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a WinForms project, I know how to add placeholder text to a regular textbox. But the ToolStripTextBox doesn't appear to be a regular textbox. For one, it doesn't expose the handle (which is what's required to set the placeholder text via Win API).

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

So, how do I either set the placeholder text on a ToolStripTextBox or get its .Handle property?

推荐答案

ToolStripTextBox承载 ToolStripTextBoxControl ,其内部派生自TextBox,您可以使用其

ToolStripTextBox hosts a ToolStripTextBoxControl inside which is derived from TextBox and you can access the the hosted control using its TextBox or its Control property. So you can write such code:

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);
    }
}

这篇关于如何将占位符文本添加到ToolStripTextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆