尝试showdialog时的vb.net错误

时间:2014-07-25 21:43:59

标签: vb.net

当我尝试显示我的某个表单的对话框时,它显示此错误所有其他表单工作完美我试图将代码复制到另一个表单中发生相同 这是错误:

  

类型' System.InvalidOperationException'的第一次机会异常。   发生在超级市场system.exe

     

其他信息:创建表单时出错。看到   Exception.InnerException以获取详细信息。错误是:对象引用   没有设置为对象的实例。

如果存在此异常的处理程序,则可以安全地继续该程序。

这是我的所有代码

Public Class farm
    Dim inifile As New IniFile(myfiles & "\system.ini")
    Dim myfiles As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\HMsystem"

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub

    Dim Count As Integer = 0
    Dim total As Long = 0

    Dim productnum As String = TextBox1.Text

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim productnum As String = TextBox1.Text
        Dim num As String = TextBox2.Text
        Dim itemname As String = inifile.GetString("productname", productnum, "غير موجود")
        Dim price As String = inifile.GetString("productprice", productnum, "غير موجود")
        ListView1.Items.Add(productnum)
        ListView1.Items(Count).SubItems.Add(itemname)
        ListView1.Items(Count).SubItems.Add(num)
        ListView1.Items(Count).SubItems.Add(price)
        total += price
        Count += 1
        Dim a As String = inifile.GetString("productquan", productnum, "0")
        Dim itemquannow As String = inifile.GetString("productquan", productnum, "0")
        If itemquannow <= 5 Then

        Else
            MsgBox("لم يبق الا 5 من هذا المنتج")
        End If
        inifile.WriteInteger("productquan", productnum, a - num)
        MsgBox("تم الاضافة")
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Label4.Text = total & "           السعر النهائي"
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListView1.Clear()
        TextBox1.Clear()
        TextBox2.Clear()
        Count = 0
        total = 0
        MsgBox("تم الشراء بنجاح")
    End Sub

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click

    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

当您收到类似的错误消息时,即&#34;创建表单时发生错误&#34;,它几乎总是意味着相同的基本问题:您有一个由于属性值而引发的事件处理程序在设计器中设置,并且事件处理程序假定用户在显示表单后进行了更改。例如,如果您在设计器中设置Text的{​​{1}}属性,则会引发TextBox事件。如果您已经处理了该事件,那么在表单初始化之前,您的事件处理程序将在向用户显示之前执行。例如,如果您认为在TextChanged中选择了某个项目,那么您就会遇到麻烦,因为没有这样的选择。

如错误消息所示,请查看ComboBox,它会告诉您原始异常的确切位置。这将告诉您哪个事件处理程序是问题,然后您可以查看该方法中的代码,并确定如果表单尚未显示将导致问题的原因。如果有疑问,请使用该事件处理程序的代码更新您的问题,并告诉我们抛出异常的位置,堆栈跟踪将告诉您。