如何将占位符文本添加到ToolStripTextBox?

时间:2018-06-18 22:22:20

标签: c# .net winforms toolstrip

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

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

2 个答案:

答案 0 :(得分:2)

ToolStripTextBoxTextBox内部托管ToolStripTextBoxControl,该TextBox来自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); } } ,您可以使用其Control或其{{3}}属性访问托管控件。所以你可以编写这样的代码:

class A {
public:
    virtual ~A() {}
};

class B : public A {
public:
    virtual ~B() {}
    static B* I() { return &i_; }

protected:
    static B i_;
    explicit B() {}
};

int main() {
    A* a = B::I();
    (void)a;
    return 0;
}

Undefined symbols for architecture x86_64:
  "B::i_", referenced from:
      B::I() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

答案 1 :(得分:1)

没试过自己。

private void DelayUpdate_PushUpdate(object sender, EventArgs e) { // You would search the directory here.. // Code that you want to delay would go here. } 部分表示您可以直接操作Remarks控件。

  

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