What is the exact procedure to show/hide forms?

A

Ajai Kumar .R

Hai all,

I've two or more forms on my app.
My requirement is, Have to show the first form asa the user press a button
have to hide the first form and show the second form. If the user press the
escape key on second form, this should be hidded and should show the first
form.... Can some one guide me how to achive this... (MUST USE SHOW & HIDE
FORM PROPERTIES). I tried to achive this using the below code but when i
check the Windows->Task Manager for each and every form changes it's eating
up memory and it's not getting down.

On Form1 :-
Button1_Click()
Dim Form2 as new Form2
Form2.show()

On Form2 :-
Dim Form1 as new Form1
Form1.show()

Thanks in advance.
Ajai
 
C

Cor

Hi Ajai,

A sample with even 3 forms

Hi x

\\\form 1 needs 2 buttons and a label the other forms only a button to run
it.
Option Strict On
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private WithEvents frm2 As New Form2
Private WithEvents frm3 As New Form3
Private myActiveForm As Integer
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
myActiveForm = 2
frm2.Show()
me.Hide()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
myActiveForm = 3
frm3.Show()
me.Hide()
End Sub
Private Sub frm2_VisibleChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles frm2.VisibleChanged,
frm3.VisibleChanged
Me.Show()
Me.Label1.Text = "Return form = " & myActiveForm.ToString
End Sub
End Class
///
\\\
Public Class Form2
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
End Sub
End Class
///
\\\
Public Class Form3
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
End Sub
End Class
///

I hope this helps a little bit?

Cor
 
A

Azhagan

There are several ways of going about doing this. Two of them are
here...

' Check if an instance of a form already exists... and if yes, try to
use it rather than creating a new one.

Declare form object variables at module level and try to dispose them
when closing the form.

On the Form1...

Dim objForm2 as Form2

Public Sub Button1_Click(...)
If (objForm2 is Nothing) objForm2 = new Form2()
objForm2.Show()
Me.Visible = False ' you could also call its Hide method.
End Sub

On Form2...

Dim objForm1 as Form1

Public Sub Button1_Click(...)
If (objForm1 is Nothing) objForm1 = new Form1()
objForm1.Show()
Me.Visible = False
End Sub


The second approach is to close the other form everytime you try to show
a new form. When you close a form, try to set that instance of the form to
Nothing either in the Closing event or Dispose method which will be called
by the Garbage Collector.

Hope this was helpful.

-Azhagan.
 

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