Re: Referencing forms? by rsine
rsine
Mon Feb 27 12:57:49 CST 2006
Goutsev,
Forgive my ignorance, but where does the Me.myForm1 come from? Do I
have to construct a form1 object called myForm1 within the constructor
of Form2?
-Thanks
Stoitcho Goutsev (100) wrote:
> rsine,
>
> You need to decalre a class field to keep the reference. form1 is a
> parameter of the constructor its scope is the constructor body (it is
> visible and has valid value only inside the constructor).
>
> Declare a class field of type Form1 and then in the constructor do:
>
> Public Sub New(form1 as Form1)
> MyBase.New()
>
> Me.myForm1 = form1
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
>
> 'Add any initialization after the InitializeComponent() call
>
> End Sub
>
> In the event handler use Me.myForm1
>
>
> --
> HTH
> Stoitcho Goutsev (100)
>
> "rsine" <rsine@stationeryhouse.com> wrote in message
> news:1141064128.704935.117390@u72g2000cwu.googlegroups.com...
> > Stoitcho,
> >
> > Can you provide an example of passing a reference to Form1 in Form2's
> > constructor? I tried doing this:
> >
> > Public Sub New(form1 as Form1)
> > MyBase.New()
> >
> > 'This call is required by the Windows Form Designer.
> > InitializeComponent()
> >
> > 'Add any initialization after the InitializeComponent() call
> >
> > End Sub
> >
> > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles Button1.Click
> >
> > form1.show
> >
> > End Sub
> >
> > But I could not reference form1 in my by button1_click event. What did
> > I do incorrectly?
> >
> > -Thanks
> >
> >
> > Stoitcho Goutsev (100) wrote:
> >> rsine,
> >>
> >> In this case when form1 calls form2's ShowDialog the execution stops on
> >> that call until the modal dialog is dismissed. Only when the dialog is
> >> dismissed the the execution continues with the isntruction following
> >> ShowDialog. This is how the modal dialogs work.
> >> Having this in mind the code for the form1 can look like:
> >>
> >> Me.Hide()
> >> form2.ShowModal()
> >> Me.Show()
> >>
> >> Usually however you don't hide the main form when showing a dialog.
> >>
> >> To answer your original question in order to reference form1 from form2
> >> code
> >> you need to pass a reference to form1 to the second form. This can be
> >> done
> >> using second form's constructor, special property, calling a method, etc.
> >> The second form need to keep this reference in a field for later use.
> >>
> >>
> >> --
> >> HTH
> >> Stoitcho Goutsev (100)
> >>
> >> "rsine" <rsine@stationeryhouse.com> wrote in message
> >> news:1141056340.543923.151660@e56g2000cwe.googlegroups.com...
> >> >I am trying to understand how to work with multiple forms. To aid in
> >> > my understanding, I created a project with two forms each with a single
> >> > button. When Form1's button is clicked, the form is hiddend via
> >> > Me.Hide and Form2 is shown using ShowDialog. When Form2's button is
> >> > clicked I want to close Form2 and show Form1. My stumbling block is
> >> > how to reference Form1. I tried Form1.Show but this does not work.
> >> > What am I doing wrong? Any help is appreciated.
> >> >
> >> > -Thanks
> >> >
> >