By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,347 Members | 2,091 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

vb.net forms

P: 28
hello everyone,
I am new to .net. I have a form, Form1, that i call a second form, Form2 from. I want to close Form1 after i call Form2 but cant seem to do so.. can anyone help me??
Apr 25 '07 #1
Share this Question
Share on Google+
7 Replies


TRScheel
Expert 100+
P: 638
hello everyone,
I am new to .net. I have a form, Form1, that i call a second form, Form2 from. I want to close Form1 after i call Form2 but cant seem to do so.. can anyone help me??

I assume you mean that if you close form1 the app closes?
Apr 25 '07 #2

P: 28
I assume you mean that if you close form1 the app closes?

' code i have in btnNext_Click of Form1

Dim F2 As New frmTwo(Me)
F2.Show()
Me.Hide()

this works but i want to close form1 as i have ten forms i want to put this code in so on clicking next on each form a different form appears. i dont want the existing forms to linger in memory as this might cause me a lot of headache
Apr 25 '07 #3

TRScheel
Expert 100+
P: 638
Why so many forms?

Why not use the same form and just alternate states?
Apr 25 '07 #4

P: 28
Why so many forms?

Why not use the same form and just alternate states?
[scratching head] huh??

seems like a good idea.. how do i go about doing that??
Apr 25 '07 #5

TRScheel
Expert 100+
P: 638
[scratching head] huh??

seems like a good idea.. how do i go about doing that??

There are a few simple ways of doing it. You could for instance, use panels, and have the next button make the next panel in the array visible. Some psuedo code would be like this:

List<Panel> panels = new List<Panel>();
...
for(int i = 0; i < panels.Count; i++)
{
if(panels[i].visible)
{
if(i != panels.Count)
panels[i].visible = false;
panels[i +1].visible = true;
}
else
{
panels[i].visible = false;
panels[0].visible = true;
}
}
parentForm.Update();

basically, you have an array of panels that each have different views in them (what buttons/labels/etc you want on those different forms) and they are all set with their visibility property set to false except for the first one, and then your buttonNext_Click event goes through all the panels, find the one thats visible, makes it not visible, and makes the next one visible.

I havent done a desktop app in awhile (well, not one that windows drew for me, I have been doing a lot with web apps and DirectX) but I believe you have to have the parentForm call refresh or update, I cant remember the function call now so that it renders the changes you made.

Thats a simple idea of how to do it, there are other, better ways, but that'll do what you want.

The other way you could do it, which would be easier on memory, would be to instantiate your labels/buttons/etc in code, and destroy those objects as you click next. Hence you would have a function to create each different 'form' as the user sees it, and by clicking next you destroy the last instance and create the next one.

The final approach you could do would be to create your different forms ( like you are now ), and create a 'ParentForm'. You would then hide the parent form, so it is never visible, but always active. Then, it could close and open every other form in sequential order on the ButtonNext_Click call (perhaps even using the ParentForm.ShowDialog(Form yourotherforms) call). And to close your program you would just exit the ParentForm.

There are quite a few different methods to do it. If you are really... really worried about memory management, I would suggest the last two, as the first method will keep all objects in memory, just showing those that are relevant. The last one would probably be the easiest on the memory/CPU, and the middle one would probably be just easy on the memory, not so much the CPU.

Some psuedo code for the final example:

public class ParentForm : Form
{
private Form _VisibleForm;

public ParentForm()
{
this.visible = false;
_VisibleForm = new FirstStepForm();
}

public Form VisibleForm
{
get { return _VisibleForm; }
set
{
_VisibleForm.Visible = false;
_VisibleForm.Hide();

_VisibleForm = value;
_VisibleForm.Controls.GetControlByName("ButtonNext ").Click += new EventHandler(delegate() { VisibleForm = NextForm; });
_VisibleForm.Refresh();

_VisibleForm.Visible = true;
_VisibleForm.Show();
}
}

public Form NextForm
{
get
{
if(_VisibleForm.GetType() = FirstStepForm)
return new SecondStepForm();
else etc...
}
}

}

public class FirstStepForm : Form
{
// Create custom objects
}

public class SecondStepForm : Form
{
// Create custom objects
}


in this example, you create your parent form, hide it, and show the _VisibleForm after initializing it to FirstStepForm. Then you attach the button on that form with the ID ButtonNext to a delegate that changes the value of _VisibleForm and refreshes it.

I believe something like that should work. If not you could just grab the different controls and change them around... ie...

_VisibleForm.Controls = new FirstStepForm().Controls;

Then make sure to refresh/update/whatever the hell the name of the function is.

The hide/visible properties I can never remember which one you have to call. I think its hide/show to be honest though.

If you need a VB example, holler.

And if this is just like whoa, tell me, I will try to explain it a different way.
Apr 25 '07 #6

P: 28
DAMMNNN!!

I feel so small :(
the first one works perfectly for me and i dont seem to need all those forms just 4 panels !!!!!! THANKS A MILLION !!!

and worst of all it seems so easy. I am going to try the other ways u recommended just for my personally understanding Once again thanx
Apr 26 '07 #7

TRScheel
Expert 100+
P: 638
DAMMNNN!!

I feel so small :(
the first one works perfectly for me and i dont seem to need all those forms just 4 panels !!!!!! THANKS A MILLION !!!

and worst of all it seems so easy. I am going to try the other ways u recommended just for my personally understanding Once again thanx

No problem, glad to help
May 10 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.