2013-09-21 40 views
0

我正在創建一個搜索欄來搜索列表。我有一個Gtk.Entry,搜索查詢將被鍵入,其中有一個intialText告訴用戶在那裏鍵入搜索查詢。當用戶第一次點擊小部件或者會有更好的小部件使用時,我將如何去刪除該文本?GTK#在點擊窗口小部件時刪除initialText

到目前爲止我的代碼:

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

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

回答

0

至少在我的版本的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; 
     } 
    } 
} 
+0

正是我需要的。我試圖使用'Activated'而不是'Focus'。 – darkling3100