如何使用类(VB.Net)将数据从一种形式传递到另一种形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15032538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:28:49  来源:igfitidea点击:

How to pass data from one form to another using a class (VB.Net)

vb.netformsclass

提问by Exn

In my main program (form) I have two list boxes, one text box, and a button. When I pick two items in each list boxes and enter a number in the text box, it is suppsoed to be stocked in an array. I wanted to do this using a class. (I just asked a question about this, it works well now). The problem is I want to show the results in a different form. The code in my class looks like this:

在我的主程序(表单)中,我有两个列表框、一个文本框和一个按钮。当我在每个列表框中选择两个项目并在文本框中输入一个数字时,它应该被存放在一个数组中。我想用一个类来做到这一点。(我刚刚问了一个关于这个的问题,它现在运行良好)。问题是我想以不同的形式显示结果。我的类中的代码如下所示:

Public Class Stocking


Public sale(3, 4) As Integer
Public numberSellers(3) As Integer
Public numberProducts(4) As Integer


Public Sub addItem(ByRef my_sellerListBox As ListBox, ByRef my_productListBox As ListBox, ByRef my_saleTextBox As TextBox)
    Dim sellerLineInteger As Integer
    Dim productColumnInteger As Integer

    sellerLineInteger = my_sellerListBox.SelectedIndex
    productColumnInteger = my_productListBox.SelectedIndex

    ' add in two dimensional array 
    If sellerLineInteger >= 0 And productColumnInteger >= 0 Then
        sale(sellerLineInteger, productColumnInteger) = Decimal.Parse(my_saleTextBox.Text)
    End If

    my_saleTextBox.Clear()
    my_saleTextBox.Focus()

    For sellerLineInteger = 0 To 3
        For productColumnInteger = 0 To 4
            numberSellers(sellerLineInteger) += sale(sellerLineInteger, productColumnInteger)
        Next productColumnInteger
    Next sellerLineInteger

End Sub
Public Sub showItems(ByRef my_label)

    my_label.Text = numberSellers(0).ToString 'using this as a test to see if it works for now


End Sub
End Class

My main form looks like this:

我的主要形式是这样的:

Public Class showForm

Public sale(3, 4) As Integer
Public numberSellers(3) As Integer
Public numberProducts(4) As Integer

Dim StockClass As New Stocking

    Public Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click

    StockClass.addItem(sellerListBox, producttListBox, saleTextBox)

End Sub

Public Sub SalesByMonthToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalesByMonthToolStripMenuItem.Click

    saleForm.Show()

And in my second form, to show the results stocked in the array is:

在我的第二种形式中,显示存储在数组中的结果是:

Public Class saleForm

Dim StockClass As New Stocking

Public Sub saleForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    StockClass.showItems(Label00)
    'Only using one label as a test for now.

End Sub

End Class

End Sub

I tested it and tried to see if the results shows on the main form, it does. So I'm guessing the problem is because I use a different form. Also I think it might be because I call the class again in my different form and doesn't keep the data.

我测试了它并试图查看结果是否显示在主窗体上,确实如此。所以我猜问题是因为我使用了不同的形式。另外我认为这可能是因为我以不同的形式再次调用该类并且不保留数据。

采纳答案by Jason M. Batchelor

The problem is that your saleForm is instantiating a new Stocking object. You need to send the Stocking object that is created in your primary form to the new form, during the creation of saleForm, or you need to make the Stocking object in your main form publically available, perhaps through a property.

问题是您的 saleForm 正在实例化一个新的 Stocking 对象。在创建 saleForm 期间,您需要将在主表单中创建的 Stocking 对象发送到新表单,或者您需要使主表单中的 Stocking 对象公开可用,可能通过属性。

So, in your main form, you might have something like this:

所以,在你的主窗体中,你可能有这样的东西:

Public StockClass As New Stocking

then, because it's not protected as a private variable, you could access it from your secondary form through something like

然后,因为它不受保护为私有变量,所以您可以通过类似的方式从辅助表单访问它

showForm.StockClass.showItems(Label00)

The danger, of course, is that this tightly binds the two forms together. It would be better, in the long run, to learn how to send the StockClass that is populated in the first form to the second form during initialization, but I don't remember enough on WinForms development to help with that, sorry.

当然,危险在于这将两种形式紧密结合在一起。从长远来看,学习如何在初始化期间将第一个表单中填充的 StockClass 发送到第二个表单会更好,但我对 WinForms 开发的记忆不足以帮助解决这个问题,抱歉。