AnsweredAssumed Answered

Accessing Controls in other Classes/Forms

Question asked by dwolford on Jun 21, 2012
Latest reply on Jul 9, 2012 by ldonahue
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

Outcomes