User interface 单击小部件时GTK删除初始文本

User interface 单击小部件时GTK删除初始文本,user-interface,mono,gtk#,User Interface,Mono,Gtk#,我正在创建一个搜索栏来搜索列表。我有一个搜索查询将被输入的Gtk.Entry,它有一个intialText告诉用户在那里键入搜索查询。当用户第一次单击小部件时,我将如何删除该文本,或者是否有更好的小部件可供使用 到目前为止,我的代码是: Entry SearchText= new Entry("Search for item"); SearchText.Direction= TextDirection.Ltr; SearchText.IsEditable= true; SearchText.Se

我正在创建一个搜索栏来搜索列表。我有一个搜索查询将被输入的Gtk.Entry,它有一个intialText告诉用户在那里键入搜索查询。当用户第一次单击小部件时,我将如何删除该文本,或者是否有更好的小部件可供使用

到目前为止,我的代码是:

Entry SearchText= new Entry("Search for item");
SearchText.Direction= TextDirection.Ltr;
SearchText.IsEditable= true;
SearchText.Sensitive= true;

ContentArea.PackStart(SearchText, false, false, 2);

至少在我的gtk版本中,条目中的文本最初是选中的,因此当用户开始键入时,它会自动删除。如果这还不够,例如,您可以使用FocusInEvent清除文本,如果用户没有输入任何内容,可以选择在FocusOutEvent上重新安装它

public class FancyEntry : Entry
{
    private string _message;
    public FancyEntry(string message) : base(message)
    {
        _message = message;
        FocusInEvent += OnFocusIn;
        FocusOutEvent += OnFocusOut;
    }

    private void OnFocusIn(object sender, EventArgs args)
    {
        FocusInEvent -= OnFocusIn;
        this.Text = String.Empty;
    }

    private void OnFocusOut(object sender, EventArgs args)
    {
        if (String.IsNullOrEmpty(this.Text))
        {
            this.Text = _message;
            FocusInEvent += OnFocusIn;
        }
    }
}

正是我需要的。我试着用“激活”而不是“聚焦”。