2014-10-02 121 views
0

我試着做一個程序,當我把一個值小於0時,在標籤像「 負數不能使用我有這個視覺基本小於0

Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014 


    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click 

     Try 
      Dim intSide1 As Integer = txtSide1.Text 
      Dim intSide2 As Integer = txtSide2.Text 
      Dim intSide3 As Integer = txtSide3.Text 
      Dim intSide4 As Integer = txtSide4.Text 
      Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4 
      lblMessage.Text = intTotal.ToString("#,###.##") 
     Catch ex As Exception 
      MessageBox.Show("Incorrect Input. Enter a numeric value.") 
     End Try 

    End Sub 
End Class 

是的,我有使用嘗試捕捉方法,我需要幫助這個

+0

這將防止它甚至點擊按鈕前:http://stackoverflow.com/a/17036254/752527 – 2014-10-02 15:07:38

+0

我不知道vb.net。但你可以像這樣('Dim intSide1 As Integer = txtSide1.Text')給'Integer'分配一個'String'?我想你會需要一個'Parse'或'TryParse'。 – crashmstr 2014-10-02 15:10:17

+0

@crashmstr:使用'Option Strict Off',它會爲您執行隱式轉換。 – 2014-10-02 15:16:39

回答

0

好一個整數可以是負數...

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click 

    Try 
     Dim intSide1 As Integer = txtSide1.Text 
     Dim intSide2 As Integer = txtSide2.Text 
     Dim intSide3 As Integer = txtSide3.Text 
     Dim intSide4 As Integer = txtSide4.Text 
     Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4 
     lblMessage.Text = intTotal.ToString("#,###.##") 
     //New code 
     if intSide1 < 0 or intSide2 < 0 or intSide3 < 0 intSide4 < 0 Then 
     MessageBox.Show("Incorrect Input. Negative number not valid") 
     end if 
    Catch ex As Exception 
     MessageBox.Show("Incorrect Input. Enter a numeric value.") 
    End Try 



End Sub 
+0

感謝兄弟,幫助我弄清楚:)我做到了,但由於某種原因,它不適合我,但謝謝 – 2014-10-02 15:15:32

2

這聽起來極像是功課,但你真的需要轉換的整數正確,然後執行實際比較。

Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014 

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click 

     Try 
      ' Relies on the GetIntegerInput method to throw exceptions 
      ' for invalid entries 
      Dim intSide1 As Integer = GetIntegerInput(txtSide1.Text) 
      Dim intSide2 As Integer = GetIntegerInput(txtSide2.Text) 
      Dim intSide3 As Integer = GetIntegerInput(txtSide3.Text) 
      Dim intSide4 As Integer = GetIntegerInput(txtSide4.Text) 
      Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4 
      lblMessage.Text = intTotal.ToString("#,###.##") 
     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     End Try 

    End Sub 

    Private Function GetIntegerInput(ByVal input as String) As Integer) 
     Dim returnValue as Integer 

     ' Will attempt a proper try parse. AndAlso will short circuit 
     ' the comparison so a failure in TryParse will not perform the 
     ' the second evaluation. In either case, an actual exception is 
     ' thrown with your invalid numeric message 
     If (Not Int32.TryParse(input, returnValue) AndAlso returnValue < 0) Then 
      Throw New ArgumentException("Incorrect input. Enter a proper numeric value.") 
     End If 

     Return returnValue 
    End Function 
End Class