Form interaction

P

poldoj

Hi all, I have a simple question. I have 2 form; form1 is the main form and
form2 is a secondary form. Just wondering if is possible to change the text
value of a control label placed in the form1 from form2. Example, I have put
a button inside the form2 like this:
________________
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim xform1 As Form1

xform1.Label1.Text = "test"

End Sub

___________________

This code produce an error :(

Thanks.
 
A

alantolan

The code above should produce a NullReference exception as there is
nothing ;) attached to the xForm1 variable.


You will need a reference to the main form in the 'child' form to
interact with it.

eg. (much abbreviated)


class Form1

' Create the child form with a reference to parent and display
Private Sub LaunchForm2( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Launch.Click

dim frm as new Form2(me)
frm.Show()

end sub

end class


class Form2

' store the reference to the parent
public sub new ( _
parent as Form _
)
_parent = parent

<rest of code>

end sub


' Update the parent through the reference
Private Sub UdpateForm1( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles ChangeText.Click

_parent.Label1.Text = "Hello World"

end sub

end class



hth,
Alan.
 
C

Cor Ligthert [MVP]

Poldoj,

Assuming that I understand you, than add in your main form by the instancing
of your form2
\\\
frm2.owner = me
///
Than it is in form2
\\\
Owner.Label1.Text = "test"
///
I hope this helps,

Cor
 
A

alantolan

Using the owner property is faster/easier than creating your own
association between the forms; the only caveat I would make is to be
sure that Form1 'owns' Form2

i.e.
if you minimize Form1, Form2 should be minimized as well
if you close Form1, Form2 should close as well


hth,
Alan.
 

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

Top