我正在尝试创建一个可以充当“购物清单”的程序。我已经完成了所有代码并进行了设置,但是当我尝试运行程序时,它将无法运行。我的代码中有0个错误,它也不会调试。我遇到这个问题有两个程序。这是两个程序的代码 计划1:
Public Class Form1
Dim Item As String = txtAdd.Text
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Item = Me.txtAdd.Text
Me.lstItems.Items.Add(Item)
End Sub
Private Sub btnMoveToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveToList.Click
Me.lstItems.Items.Remove(Item)
Me.lstList.Items.Add(Item)
End Sub
Private Sub btnMoveToItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveToItems.Click
Me.lstList.Items.Remove(Item)
Me.lstItems.Items.Add(Item)
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Me.lstItems.Items.Remove(Item)
End Sub
End Class
计划2:
Public Class Form1
Dim total As Double
Dim Cost As Double = Me.txtCost.Text
Dim Number As Integer = Me.txtNumber.Text
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
total = (Cost * Number)
If Int32.TryParse(Me.txtCost.Text, Me.txtNumber.Text) Then
MessageBox.Show("The total cost of your items is:" & total)
Else
MessageBox.Show("Enter correct numeric values")
End If
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
End Class
我收到一个弹出窗口,显示“创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:对象引用未设置为对象的实例。”
答案 0 :(得分:2)
Dim Item As String = txtAdd.Text
不会上课; txtAdd
在此时未提及您的文本框。您可以在New
中初始化它:
Private Item As String
Public Sub New()
Me.InitializeComponent()
Me.Item = txtAdd.Text
End Sub
你的第二个表格有同样的问题,但是看到你没有使用那里的两个字段,你不需要它们。如果它们应该反映输入,请改用属性,例如
Public ReadOnly Property Cost() As String
Get
Return Me.txtCost.Text
End Get
End Property