2013-06-21 2 views
0

컴파일 할 때이 오류 메시지가 나타납니다. 양식이 작동하고 내가 원하는대로 작동하지만 오류가 계속 발생합니다. ISNUMERIC() 반드시 뭔가 당신이 사용할 수있는 번호 인 경우 확인하기위한 최선의 선택이 아니다 -컴파일 오류 : Microsoft.VisualBasic.dll에서 'System.InvalidCastException'유형의 첫 번째 예외가 발생했습니다.

Option Strict On 
Option Explicit On 

Public Class frmEnterMidPoint 

    Dim Obj As New frmCustomRanges 

    Private Sub txtMidPointValue_TextChanged(sender As Object, e As EventArgs) Handles txtMidPointValue.TextChanged 

     'This event runs when the txtMidPointValue is changed. 
     'it enables the clear button if it's not empty. 

     If txtMidPointValue.Text = "" Then 

      btnClear.Enabled = False 

     Else 

      btnClear.Enabled = True 

     End If 


    End Sub 

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click 


     'This event runs when the btnOk is pressed. The procedure takes the value 
     'entered in the text box and converts to a decimal. That value 
     'is then sent to the frmCustomRanges form Mid label. The procedure 
     'also calculates the hourly equivalent. 

     'The procedure ends by closing the current form and opening 
     'the frmCustomRanges form. 

     If IsNumeric(txtMidPointValue.Text) Then 

      'Convert number entered to decimal 
      Dim decMidPointValue As Decimal = Convert.ToDecimal(txtMidPointValue.Text) 

      'Display results as dollar sign for Annual and Hourly 
      Obj.lblAnnualMid.Text = decMidPointValue.ToString("C") 
      Obj.lblHourlyMid.Text = Convert.ToDecimal(decMidPointValue/52/40).ToString("C") 

      'Close current form 
      Me.Close() 

      'Open frmCustomRanges 
      Obj.ShowDialog() 

     Else 

      MsgBox("You have entered a non-numeric value. Please check value and enter again", vbCritical, "Input Error") 


      With txtMidPointValue 
       .Text = "" 
       .Focus() 

      End With 

     End If 

    End Sub 

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click 

     'This event runs when the btnClear is clicked. 
     'The event clears the textbox and sends the foucs 
     'back to the textbox 

     With txtMidPointValue 
      .Text = "" 
      .Focus() 

     End With 

    End Sub 

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click 

     'This even runs when the btnCancel is clicked. 
     'The procedure closes the current form and opens the 
     'Custom Ranges form. 


     'Close current form 
     Me.Close() 

     'Open the Custom Ranges form 
     Obj.ShowDialog() 


    End Sub 

End Class 
+0

컴파일되지 않은 경우 어떻게 작동합니까? –

+0

당신의'Convert.ToDecimal' 중 하나가 예외를 던지고있는 것 같아요. try/catch로 래핑하여 오류의 출처를 찾아 냈습니까? – SlightlyCuban

+0

IsNumeric()을 사용할 때 발생하며 문자열은 그렇지 않습니다. 디버거가 너무 혼란 스럽습니다. 출력 창을 마우스 오른쪽 단추로 클릭하고 "예외 메시지"의 표시를 해제하십시오. –

답변

0

당신은 Decimal.TryParse를 사용할 수는 :

여기 내 소스 코드입니다.

Private Sub bnOK_Click(sender As Object, e As EventArgs) Handles bnOK.Click 
    Dim decMidPointValue As Decimal 
    If Decimal.TryParse(txtMidPointValue.Text, decMidPointValue) Then 
     ' decMidPointValue contains the parsed value. 
     Obj.lblAnnualMid.Text = decMidPointValue.ToString("C") 
     Obj.lblHourlyMid.Text = (decMidPointValue/52/40).ToString("C") 
     ' more code... 
    Else 
     ' could not be parsed as a Decimal; decMidPointValue is zero. 
     ' inform user. 
    End If 
End Sub 
+0

고맙습니다. 나는 여전히 vb.net을 통해 자신의 길을 느낀다. 이것은 매우 도움이되었다. –

0

올바르게 변환하십시오.

Dim dec As Decimal 
If Decimal.TryParse(txtMidPointValue.Text, dec) 
    'dec is now converted 
Else 
    'dec is not converted - tell the user 
End If 
관련 문제