Select to view content in your preferred language

Accessing Controls in other Classes/Forms

651
3
Jump to solution
06-21-2012 10:43 AM
DanWolford
Deactivated User
I am having difficulty accessing a control property on another form.  I followied some instructions online that suggested using Get to access
control properties on other forms.  For the most part it seems to work but the same problem occurs in that I am unable to retrieve the number
of columns from an already existing and populated DataGridView (dgvDatabase) on the already existing form frmXLStoDBF.  The code below is just
a subset and only includes the relevant subroutines to this problem.

This program consists of 3 class modules, 2 of which are form classes.
Execution Order
1.) btnXLStoDBF.OnClick()         <--This executes properly and calls myfrmXLStoDBF.ReadXL()
2.) myfrmXLStoDBF.ReadXL()     <--This executes properly and populates the DataGridView control named frmXLStoDBF.dgvDatabase
                                            <--A button (btnFieldAttributes_Click is pressed to display the myfrmFieldAttributes.Show
3.) btnFieldAttributes                <--This executes properly
4.) btnGetHeadings_Click           <--Gets to this routine but does not properly read the number of columns from frmXLStoDBF.dgvDataBase

Public Class btnXLStoDBF     Inherits ESRI.ArcGIS.Desktop.AddIns.Button     'Inherits System.Windows.Forms.Form             'Inherits can appear only once within a Class statement and can only specifiy one class     Public m_myfrmXLStoDBF As frmXLStoDBF     Public ReadOnly Property myfrmXLStoDBF() As frmXLStoDBF         Get             If IsNothing(m_myfrmXLStoDBF) Then m_myfrmXLStoDBF = New frmXLStoDBF             Return m_myfrmXLStoDBF         End Get     End Property     Protected Overrides Sub OnClick()         'This is the main subroutine that is initiated upon clicking a button in ESRI ArcMap Addin         Call myfrmXLStoDBF.ReadXL()     End Sub  End Class  Public Class frmXLStoDBF     'THIS IS THE MAIN FORM CLASS - There are two Panel Controls, Two Button Controls, and a DataGridView named dgvDatabase     Inherits System.Windows.Forms.Form         Public ReadOnly Property myfrmFieldAttributes() As frmFieldAttributes         Get             If IsNothing(m_myfrmFieldAttributes) Then m_myfrmFieldAttributes = New frmFieldAttributes             Return m_myfrmFieldAttributes         End Get     End Property     Private Sub btnReadExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadExcel.Click         Call ReadXL()     End Sub     Private Sub btnFieldAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFieldAttributes.Click         myfrmFieldAttributes.Show()     End Sub         Public Sub ReadXL()         'Blah do stuff here     End Sub End Class  Public Class frmFieldAttributes     'THIS IS THE SECOND FORM CLASS - There are two Panel Controls, 6 Button Controls, and a DataGridView named dgvFieldAttributes     Inherits System.Windows.Forms.Form     Public m_myfrmXLStoDBF As frmXLStoDBF     Public ReadOnly Property myfrmXLStoDBF() As frmXLStoDBF         Get             If IsNothing(m_myfrmXLStoDBF) Then m_myfrmXLStoDBF = New frmXLStoDBF             Return m_myfrmXLStoDBF         End Get     End Property     Private Sub btnGetHeadings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetHeadings.Click         Call BlankAttributes()         Dim ncol As Integer = myfrmXLStoDBF.dgvDataBase.ColumnCount - 1             '<--Problem occurs here.  It returns 0 for ColumnCount: ncol = -1         Dim nrow As Integer = myfrmXLStoDBF.dgvDataBase.RowCount - 2                          ReDim Headings(ncol)         Dim row(4)         For i = 0 To ncol             Headings(i) = myfrmXLStoDBF.dgvDataBase.Columns(i).HeaderText             row(0) = i             row(1) = Headings(i)             dgvFields.Rows.Add(row)         Next i     End Sub End Class


Thanks

Dan
0 Kudos
1 Solution

Accepted Solutions
DanWolford
Deactivated User
I was unable to directly access the DataViewGrid control using the methods in the previous post.  I solved the problem by making some global variables (arrays et al) that I could access from other classes and subroutines.

View solution in original post

0 Kudos
3 Replies
DanWolford
Deactivated User
I was unable to directly access the DataViewGrid control using the methods in the previous post.  I solved the problem by making some global variables (arrays et al) that I could access from other classes and subroutines.
0 Kudos
LeoDonahue
Deactivated User
Public Class frmFieldAttributes
    'THIS IS THE SECOND FORM CLASS - There are two Panel Controls, 6 Button Controls, and a DataGridView named dgvFieldAttributes
    Inherits System.Windows.Forms.Form
    Public m_myfrmXLStoDBF As frmXLStoDBF
    Public ReadOnly Property myfrmXLStoDBF() As frmXLStoDBF
        Get
            If IsNothing(m_myfrmXLStoDBF) Then m_myfrmXLStoDBF = New frmXLStoDBF
            Return m_myfrmXLStoDBF
        End Get
    End Property
    Private Sub btnGetHeadings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetHeadings.Click
        Call BlankAttributes()
        Dim ncol As Integer = myfrmXLStoDBF.dgvDataBase.ColumnCount - 1             '<--Problem occurs here.  It returns 0 for ColumnCount: ncol = -1
        Dim nrow As Integer = myfrmXLStoDBF.dgvDataBase.RowCount - 2                

        ReDim Headings(ncol)
        Dim row(4)
        For i = 0 To ncol
            Headings(i) = myfrmXLStoDBF.dgvDataBase.Columns(i).HeaderText
            row(0) = i
            row(1) = Headings(i)
            dgvFields.Rows.Add(row)
        Next i
    End Sub
End Class


I've bolded the problem.  You're instantiating a new frmXLStoDBF, rather than accessing the current instance of frmXLStoDBF.
0 Kudos
LeoDonahue
Deactivated User
Wouldn't it be better to have a property in the Class frmXLStoDBF that provides read access to the DataGridView control, so that Class frmFieldAttributes has access?

In my forms that have tables, I create a "get" property to my table, but I use another class entirely to load data into that table and another class to interact with the table.

Some pseudocode:

ClassFormA :
Table myTable = new Table()
property: getMyTable()

Class2 :
ClassFormA frmA = new ClassFormA()
frmA.getMyTable.getColumnCount()

etc...
0 Kudos