How to check if a “.” has been input into a TextBox?
is it something like:
if (TextBox1.Text == ".")
{
//Do something...
}
because when I do this, it seems to not check what is in the TextBox1 and just continue with the next line of code
Here is my code:
private void btnContinue1_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(HouseholdNoTextBox.Text))
{
textBlockError2.Visibility = Visibility.Collapsed;
textBlockError1.Visibility = Visibility.Visible;
}
if (HouseholdNoTextBox.Text.Length >= 3)
{//checking to see if it has a length greater than 2
textBlockError1.Visibility = Visibility.Collapsed; // collapse the first error box if it is visible
textBlockError2.Visibility = Visibility.Visible; // and make visible the second
}
if (HouseholdNoTextBox.Text == ".")
{
textBlockError1.Visibility = Visibility.Visible;
}
if (HouseholdNoTextBox.Text.Length <= 2 && HouseholdNoTextBox.Text.Length > 0)
{
textBlockError1.Visibility = Visibility.Collapsed;
textBlockError2.Visibility = Visibility.Collapsed;
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
I have also tried:
if (HouseholdNoTextBox.Text.Contains("."))
{
textBlockError1.Visibility = Visibility.Visible;
}
I have also tried:
private void btnContinue1_Click(object sender, RoutedEventArgs e)
{
string houseHoldNo = HouseholdNoTextBox.Text.ToString();
if (string.IsNullOrEmpty(HouseholdNoTextBox.Text) || houseHoldNo.Contains(".")) // adding it here along with the first if statement
{
textBlockError2.Visibility = Visibility.Collapsed;
textBlockError1.Visibility = Visibility.Visible;
}
}
I have also tried:
private void btnContinue1_Click(object sender, RoutedEventArgs e)
{
string houseHoldNo = HouseholdNoTextBox.Text.ToString();
if (string.IsNullOrEmpty(HouseholdNoTextBox.Text))
{
textBlockError2.Visibility = Visibility.Collapsed;
textBlockError1.Visibility = Visibility.Visible;
}
if (houseHoldNo.Contains("."))
{
textBlockError1.Visibility = Visibility.Visible;
}
}
and this above code does the same as the other.. it skips to the last if statement.
You can do this by checking for the “.”
if(TextBox1.Text.Contains(".")){//Do Something}And then handle the event
TextBox.TextChangedand place the above code in it.