获取InvalidOperatorException在编译时未处理

时间:2010-12-12 17:26:56

标签: vb.net

在实施文本字段验证之前,所有内容都正常编译。

为什么不编译?

  

创建表单时出错。   请参阅Exception.InnerException   细节。错误是:外部   组件抛出异常。

来源:

Public Class frmAddStudent
    Dim aryData(6) As String
    Public Shared newStudentRecord As String

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = DialogResult.Cancel
    End Sub

    Private Sub btnAddStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddStudent.Click
        'place all the student field data into the array of elements
        aryData(0) = txtFirstName.Text
        aryData(1) = txtLastName.Text
        aryData(2) = txtMajor.Text
        aryData(3) = txtPhone.Text
        aryData(4) = txtEmail.Text
        aryData(5) = txtGPA.Text

        'join the array elements and place the result into a string variable
        newStudentRecord = Join(aryData, " ")

        'create a link between the form and the file with streamwriter and write to the text file
        If System.IO.File.Exists(frmMain.FILE_NAME) = True Then
            Dim objWriter As New System.IO.StreamWriter(frmMain.FILE_NAME, True)
            objWriter.WriteLine(newStudentRecord)
            objWriter.Close()
        End If
        Me.DialogResult = DialogResult.OK
    End Sub

    Private Sub txtFirstName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFirstName.Validating
        If txtFirstName.Text.Contains(" ") Or txtFirstName.Text.Length = 0 Or Not System.Text.RegularExpressions.Regex.IsMatch(txtFirstName.Text, "[a-z|A-Z]+$") Then
            ErrorProvider1.SetError(txtFirstName, "First Name can't contain spaces or be of zero length and must consist of only letters")
            e.Cancel = True
        Else
            ErrorProvider1.SetError(txtFirstName, "")
        End If
    End Sub

    Private Sub frmAddStudent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ErrorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink
    End Sub

    Private Sub txtLastName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLastName.Validating
        If txtLastName.Text.Contains(" ") Or txtLastName.Text.Length = 0 Or Not System.Text.RegularExpressions.Regex.IsMatch(txtLastName.Text, "[a-z|A-Z]+$") Then
            ErrorProvider1.SetError(txtLastName, "Last Name can't contain spaces or be of zero length and must consist of only letters")
            e.Cancel = True
        Else
            ErrorProvider1.SetError(txtLastName, "")
        End If
    End Sub

    Private Sub txtMajor_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtMajor.Validating
        If txtMajor.Text.Length > 0 Then
            If Not System.Text.RegularExpressions.Regex.IsMatch(Strings.Left(txtMajor.Text, 1), "[a-z|A-Z]+$") Then
                ErrorProvider1.SetError(txtMajor, "Major course code is incorrect")
            Else
                ErrorProvider1.SetError(txtMajor, "")
            End If
        Else
            ErrorProvider1.SetError(txtMajor, "Major cant' be zero length")
        End If
    End Sub
End Class

编辑:此处还有问题的PrtSc链接: http://img88.imageshack.us/i/imageqc.gif/

0 个答案:

没有答案