Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2006

Tip: Looking for answers? Try searching our database.

Referencing forms?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rsine - 27 Feb 2006 16:05 GMT
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
Stoitcho Goutsev (100) - 27 Feb 2006 17:46 GMT
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.

Signature

HTH
Stoitcho Goutsev (100)

>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
[quoted text clipped - 5 lines]
>
> -Thanks
rsine - 27 Feb 2006 18:15 GMT
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

> rsine,
>
[quoted text clipped - 28 lines]
> >
> > -Thanks
Stoitcho Goutsev (100) - 27 Feb 2006 18:43 GMT
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

Signature

HTH
Stoitcho Goutsev (100)

> Stoitcho,
>
[quoted text clipped - 57 lines]
>> >
>> > -Thanks
rsine - 27 Feb 2006 18:57 GMT
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

> rsine,
>
[quoted text clipped - 83 lines]
> >> >
> >> > -Thanks
Stoitcho Goutsev (100) - 27 Feb 2006 19:25 GMT
myForm is the Form2 class filed member that I told you you need to declare.
On the class level (not as a local variable) declare one

Private myForm as Form1

Signature

Stoitcho Goutsev (100)

> Goutsev,
>
[quoted text clipped - 95 lines]
>> >> >
>> >> > -Thanks

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.