Closing Winforms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I am running into an issue trying to close a form from another form.

I have a main form called MainForm.

A child form is opened in the MainForm as follows:
Dim Form2 As New Form2
Form2.MdiParent = Me
Form2.Show()

Now from Form2 I am opening another form as follows:
Dim Form3 As New Form3
Form3.MdiParent = Me.MdiParent
Form3.Show()

Form3 has 1 button that when pressed I want it to close Form2.
Form3 has another button when pressed I want it to return the focus to form2.

Don't know why I am having problems with this, doesn't seem like a
compicated thing to do. Any ideas?

Thanks in advance.
 
I forgot to put another question in my original post.

I was wondering if there was a way to access functions on form2 from form3.

For example, when a button is clicked on form3, some action is performed on
form2, like a datagrid refresh for instance.

Thanks again.
 
create a holder class

public class AllForms

public Form1 as form1
public Form2 as form2

End class

put this code on each of the form :

private Forms as AllForms

now when you try to create the form add one more line..

Dim 2ndForm As New Form2
2ndForm.MdiParent = Me
Forms.Form2 = 2ndForm
2ndForm.Show()

you can Forms.Form2.Close()
so you can access Forms.Form2.MethodInForm2

do the same in form 1..
 
Thanks Alan, that worked great!!


create a holder class

public class AllForms

public Form1 as form1
public Form2 as form2

End class

put this code on each of the form :

private Forms as AllForms

now when you try to create the form add one more line..

Dim 2ndForm As New Form2
2ndForm.MdiParent = Me
Forms.Form2 = 2ndForm
2ndForm.Show()

you can Forms.Form2.Close()
so you can access Forms.Form2.MethodInForm2

do the same in form 1..
 
Back
Top