我们如何访问表单3中的表单列表框(lb1)

时间:2014-04-24 05:51:46

标签: asp.net .net vb.net

导入System.Windows.Forms.ListBox

Public Class Form2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)    Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim mf1 As New Form3()
    Form3.Visible = True
    Me.Hide()

End Sub

Imports System.Data.OleDb

Public Class Form3

Private Class dataaccess
    Public Shared Function getconnection() As OleDbConnection
        'string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database\Database2007.accdb";
        Dim constr1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Suman\Desktop\vs ws\vb\project_sample1\Database1.accdb"
        Return New OleDbConnection(constr1)
    End Function
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim con As OleDbConnection = dataaccess.getconnection()
    Dim query As String = "SELECT * FROM Burgers"
    Dim cmd As New OleDbCommand(query, con)
    Dim da As New OleDbDataAdapter(cmd)
    Dim ds As New DataSet()
    ' Dim x As Integer
    da.Fill(ds)

    **lb1**.Items.Add(ds.Tables(0).Rows(0).ItemArray(0).ToString())

End Sub

我想将表单3数据存储到lb1 listbox中,该列表框以2

形式声明

1 个答案:

答案 0 :(得分:0)

您可以将对特定列表框或整个form2的引用传递给form3。简单的方法是将其写入表单的构造函数中。

在Form3中:

private _TargetListBox as ListBox

public sub New(ByRef TargetListBox as ListBox)
    _TargetListBox = TargetListBox
end sub 


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim con As OleDbConnection = dataaccess.getconnection()
    Dim query As String = "SELECT * FROM Burgers"
    Dim cmd As New OleDbCommand(query, con)
    Dim da As New OleDbDataAdapter(cmd)
    Dim ds As New DataSet()
    ' Dim x As Integer
    da.Fill(ds)
    _TargetListBox.Items.Add(ds.Tables(0).Rows(0).ItemArray(0).ToString())
End Sub

在Form2中:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim mf1 As New Form3(Me.lb1)
    mf1.Visible = True
    Me.Hide()

End Sub