I'm trying to emulate on how Facebook does it search on each time a key is being entered into their search box. The problem I'm currently facing is that, is when I want to clear the search box or when I try to erase all the text manually. It stills fires the default text (Search) as a search entry. Much help is appreciated, thanks!
private void SearchTB_TextChanged(object sender, TextChangedEventArgs e)
{
int keyword_length = ((TextBox)sender).Text.Length;
if (keyword_length == 0)
{
SearchTB.Text = "Search";
myButton.Text = "Cancel";
}
// When user starts to enter in text
if (keyword_length > 0)
{
SearchTB.Text = "";
myButton.Text = "Clear";
}
// Search when 3 chars has been entered into Searchbox
if (keyword_length > 2)
{
keyword = ((TextBox)sender).Text;
SearchBook(this.keyword);
}
}
private void myButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (myButton.Text == "Clear")
{
SearchTB.Text = "Search";
myButton.Text = "Cancel";
}
else
{
if (cancelButton.Text == "Cancel")
{
Debug.WriteLine("This is cancel button");
}
}
}
As I can see, you are using WPF (no tags, but namespaces belong to WPF), so you may do that in XAML:
<Grid>
<TextBox x:Name="SearchTB" />
<TextBlock x:Name="SearchBlock" Text="Search" IsHitTestVisible="False"
VerticalAlignment="Center" />
</Grid>
And in code
if (keyword_length == 0)
{
SearchBlock.Visibility = Visibility.Visible;
myButton.Text = "Cancel";
}
else
{
SearchBlock.Visibility = Visibility.Hidden;
myButton.Text = "Clear";
}
So, you are hiding textblock, which not clickable because of IsHitTestVisible="False"
and visible only when search text is empty.