Frage

I'm creating a search bar to search through a list. I have an Gtk.Entry that the search query would be typed into that has an intialText telling the user to type the search query there. How would I go about deleting that text when the user first clicks the widget or would there be a better widget to use?

My code so far:

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

ContentArea.PackStart(SearchText, false, false, 2);
War es hilfreich?

Lösung

At least in my version of gtk# the text in the entry is initially selected so when the user starts typing it is automatically deleted. If that's not enough for you, you can for example use the FocusInEvent to clear the text, optionally re-installing it on FocusOutEvent if the user hasn't entered anything.

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;
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top