简体   繁体   中英

How to reference controls located on different Tabs VB.NET

Looking to reference a Data Grid view on another Tab that is create by code.

I want to populate the combo box with the columns of the data grid view that is in the currently selected tab. Right now i can only get the combo box to populate with the last file loaded by the user, not w/e tab is selected.

Dim mstrFirstFile As String = ""
Dim mstrFileName As String = ""
Dim mdstTableInput As New DataSet
Dim mintCounter As Integer
Dim mdgvTab As DataGridView

Public Function CreateNewDGVInsideNewPageTab() As DataGridView
    Dim tbpNewTab As New TabPage()
    tbpNewTab.Text = mstrFileName

    Dim mdgvTabControl As New DataGridView
    mdgvTabControl.Dock = DockStyle.Fill
    mdgvTabControl.Name = ("Grid" + ToString(mintCounter))

    tbpNewTab.Controls.Add(mdgvTabControl)
    tbcsource.TabPages.Add(tbpNewTab)
    tbcsource.SelectedTab = tbpNewTab
    Return mdgvTabControl
End Function

Private Sub generate()

    Dim objCLSConverter As New DataTier.clsCSVConverter
    Dim strNewXMLPath As String = ""
    Dim xmlFile As XmlReader

    If mstrFirstFile.Length > 1 Then


        strNewXMLPath = objCLSConverter.writeXML(mstrFirstFile)

        xmlFile = XmlReader.Create(strNewXMLPath, New XmlReaderSettings())

        Dim dsTemp As DataSet = New DataSet
        Dim dtTable As DataTable = Nothing

        dsTemp.ReadXml(xmlFile)
        dtTable = dsTemp.Tables(0).Copy
        dtTable.TableName = mintCounter
        mdstTableInput.Tables.Add(dtTable)

        mdgvTab = CreateNewDGVInsideNewPageTab()

        mdgvTab.DataSource = mdstTableInput.Tables(mintCounter)

        mintCounter = (mintCounter + 1)

        cbxColumnsUpdate()

        xmlFile.Close()

    End If
End Sub

Any help or if you need more of my code let me know.


The program takes any .csv or .xls file a user adds and tosses it into dataset. It adds these files onto a new code generated Tab with a data grid view inside that has the file loaded into it.

When a tab is selected a combo box will be populated with all the column names of that tabs data grid view, you can select any of the tabs and it will repopulate the combo box for that tab. You can then select from the combo box the column name and then in a text box enter w/e you are searching for in that column, its just a select where clause.

This data will then be tosses into a data grid view below for the user to see and export to excel or continue to change. This is all for general reporting purposes and massaging of data.

Right now the problem is that the combo box won't repopulated based on the tab selected.

Just an option - convert this:

Dim mdgvTab As DataGridView

to a dictionary:

Dim mdgvTabDictionary As Dictionary(Of Integer, DataGridView)

Then, inside Sub generate() you replace this:

mdgvTab = CreateNewDGVInsideNewPageTab()

to this:

Dim mdgvTab As DataGridView = CreateNewDGVInsideNewPageTab()
mdgvTabDictionary.Add(mintCounter, mdgvTab)

Now you don't need to deal with control hierarchy and find controls by name. Instead, you can find the DataGridView you need by index: mdgvTabDictionary(index) .

Don't forget to initialize mdgvTabDictionary at Sub New , Form_Load (or any other place, as applicable to your situation):

mdgvTabDictionary = New Dictionary(Of Integer, DataGridView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM