|
-
Dec 18th, 2009, 08:58 AM
#1
Thread Starter
New Member
Hide/Show, Visible/Invisible Forms problem
Hi,
This problems is getting around the net with no proper answer:
--
I have a form (Form1 class) which has got a "next" button (like we have setup wizards) which shud take me to other form. Now that other form has two buttons , next and back , and it shud take me to the previous and the next form. And I want to preserve the states of each form.
What I did was that I instantiated the Form2 class in the click event of the next button :
me.visible = false
dim x as new form2
form2.show()
That does the job of showing the second form . In the click event (of the back button) handler of the second form , I have the following code:
Form1.ActiveForm.Visible = True
Me.Visible = False
Now , when I press the back button , the program hangs. And nothing happens.
--
Can you please explain how to hide/show forms with .NET. In VB6 it's quite easy using Show/Hide methods.
-
Dec 18th, 2009, 09:26 AM
#2
Re: Hide/Show, Visible/Invisible Forms problem
How are you showing Form1? Are you also creating a new instance of it, and showing that? Or are you simply calling "Form1.Show()"? In that case you are using the default instance, and you can simply use "Form1.Visible = True" in any instance of Form2 (or any other form for that matter).
If you are not using the default instance (so you must be creating a "New Form1" somewhere), then the easiest thing to do is to pass that instance to Form2, preferably via it's constructor.
I do see another error: you are creating a new instance of Form2, called x, and then ignore it and use the default instance of Form2 instead. Either use the default instance only, or use your new instance only, don't combine the two.
Code:
Public Class Form2
Private _form1 As Form
Public Sub New(ByVal f As Form)
_form1 = f
End Sub
Private Sub BackButtonClicked(..)
_form1.Visible = True
Me.Visible = False
End Sub
End Class
You would then show Form2 like so
Code:
Dim x As New Form2(Me)
x.Show()
'NOT Form2.Show !!
That said, if you want to create a wizard, you may want to take a look at my Wizard control (see signature). It's a control that you can drop on any form and it will behave ike a Wizard. You can add and remove pages, browse through them (even during design-time) and drag/drop controls on to them. No need to mess with multiple forms or panels; the control handles it all.
It can even extend the glass titlebar if the Aero style is enabled (Vista or higher required obviously) which makes it look exactly like a Vista wizard.
-
Dec 18th, 2009, 11:38 AM
#3
Thread Starter
New Member
Re: Hide/Show, Visible/Invisible Forms problem
If i understand you right, here's what I did:
Windows Forms Project with 2 Forms:
Form1
Form2
The task is -> closing Form1, Form2 is opening; closing Form2, Form1 is opening with only 1 instance of Form1 and Form2. The code I tried with no success:
Code:
Public Class Form1
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
Dim x As New Form2(Me)
x.Show()
'NOT Form2.Show !!
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Code:
Public Class Form2
Private _form1 As Form
Private Sub Form2_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
_form1.Visible = True 'here is the error
Me.Visible = False
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Sub New(ByVal f As Form)
_form1 = f
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
-
Dec 18th, 2009, 11:57 AM
#4
Re: Hide/Show, Visible/Invisible Forms problem
Why did you put the Form2 show code in the Form1 disposed code? Disposing a form means completely deleting it. You can no longer make it Visible after that (I think you can Show it again though). If all goes well I think your forms should not be disposed at all (except when you close the application), they should only ever be invisible.
-
Dec 18th, 2009, 12:20 PM
#5
Thread Starter
New Member
Re: Hide/Show, Visible/Invisible Forms problem
Ooops, yes, i'm closing the forms with 'x' button. And maybe if I use 'x' button the Forms are awlays disposed (the instance is not anymore live)! I'm not sure, if i can use the 'x' button and save instances of form classes and do what i want!
So, to make my question more clear:
Is it possible closing(with 'x' button) Form1, Form2 is opening; closing(with 'x' button) Form2, Form1 is opening with only 1 instance of Form1 and Form2? If it's possible, how to do that?
-
Dec 18th, 2009, 12:44 PM
#6
Re: Hide/Show, Visible/Invisible Forms problem
Take a look at the FormClosing event, and also take a look at the members of the event argument (e). You can cancel closing a form. Once canceled, you can do with it whatever you want (like hiding it).
-
Dec 18th, 2009, 02:31 PM
#7
Thread Starter
New Member
Re: Hide/Show, Visible/Invisible Forms problem
I did it:
Code:
Public Class Form1
Dim _form2 As New Form2
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Me.Hide()
_form2.Show()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Me.Name & ", " & _form2.Name
End Sub
End Class
Code:
Public Class Form2
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Me.Hide()
Form1.Show()
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Me.Name & ", " & Form1.Name
End Sub
End Class
Thanks for your help. I shall look at your Wizard control later. It seems to be a helpful tool.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|