我需要所有salesPerID的输入来对应salesIDListBox中的每个ID。 我然后有一个需要存储这些值的addButton。 然后另一个createButton是创建一个报告,显示每个ID及其相应的销售以及在totalSalesTextBox中输入的所有5个销售总额。
我很迷茫。我知道我的变量没有被声明,因为它们位于不同的Subs中。我只是需要一些帮助,所以我可以看到如何使这一切工作。Public Class MainForm
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
salesIDListBox.Items.Add(101)
salesIDListBox.Items.Add(112)
salesIDListBox.Items.Add(203)
salesIDListBox.Items.Add(301)
salesIDListBox.Items.Add(302)
salesIDListBox.SelectedIndex = 0
End Sub
Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
Static salesPerID(5) As Integer
Dim total As Integer
Dim subscript As Integer
subscript = salesIDListBox.SelectedIndex
salesTextBox.Text = salesPerID(subscript).ToString("C0")
Integer.TryParse(salesTextBox.Text, total)
subscript = salesIDListBox.SelectedIndex
salesPerID(subscript) += total
End Sub
Private Sub createButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createButton.Click
totalSalesTextBox.Text = salesPerID(0).ToString
totalSalesTextBox.Text = salesPerID(1).ToString
totalSalesTextBox.Text = salesPerID(2).ToString
totalSalesTextBox.Text = salesPerID(3).ToString
totalSalesTextBox.Text = salesPerID(4).ToString
totalSalesTextBox.Text = total
End Sub
End Class
答案 0 :(得分:1)
您必须在子(字段)之外声明这些变量。
Dim total As Integer
Dim subscript As Integer
Dim salesPerID(5) As Integer
Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
subscript = salesIDListBox.SelectedIndex
salesTextBox.Text = salesPerID(subscript).ToString("C0")
Integer.TryParse(salesTextBox.Text, total)
subscript = salesIDListBox.SelectedIndex
salesPerID(subscript) += total
End Sub
Private Sub createButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createButton.Click
totalSalesTextBox.Text = salesPerID(0).ToString
totalSalesTextBox.Text = salesPerID(1).ToString
totalSalesTextBox.Text = salesPerID(2).ToString
totalSalesTextBox.Text = salesPerID(3).ToString
totalSalesTextBox.Text = salesPerID(4).ToString
totalSalesTextBox.Text = total
End Sub