2016-10-28 2 views
0
Private Sub btnConfirmInfantry_Click(sender As Object, e As EventArgs) Handles btnConfirmInfantry.Click 
    Dim input = InputBox("How many do you want to attack with?", "Choose how many Infantry:") 
    Dim infantry As Integer 
    If (input > frmMainGame.lblInfantryNumberPlayer.Text) Then 
     MessageBox.Show("Error: The inputted number has to be <= current number of infantry.") 
    Else 

     If Integer.TryParse(input, infantry) Then 
      Dim hpai = Integer.Parse(frmMainGame.lblHPAI.Text, Globalization.NumberStyles.AllowThousands, Globalization.CultureInfo.InvariantCulture) 
      frmMainGame.lblHPAI.Text = String.Format("{0:n0}", hpai - infantry * 2) 

      Dim numPlayer = frmMainGame.lblInfantryNumberPlayer.Text 'Integer.Parse(frmMainGame.lblInfantryNumberPlayer.Text) 
      frmMainGame.lblInfantryNumberPlayer.Text = (numPlayer - input).ToString("N0") 

     End If 
    End If 
End Sub 

If (input> frmMainGame.lblInfantryNumberPlayer.Text)를 얻으려고합니다. 나머지 코드 줄은 건너 뛰십시오. 하지만 문제는 lblInfantry 숫자가 수백만 개이며 첫 번째 숫자가 2뿐입니다. 따라서 3을 입력하면 msgbox 오류가 발생합니다. 난 frm .... 후 그것을 넣어 Globilization.NumberStyles.AllowThousands 시도했지만 작동하지 않았다. 어떤 제안? 감사!입력 상자 값 시스템

참고 : 입력 값을 30으로 입력하고 lblInfantry가 100,000과 같은 경우 msgbox가 실행되지 않고 Else 후에 코드가 읽혀집니다.

업데이트 :

당신은 정수로 입력 값을 변환 할 필요가
Private Sub btnConfirmInfantry_Click(sender As Object, e As EventArgs) Handles btnConfirmInfantry.Click 
      Dim input = InputBox("How many do you want to attack with?", "Choose how many Infantry:") 
    Dim infantry As Integer 

    Dim intInfanty As Integer = 0 
    Dim intInput As Integer = 0 
    Dim txt As String = frmMainGame.lblInfantryNumberPlayer.Text, intInfantry 

    If Integer.TryParse(input, intInput) AndAlso Integer.TryParse(txt, intInfantry) AndAlso (intInput > intInfantry) Then 
     If (IsNumeric(input) AndAlso IsNumeric(frmMainGame.lblInfantryNumberPlayer.Text) AndAlso CInt(input) > CInt(frmMainGame.lblInfantryNumberPlayer.Text)) Then 
      MessageBox.Show("The value can not be a letter.") 
     Else 

      If Integer.TryParse(input, infantry) Then 
       Dim hpai = Integer.Parse(frmMainGame.lblHPAI.Text, Globalization.NumberStyles.AllowThousands, Globalization.CultureInfo.InvariantCulture) 
       frmMainGame.lblHPAI.Text = String.Format("{0:n0}", hpai - infantry * 2) 

       Dim numPlayer = frmMainGame.lblInfantryNumberPlayer.Text 'Integer.Parse(frmMainGame.lblInfantryNumberPlayer.Text) 
       frmMainGame.lblInfantryNumberPlayer.Text = (numPlayer - input).ToString("N0") 

      End If 
     End If 
    Else 

     MessageBox.Show("Error: The inputted number has to be <= current number of infantry.") 
    End If 
End Sub 
+0

에서 InputBox 결과와 텍스트 속성 문자열입니다. Try to Try 그들을 평가하기 전에 숫자로 나눕니다. – LarsTech

답변

1

. 문자열에 >과 같은 비교 연산자를 사용하면 을 알파벳순으로과 비교할 수 있습니다. 예를 들어 "20000" < "3"true을 반환합니다 - 알파벳순으로는 20000이 먼저옵니다. 당신이 숫자가 아닌 값이 > 비교와 같은 else에 빠지지 않도록 If 문을 결합하려는 경우

Dim intInfanty as int = 0; 
Dim intInput as int = 0; 
Dim txt as String = frmMainGame.lblInfantryNumberPlayer.Text, intInfantry; 

If Int.TryParse(input, intInput) AndAlso Int.TryParse(txt, intInfantry) Then 
    If (intInput > intInfantry) Then 
     'Input > InfantryNumberPlayer 
    Else 
     'Input <= InfantryNumberPlayer 
    End If 
Else 
    'Skipped the > comparison because one of them wasn't a number 
    '"Please make sure you entered a valid number" 
End If 

, 당신은이 작업을 수행 할 수 있습니다

Dim intInfanty as int = 0; 
Dim intInput as int = 0; 
Dim txt as String = frmMainGame.lblInfantryNumberPlayer.Text, intInfantry; 

If Int.TryParse(input, intInput) AndAlso Int.TryParse(txt, intInfantry) AndAlso (intInput > intInfantry) Then 
    'Input > InfantryNumberPlayer 
Else 
    'Anything else happened 
    '(Input was <= InfantryNumberPlayer, OR one wasn't a number) 
End If 
+0

그게 효과가있다. 고마워요! 나는 다음 번에 이것을 염두에 둘 것이다. –

+0

문제 없습니다. 위에서 작성한 스크립트는 빠르 고 더러 웠습니다. - 숫자를 정수로 변환 할 수없는 경우 * 스크립트에서 오류가 발생합니다 *. 나는 더 적절한 해결책을 쓸 것이다. ** 편집 : ** failsafe를 추가했습니다. 값 중 하나가 숫자가 아니면 코드는 'else'경우에 해당합니다. – Santi

+0

코드에 추가 할 것입니다. 고마워요! –