如何检查是否为“。” 已输入到文本框?
是这样的吗:
if (TextBox1.Text == ".")
{
//Do something...
}
因为当我这样做时,它似乎没有检查 TextBox1 中的内容,而是继续执行下一行代码
这是我的代码:
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));
}
我也试过:
if (HouseholdNoTextBox.Text.Contains("."))
{
textBlockError1.Visibility = Visibility.Visible;
}
我也试过:
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;
}
}
我也试过:
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;
}
}
上面的代码与其他代码的作用相同。它跳到最后一个 if 语句。