简体   繁体   中英

How to reference controls located on different Tabs (VB.NET)

I have an application written in VB.NET that reads data from a file and displays the data on the screen.

Depending on the data in the file, the program has a TabControl with up to 3 tabs and each tab in turn has a DataGridView for displaying data. For example I have a TabControl that has a tab called "Saturday" and a tab called "Sunday".

The problem I am having is that when I read data from a file, the program displays all the data on the Saturday's tab grid because I am not sure how to reference the Grid on the Sunday tab.

To add the DataGridView I am using the following code:

Grid = New DataGridView
Grid.Dock = DockStyle.Fill
Grid.Name = "Grid" & TabControl.SelectedIndex
Grid.Tag = "Grid" & TabControl.SelectedIndex 

And this is how I am reading the data in:

If reader.GetAttribute("controltype") = "Tab" Then
    SelectedTab = reader.Name
End If

If reader.Name = "cell" Then
y = y + 1
Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
End If

What I almost want to do is something like (pseudocode):

SelectedTab.Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))

However when I use the above code it complains:

'Grid' is not a member of 'String'

I hope you understand the issue. Let me know if you need clarification

Your code is a little unclear. However, it appears to me that the following line:

If reader.GetAttribute("controltype") = "Tab" Then
    SelectedTab = reader.Name
End If

is creating at least one problem. It looks like you are attempting to refer to a Tabpage control by the string representation of its name, but unless I missed something, what that line is actually doing is trying to make a tabpage control type("SelectedTab") refer to a string type. If that is the case, then you will want to try this instead:

If reader.GetAttribute("controltype") = "Tab" Then
    TabControl1.SelectedTab = TabControl1.TabPages(reader.name)
End If

It is a little hard to tell from the code you have posted, but that might get you headed down the right path.

++++++++++++

UPDATE: It appears from your code that you are naming each DGV control by appending the index of the tab on which it is located to the string "grid." I am going to assume that you are using a class member variable named "SelectedTab" to represent the current tab selected in the control. I will assume that at the top of your class you have done something like this:

'Form-or-class scoped memebr variables:
Private SelectedTab As TabPage
Private SelectedGrid As DataGridView

You should be able to refer to the active grid control using something like this:

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

    ' Set SelectedTab member variable to refer to the new selected tab page:
    SelectedTab = TabControl1.SelectedTab

    ' Set the SelectedGrid to refer to the grid control hosted on the selected tab page:
    SelectedGrid = TabControl1.SelectedTab.Controls("Grid" & TabControl1.SelectedIndex.ToString())
End Sub

From here, you should be able to use the member variable for SelectedGrid to refer to the grid present on which ever tab page is selected in your tab control.

It is challenging to address your concerns with only fragments of your code. If you have additional difficulties, please post more of your code, so we can better see what else is going on.

Hope that helps!

Okay, I would go about something like this. Maybe you can simply use a DataSet to load the XML data in one line (if they have been saved with DataSet.WriteXML before).

    Dim ds As New DataSet
    Dim p As TabPage
    Dim gv As DataGridView

    ds.ReadXml("F:\testdata.xml")
    For i As Integer = TabControl1.TabPages.Count - 1 To 0 Step -1
        TabControl1.TabPages.RemoveAt(i)
    Next

    For Each dt As DataTable In ds.Tables
        p = New TabPage(dt.TableName)
        gv = New DataGridView
        ' ... configure the gv here...
        gv.AutoGenerateColumns = True
        gv.Dock = DockStyle.Fill
        ' ...
        gv.DataSource = dt
        TabControl1.TabPages.Add(p)
        p.Controls.Add(gv)
    Next

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