如何使用类(VB.Net)将数据从一种形式传递到另一种形式 [英] How to pass data from one form to another using a class (VB.Net)
问题描述
在我的主程序(窗体)中,我有两个列表框,一个文本框和一个按钮。
当我在每个列表框中选择两个项目并在文本框中输入一个数字时,它会被存储在数组中。我想使用类做这个。 (我刚刚问了一个问题,它现在工作得很好)。问题是我想以不同的形式显示结果。
我的类中的代码如下所示:
公共类库存
公开出售(3,4)As Integer
公开号码卖家(3)As Integer
公开号码产品(4)As Integer
Public Sub addItem 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
'在二维数组中添加
如果sellerLineInteger> = 0和productColumnInteger> = 0那么
sale(sellerLineInteger,productColumnInteger)= Decimal.Parse(my_saleTextBox。文本)
结束如果
my_saleTextBox.Clear()
my_saleTextBox.Focus()
对于sellerLineInteger = 0至3
对于productColumnInteger = 0至4
numberSellers(sellerLineInteger)+ = sale(sellerLineInteger,productColumnInteger)
下一个productColumnInteger
下一个卖家LineInteger
End Sub
Public Sub showItems ByRef my_label)
my_label.Text = numberSellers(0).ToString'使用它作为测试,看看它是否工作
End Sub
结束类
我的主窗体如下所示:
公共类showForm
公开出售(3,4)As Integer
公共编号卖家(3)As Integer
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)处理SalesByMonthToolStripMenuItem.Click
saleForm.Show()
在我的第二种形式,显示存储在数组中的结果是:
公共类saleForm
Dim StockClass As New Stocking
Public Sub saleForm_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)句柄MyBase.Load
StockClass.showItems(Label00)
'现在只使用一个标签作为测试。
End Sub
结束类别
End Sub
我测试了它,并试图看看结果是否在主窗体上显示,它是。所以我猜这个问题是因为我使用不同的形式。我认为这可能是因为我以不同的形式再次调用类,并且不保存数据。
问题是你的saleForm是实例化一个新的Stocking对象。您需要在创建saleForm期间将在主表单中创建的Stocking对象发送到新表单,或者您需要使您的主表单中的Stocking对象公开可用(通过属性)。
所以,在你的主窗体中,你可能会有这样的:
Public StockClass因为新库存
那么,因为它不被保护作为一个私有变量,你可以从你的辅助
showForm.StockClass.showItems(Label00)
当然,这种危险是将两种形式紧密地结合在一起。从长远来看,最好是在初始化过程中学习如何将在第一个表单中填充的StockClass发送到第二个表单,但是我不记得在WinForms开发中有什么帮助,对不起。 p>
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.
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.
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.
这篇关于如何使用类(VB.Net)将数据从一种形式传递到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!