2013-01-23 51 views
0

正如你在我的下面的代碼中看到的,我只想知道如何判斷在HouseholdNoTextBox中是否有超過2個整數。WP7 TextBox to 2 Integers

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 
     } 
    } 
+0

通過說「兩個整數」你會區分01和10嗎?我的意思是將「01」視爲「兩個整數」? – dumbfingers

+0

我不打擾什麼2整數,他們把,只要他們不能夠超過2整數標記 – Simagen

+0

我相信有問題,當涉及到.Text檢查文本框中的數據時,它更喜歡工作,當我們詢問其字符串。有沒有一種方法來驗證它是否比它的STRING大於2?例如如果(string.equals(HouseHoldNoTextBox.Text == 2) – Simagen

回答

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 
     } 
    } 
+0

這段代碼檢查,看看是否文本的長度大於2? – Simagen

+0

對不起編輯,你的問題是不明確 – Cybermaxs

+0

它的好,我已經找到了解決方案感謝你的指導 – Simagen

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)); 
     }   
    }