0

我目前正在使用 vb.net 在 Visual Basic 中设计一个应用程序。第一个表单要求输入登录信息,然后提示下一个表单选择客户。客户信息存储在一个文本文件中,该文件被放入一个数组中。接下来我有一个表单供用户显示和编辑该信息。如何在显示和编辑表单中使用已在上一个表单中创建的数组?

Private Sub frmCustomerList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sr As StreamReader = File.OpenText("customer.txt")
    Dim strLine As String

    Dim customerInfo() As String

    Do While sr.Peek <> -1
        strLine = sr.ReadLine
        customerInfo = strLine.Split("|")
        cboCustomers.Items.Add(customerInfo(0))
        customerList(count) = strLine
        count = count + 1
    Loop
End Sub



Private Sub cboCustomers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCustomers.SelectedIndexChanged
    Dim customerInfo() As String
    Dim index As Integer = cboCustomers.SelectedIndex
    Dim selectedCustomer As String = customerList(index)

    customerInfo = selectedCustomer.Split("|")

End Sub
4

1 回答 1

0

使下一个表单在构造函数中需要它:

Public Class EditCustomer

  Public Sub New(customerInfo As String())
    InitializeComponent() 'This call is required, do not remove
    'Yay! Now you have your info
  End Sub

End Class

你可以通过做类似的事情来调用它......

Dim editForm = New EditCustomerFrom(customerInfo)
editForm.Show()

或者,您可以在设置的表单上拥有一个属性。

Dim editForm = New EditCustomerFrom()
editForm.Customer = customerInfo
editForm.Show()

然后在该Load表单的事件中,您将编写显示它查看该属性的逻辑。

旁白:您应该考虑定义一个对象来保存客户信息并进行一些 JSON 或 XML 序列化以读取/写入文件,IMO。这种架构有点不好......

于 2013-10-24T03:27:00.520 回答