在Visual Basic 2010上打开我的第三个表单时出错

时间:2014-07-28 18:26:30

标签: visual-studio-2010 windows-forms-designer invalidoperationexception

我正在尝试类似于数据库的程序,它可以添加,搜索,编辑或删除程序创建的文本文件中的记录。在处理第三个表单(Editpg)上的第二个按钮时,我收到一个错误,表示" InvalidOperationException未处理:创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:对象引用未设置为对象的实例。"现在我认为自己是Visual Basic,编程和StackOverflow的初学者,所以我不知道错误是什么以及它们从错误消息中提供的解决方案。谁能帮助我?

主菜单代码(第1版):

Public Class MainMenu

Private Sub MainMenu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim path As String = "MyTest.txt"
    Dim objReader As New System.IO.StreamReader(path)
    Dim TextLine As String = ""
    Dim record As String = ""

    If My.Computer.FileSystem.FileExists(path) Then
        Do While objReader.Peek() <> -1
            TextLine = objReader.ReadLine() & vbNewLine
            record = record + TextLine
        Loop
        objReader.Close()
        Label2.Text = record
    End If
End Sub

Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
    Label2.Top = Label2.Top - 15
End Sub

Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
    Label2.Top = Label2.Top + 15
End Sub

Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
    Addpg.Show()
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Searchpg.Show()
End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    Editpg.Show()
End Sub

Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    Dim path As String = "MyTest.txt"
    Dim objReader As New System.IO.StreamReader(path)
    Dim TextLine As String = ""
    Dim record As String = ""

    If My.Computer.FileSystem.FileExists(path) Then
        Do While objReader.Peek() <> -1
            TextLine = objReader.ReadLine() & vbNewLine
            record = record + TextLine
        Loop
        objReader.Close()
        Label2.Text = record
    End If
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Deletepg.Show()
End Sub
End Class

Editpg代码(第三种形式):(它没有显示但是有#34; Imports System.IO&#34;&amp;&#34; Imports System.Text&#34;代码顶部)

Public Class Editpg
    Dim record As String = ""
    Dim path As String = "MyTest.txt"
    Dim objReader As New System.IO.StreamReader(path)
    Dim objReader2 As New System.IO.StreamReader(path)
    Dim TextLine As String = ""
    Dim idnum As String = NumericUpDown1.Text

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim firstpoint As Integer
    Dim lastpoint As Integer
    Dim result As String = ""
    Dim cabin As String
    Dim lastn As String
    Dim firstn As String
    Dim address As String
    Dim city As String
    Dim zipcode As String

    Do While objReader.Peek() <> -1
        TextLine = objReader.ReadLine() & vbNewLine
        firstpoint = 1
        lastpoint = TextLine.IndexOf(")") - firstpoint
        result = TextLine.Substring(firstpoint, lastpoint)
        If result = idnum Then
            record = TextLine
        End If
    Loop
    objReader.Close()

    firstpoint = record.IndexOf(")") + 2
    lastpoint = record.IndexOf(".") - firstpoint
    cabin = record.Substring(firstpoint, lastpoint)
    If cabin = "C1" Then
        RadioButton1.Checked = True
    ElseIf cabin = "C2" Then
        RadioButton2.Checked = True
    ElseIf cabin = "C3" Then
        RadioButton3.Checked = True
    ElseIf cabin = "C4" Then
        RadioButton4.Checked = True
    ElseIf cabin = "C5" Then
        RadioButton5.Checked = True
    ElseIf cabin = "C6" Then
        RadioButton6.Checked = True
    ElseIf cabin = "C7" Then
        RadioButton7.Checked = True
    ElseIf cabin = "C8" Then
        RadioButton8.Checked = True
    ElseIf cabin = "C9" Then
        RadioButton9.Checked = True
    ElseIf cabin = "C10" Then
        RadioButton10.Checked = True
    ElseIf cabin = "UC1" Then
        RadioButton11.Checked = True
    ElseIf cabin = "UC2" Then
        RadioButton12.Checked = True
    ElseIf cabin = "UC3 " Then
        RadioButton13.Checked = True
    ElseIf cabin = "N/A" Then
        RadioButton14.Checked = True
    End If

    firstpoint = record.IndexOf(".") + 2
    lastpoint = record.IndexOf(",") - firstpoint
    lastn = record.Substring(firstpoint, lastpoint)
    TextBox2.Text = lastn

    firstpoint = record.IndexOf(",") + 2
    lastpoint = record.IndexOf(":") - firstpoint
    firstn = record.Substring(firstpoint, lastpoint)
    TextBox1.Text = firstn

    firstpoint = record.IndexOf(":") + 2
    lastpoint = record.IndexOf(",", record.IndexOf(":")) - firstpoint
    address = record.Substring(firstpoint, lastpoint)
    TextBox3.Text = address

    firstpoint = record.IndexOf(",", firstpoint) + 2
    lastpoint = record.IndexOf(",", firstpoint) - firstpoint
    city = record.Substring(firstpoint, lastpoint)
    TextBox4.Text = city

    firstpoint = record.IndexOf(",", firstpoint) + 2
    lastpoint = record.Length - firstpoint
    zipcode = record.Substring(firstpoint, lastpoint)
    TextBox5.Text = zipcode
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim newrecord As String = "(" + idnum + ") "
    Dim wholefile As String = ""
    Select Case newrecord
        Case RadioButton1.Checked
            newrecord = newrecord + "C1. "
        Case RadioButton2.Checked
            newrecord = newrecord + "C2. "
        Case RadioButton3.Checked
            newrecord = newrecord + "C3. "
        Case RadioButton4.Checked
            newrecord = newrecord + "C4. "
        Case RadioButton5.Checked
            newrecord = newrecord + "C5. "
        Case RadioButton6.Checked
            newrecord = newrecord + "C6. "
        Case RadioButton7.Checked
            newrecord = newrecord + "C7. "
        Case RadioButton8.Checked
            newrecord = newrecord + "C8. "
        Case RadioButton9.Checked
            newrecord = newrecord + "C9. "
        Case RadioButton10.Checked
            newrecord = newrecord + "C10. "
        Case RadioButton11.Checked
            newrecord = newrecord + "UC1. "
        Case RadioButton12.Checked
            newrecord = newrecord + "UC2. "
        Case RadioButton13.Checked
            newrecord = newrecord + "UC3. "
        Case RadioButton14.Checked
            newrecord = newrecord + "N/A. "
    End Select
    record = record + TextBox2.Text + ", " + TextBox1.Text + ": " + TextBox3.Text + ", " + TextBox4.Text + ", " + TextBox5.Text

    Do While objReader2.Peek() <> -1
        TextLine = objReader2.ReadLine() & vbNewLine
        If TextLine = record Then
            TextLine = TextLine.Replace(record, newrecord)
        End If
        wholefile = wholefile + TextLine
    Loop
    objReader2.Close()
    Dim newfile As String = "new.txt"
    Dim fs As FileStream = File.Create(newfile)
    Dim info As Byte() = New UTF8Encoding(True).GetBytes(wholefile)
    fs.Write(info, 0, info.Length)
    fs.Close()

    My.Computer.FileSystem.DeleteFile(path)

    My.Computer.FileSystem.RenameFile(newfile, path)
End Sub

结束班

0 个答案:

没有答案