1

如何检查是否为“。” 已输入到文本框?

是这样的吗:

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 语句。

4

2 回答 2

1

您可以通过检查“。”来做到这一点。

if(TextBox1.Text.Contains("."))
{
//Do Something
}

然后处理事件TextBox.TextChanged并将上面的代码放入其中。

于 2013-01-23T14:25:12.003 回答
1

我会尝试this.Focus在按钮事件下键入作为您的第一个操作(这将隐藏键盘)。然后在下一行创建变量 like string tbContent = HouseholdNoTextBox.Text,最后在所有 'if' 语句中使用这个变量而不是HouseholdNoTextBox.Text. 第一次尝试在第一个'if'上设置断点并检查你的字符串是否有值('.'或其他)。如果是,则两种比较方法(==,包含())都应该在 imo 中工作。

于 2013-01-23T15:16:51.577 回答