WP7 TextBox为2个整数

时间:2013-01-23 10:35:53

标签: windows-phone-7

正如您在下面的代码中所看到的,我只想弄清楚如何判断HouseholdNoTextBox中是否有超过2个整数。

private void btnContinue1_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(HouseholdNoTextBox.Text))
        {
            textBlockError1.Visibility = Visibility.Visible;
        }
        if (//HouseholdNoTextBox.Text has > than 2 integers input)
        {
            textBlockError1.Visibility = Visibility.Collapsed; // collapse the first error box if it is visible
            textBlockError2.Visibility = Visibility.Visible; // and make visible the second
        }
    }

2 个答案:

答案 0 :(得分:1)

试试这个

private void btnContinue1_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(HouseholdNoTextBox.Text))
        {
            textBlockError1.Visibility = Visibility.Visible;
        }
int val;
        if (HouseholdNoTextBox.Text.Length>2 && int.TryParse(HouseholdNoTextBox.Text, out val) && val > 99)
        {
            textBlockError1.Visibility = Visibility.Collapsed; // collapse the first error box if it is visible
            textBlockError2.Visibility = Visibility.Visible; // and make visible the second
        }
    }

答案 1 :(得分:0)

这是我最后的工作代码。我将+1 Cyber​​maxs回答,因为他带领我朝着正确的方向前进

private void btnContinue1_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(HouseholdNoTextBox.Text))
        {
            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.Length <= 2 && HouseholdNoTextBox.Text.Length > 0)
        {
            textBlockError1.Visibility = Visibility.Collapsed;
            textBlockError2.Visibility = Visibility.Collapsed;
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }           
    }