Switching between forms

  • Thread starter Thread starter Narayan
  • Start date Start date
N

Narayan

Hi

Has anyone actually tried out the code in the link given below.
It doesn't work for me.

\\\
Dim f2 as Form2
f2 = New Form2()
f2.Show()
Me.Close()
///

I do not think the above code can actually persistently display a form. The
variable f2 has local scope. So once the containing procedure is exited, f2
will be garbage collected. Had it been a case of ShowDialog(), it may have
worked. But here that is not the case.

Regards

Narayan



Yavuz Bogazci said:
In VB6 i switched between two forms on this way:

FORM1:
sub onButton_Click
form2.show
me.close
end sub

FORM2:

sub onButton_Click
form1.show
me.close
end sub


How can i realize this with VB.Net? When i want to show
form2 with

form2.show()
me.close()

the hole Application terminates! What must i do?

The _hole_ application... LOL.

http://www.google.de/groups?selm=u#[email protected]
 
Hi Narayan,

What does "doesn't work for me" mean? It works for me.

Regards,
Fergus
 
Hi again,

Being more specific.

If your main form is Form1 and it contains

Sub Form1_Load(...)
Dim f2 as Form2
f2 = New Form2()
f2.Show()
Me.Close()



Form1 will load, create and display Form2 and then close itself. And as
Form1 is the application's <main form>, the application will close and,
therefore, so will Form2.


If your main form is FromMain and it contains

Sub FormMain_Load(...)
Dim f1 as Form1
f1 = New Form1()
f1.Show()

and Form1 contains

Sub Form1_Load(...)
Dim f2 as Form2
f2 = New Form2()
f2.Show()
Me.Close()

FormMain will load, create and display Form1. Form1 will load, create and
display Form2 and then close itself. And as Form1 is <not> the application's
main form, the application lives on and FormMain and Form2 are still present.

Regards,
Fergus
 
Hi Fergus,

I was referring to the scenario when there are only forms and you want to
show another form after closing the first. I was referring to the link in my
earlier post viz:

http://www.google.de/groups?selm=u#[email protected]

For some reason earlier it didn't seem to work, but now for some the same
code works perfectly fine for just two forms (just Form1 and Form2 (w/o
FrmMain)). i.e

even if you have just 2 forms and instantiate and show form2 from the load
event of form1 while closing form1 itself. But startup must be from using
Sub Main as illustrated in the above.

However one question remains unanswered. In this case, doesn't the compiler
generate code to clean up form2 as it is only a local variable? How come
form2 is displayed even after the procedure has been exited?

Regards

Narayan
 
Hi,

I actually meant to say 'but now for some reason the same
code works perfectly fine for just two forms (....'

Narayan
 
Hi again Narayan,

I knew what you meant. Strangely enough, your correction actually arrived
just as I was reading that sentence! :-)

There's an excellent two part article about garbage collection:
http://msdn.microsoft.com/msdnmag/issues/1100/gci/

One of the concepts is that an application has a 'set of roots' which are
top-level pointers into the managed heap. As you can imagine, a Form is pretty
top-level and should be allowed to exist even if nothing else refers to it.

Regards,
Fergus

ps. In part 2 of the article there's a link to Figure 1 which uses
JavaScript. It failed on my machine. If it fails for you too, the full link
is http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/figures.asp.
 
Hi Fergus,

Thanks for the explanation.
That was a really useful article

Best Regards

Narayan
 
Hello,

Herfried K. Wagner said:
It compiles, but it doesn't work as expected.

Ooops. I didn't see the link in the OP's posting. The code works for
me.
 
Back
Top