Referencing another form's controls

J

John

Hi,

I'm still having trouble with referencing a ListView control (in Form1) from
a second form (fmOps). I'm getting an "Object reference not set to an
instance of an object" error, which I think is occuring at the "For Each itm
In Me.openingForm.lstWrkFiles.Items" line. Can anyone tell me what I'm
doing wrong?

(Note - I know an error will occur if Visio is not open, so for the moment,
it is.)

Thanks

John


In "Form1"
Public Sub btnNext1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext1.Click

Dim secondForm As fmOps

secondForm = New fmOps

secondForm.Show()

End Sub







In second form ("fmOps")

Inside class...

Friend openingForm As Form1



Further down...

Public Sub btProcess_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btProcess.Click

'Dim openingForm As New Form1


Try

Dim vDoc As Document 'Visio Document

Dim vWin As Window 'Visio Window

m_appVisio = GetObject(, "visio.application")

Dim itm As ListViewItem

For Each itm In Me.openingForm.lstWrkFiles.Items

vDoc = m_appVisio.Documents.Open(itm.Text)

'Whole Page View

If ckBxWholePageView.Checked = True Then

AllPageWholeView(vDoc)

End If

System.Windows.Forms.MessageBox.Show("Done", MessageBoxButtons.OK)

vDoc.Close()

Next

Catch ex As Exception

System.Windows.Forms.MessageBox.Show(ex.Message)

End Try

End Sub
 
K

Ken Tucker [MVP]

Hi,

Pass a reference to Form1 when you create the secondform


Public Sub btnNext1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext1.Click

Dim secondForm As fmOps

secondForm = New fmOps

secondForm.openingform = me

secondForm.Show()

End Sub

Ken
----------------------
Hi,

I'm still having trouble with referencing a ListView control (in Form1) from
a second form (fmOps). I'm getting an "Object reference not set to an
instance of an object" error, which I think is occuring at the "For Each itm
In Me.openingForm.lstWrkFiles.Items" line. Can anyone tell me what I'm
doing wrong?

(Note - I know an error will occur if Visio is not open, so for the moment,
it is.)

Thanks

John


In "Form1"
Public Sub btnNext1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext1.Click

Dim secondForm As fmOps

secondForm = New fmOps

secondForm.Show()

End Sub







In second form ("fmOps")

Inside class...

Friend openingForm As Form1



Further down...

Public Sub btProcess_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btProcess.Click

'Dim openingForm As New Form1


Try

Dim vDoc As Document 'Visio Document

Dim vWin As Window 'Visio Window

m_appVisio = GetObject(, "visio.application")

Dim itm As ListViewItem

For Each itm In Me.openingForm.lstWrkFiles.Items

vDoc = m_appVisio.Documents.Open(itm.Text)

'Whole Page View

If ckBxWholePageView.Checked = True Then

AllPageWholeView(vDoc)

End If

System.Windows.Forms.MessageBox.Show("Done", MessageBoxButtons.OK)

vDoc.Close()

Next

Catch ex As Exception

System.Windows.Forms.MessageBox.Show(ex.Message)

End Try

End Sub
 
H

Herfried K. Wagner [MVP]

John said:
I'm still having trouble with referencing a ListView control (in Form1)
from a second form (fmOps). I'm getting an "Object reference not set to
an instance of an object" error, which I think is occuring at the "For
Each itm In Me.openingForm.lstWrkFiles.Items" line. Can anyone tell me
what I'm doing wrong?

What you need in 'Form2' is a reference to your instance of 'Form1'. You
can pass the reference to 'Form1' in a property when showing 'Form2' from
'Form1':

Code for 'Form1':

\\\
Dim f As New Form2()
f.Form1 = Me
f.Show()
///

Code for 'Form2':

\\\
Private m_Form1 As Form1

Public Property Form1() As Form1
Get
Return m_Form1
End Get
Set(ByVal Value As Form1)
m_Form1 = Value
End Set
End Property
///

You can access 'Form1''s properties inside 'Form2' with 'Me.Form1', for
example, 'Me.Form1.Button1.Text = "Bla"'.

Providing a reference to an application's main form
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=accessmainform&lang=en>
 
J

John

Thanks guys,

I couldn't work out how to pass the reference, so that's great.

In the statement though, (form2.form1 = Me), what is actually happening? I
think I understand that the "Me" keyword identifies the form1 (?), but what
is the "form2.Form1" bit saying?

Thanks again to you both (Ken for the second time!)

Best regards

John
 
H

Herfried K. Wagner [MVP]

John said:
In the statement though, (form2.form1 = Me), what is actually happening?
I think I understand that the "Me" keyword identifies the form1 (?), but
what is the "form2.Form1" bit saying?

'Form2' has a property named 'Form1' that will hold the reference to your
instance of 'Form1'. Inside 'Form1', 'Me' refers to the currently executing
instance of 'Form1', thus assigning it to 'Form2.Form1' will cause
'Form2.Form1' to point to the instance of 'Form1'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top