PDA

Click to See Complete Forum and Search --> : how to close form1 when i open form2?


engjulli
Dec 14th, 2006, 03:58 AM
Can any one knows how to close form1 when i open form2, like when i heave a login form and when a log on i like to close login form!!!
what code i need to write in form1 when i kcick the button "OK"?

..:RUDI:..
Dec 14th, 2006, 08:16 AM
this.Close();
Form2 newform = new Form2();
newform.Show();


I beleive

fifo
Dec 14th, 2006, 06:39 PM
this.Close();
Form2 newform = new Form2();
newform.Show();


I beleive

It does not work. It will close both form because this code must be in Button_Click of form1 so that when you call This.Close() --> form1 will be Close with all of this code will be close, too.

Any ideas?

popskie
Dec 15th, 2006, 01:11 AM
You need to change the start up form to form2.

RudiVisser
Dec 15th, 2006, 02:45 AM
It does not work. It will close both form because this code must be in Button_Click of form1 so that when you call This.Close() --> form1 will be Close with all of this code will be close, too.

Any ideas?


this.Hide();
Form2 newform = new Form2();
newform.Show();


This way the form's still open (in theory), however calling application.exit(); to close everything, will close that form too.

fifo
Dec 15th, 2006, 02:55 AM
As for you, this form had loaded in memory and you don't use it.
I think you waste resources to store this form, anyway.

better ways??

RudiVisser
Dec 15th, 2006, 02:57 AM
As for you, this form had loaded in memory and you don't use it.
I think you waste resources to store this form, anyway.

better ways??
Not that I know of.

Or perhaps on Form2:

Owner.Close();

fifo
Dec 15th, 2006, 03:14 AM
Oh, no

I did follow your code But I got a error, you can see in my attach Image.

RudiVisser
Dec 15th, 2006, 04:02 AM
Oh, no

I did follow your code But I got a error, you can see in my attach Image.

According to some tutorials that is the preferred method.

I got the same error as you.

Try digging into your mind :wave:

tr333
Dec 15th, 2006, 08:58 AM
read this: http://vbforums.com/showthread.php?t=313050
its written for vb.net, but the ideas are the same...

basically you want to declare the reference to the new form as a public variable, something which cannot be done inside a method.


public Form newform = null;

public static void Main() {
...


then you can use the code that was first posted (slightly modified):


// newform has already been declared as a public variable
newform = new Form2();
newform.Show();
this.Close();


since the new form is no longer a child of the loginform, it won't disappear when the loginform is closed.

RudiVisser
Dec 15th, 2006, 01:18 PM
Great idea, and works perfectly.

I see why it needs to be declared as a public form, but surely there must be another way. Because it seems that for each time we need a new form in a public variable in each form when we want to close a form. :sick:

I've never really thought about it to be honest, just hidden the form and used Application.Exit(); on the close event.

RudiVisser
Dec 15th, 2006, 01:37 PM
Ohh nevermind it's in the Program.cs file, d'oh.

That's great I guess!

RudiVisser
Dec 15th, 2006, 02:21 PM
read this: http://vbforums.com/showthread.php?t=313050
its written for vb.net, but the ideas are the same...

basically you want to declare the reference to the new form as a public variable, something which cannot be done inside a method.


public Form newform = null;

public static void Main() {
...


then you can use the code that was first posted (slightly modified):


// newform has already been declared as a public variable
newform = new Form2();
newform.Show();
this.Close();


since the new form is no longer a child of the loginform, it won't disappear when the loginform is closed.
:(

I've tried placing a public declaration of a form EVERYWHERE, and it just doesn't work.

I've put it in a class, before a class, before main, and got a million errors.

It works when I put it in a class, however because I have to recreate the class in the form, it closes that form and the current form also:

class variables
{
public string windowsuser;
public mainos osform = new mainos();
}


In form1:

timer4.Enabled = false;
vars.osform = new mainos();
vars.osform.Show();
this.Close();

tr333
Dec 17th, 2006, 05:21 PM
i looked at one of my old projects as to how i did this, and it was surprisingly simple...


public System.Windows.Forms.Form LoginForm = new frmLogin // create new login form
public System.Windows.Forms.Form MainForm; // don't create yet

public static void Main() {
MainForm = new frmMain; // create new main form
Application.Run(LoginForm);
Application.Run(MainForm);
Application.Exit();
}

fifo
Dec 17th, 2006, 06:49 PM
i looked at one of my old projects as to how i did this, and it was surprisingly simple...

Code:

public System.Windows.Forms.Form LoginForm = new frmLogin // create new login form public System.Windows.Forms.Form MainForm; // don't create yet public static void Main() { MainForm = new frmMain; // create new main form Application.Run(LoginForm); Application.Run(MainForm); Application.Exit(); }


Maybe, you misunderstand this question.
This question in here :
Can any one knows how to close form1 when i open form2, like when i heave a login form and when a log on i like to close login form!!!
what code i need to write in form1 when i kcick the button "OK"?

It mean you creat a button in form 1 and click it to creat a form 1 and show form 2.

I think we should referent var form1 to form2, and in form 2 we could call form1.close();


// In class form1
public form1 myform1 = new form1;
void button1_Click(sender, e){
public form2 myfrm2 = new form2;
form2.show(myform1);
}
// In class form 2
// using declare form2(parameter form1)
public void form2(form1 frm1){
frm1.close();
// do another thing;
}


better??

tr333
Dec 17th, 2006, 10:14 PM
the code i posted previously was converted from a VB.NET project which has the code inside a module. i believe you could get the same effect by placing the code (main() method, etc) inside its own class since C# doesnt have modules.

as an addon to the previous code, the button on the login form would just close the form (and possibly process some data from the form):

button1_Click(Object sender, EventArgs e) {
this.close();
}

tr333
Dec 17th, 2006, 10:16 PM
this.Hide();
Form2 newform = new Form2();
newform.Show();


This way the form's still open (in theory), however calling application.exit(); to close everything, will close that form too.
the error with this code is that you should be declaring 'newform' with the type 'Form' not the type 'Form2:

this.Hide();
Form newform = new Form2();
newform.Show();

fifo
Dec 17th, 2006, 10:53 PM
Sorry, I don't totally understand your idea.

Can you post whole code for this problem? Thanks

RudiVisser
Dec 18th, 2006, 07:09 AM
the error with this code is that you should be declaring 'newform' with the type 'Form' not the type 'Form2:

this.Hide();
Form newform = new Form2();
newform.Show();
No, it's not.

RudiVisser
Dec 18th, 2006, 07:10 AM
No, it's not.
Follow-up: Because there was no error.. :afrog:

tr333
Dec 18th, 2006, 07:48 AM
Follow-up: Because there was no error.. :afrog:
:afrog: (i thought i saw earlier in this thread that someone had an error with the code. didn't test it myself :) )

fifo
Dec 18th, 2006, 09:22 PM
:cry

engjulli
Dec 19th, 2006, 04:55 AM
My english is pure but i will try to explain by my words!
I use VisualStudio 2003 and i am beginer in C# but this problem i solved with some tricks:
form1 i make as container so main form
in form 2 i put th login form
when i open project in form 1 load i do this:i create new item class1 in this i declare one variable string but public, this variable in begining is null and in form 1 load i import the variable(Class1 a=new Class1();)i put the "if( a.the_name_of_varable_of_class1==null) then if is that true do this Form2 f2=new Form2(); f2.show(); this.hide(); to hide form1.If is false i open the child of this MDI form. In beginin allvays the public variable is null so must go in form2.In form2 vhen i click ok if th username and pass egsist in database i put the code to show form1 and this form2 i close becasuse this don't close my aplication so is not a primary form.
i excuze for this long text but in this way i solve my problem!
Any idea!

tr333
Dec 20th, 2006, 07:04 PM
Sorry, I don't totally understand your idea.

Can you post whole code for this problem? Thanks

The code is written in VB.NET, but you should be able to understand it.


Module programModule

Friend loginForm as New frmLogin
Friend mainForm as frmMain

Friend Sub Main()

mainForm = New frmMain
Application.Run(loginForm)
Application.Run(mainForm)
Application.Exit()

End Sub

End Module


What this code does, is the Main method for the program loads up the login form. When the login form is closed, the program will continue on and load up the main form. Since the form variables are declared inside a Module instead of a class, this allows them to stay active unlike if they were declared inside a Class. This could pose a problem since C# doesn't have modules, and i'm not quite sure how a VB Module should be implemented in C#.


FYI: the code is from a small e-mail program i wrote a while ago (possibly contains bits of vb6 code here and there)...
you can take a look at the code to see if i've missed anything important here.

fifo
Dec 20th, 2006, 10:00 PM
oh, I see.

you mean Login form will be first and when you pass this form then Main Form will be shown.

BUT as for me, the Main form and Login form should be shown together. Main form being like Background. I mean Login form is being Modalless.

I think you code can not solve this problem (please see the first post again).

C# code

theMainform = new Form1();
theLoginform = new Form2();
theLoginform.show(theMainform); // <--- Show form Modalless

tr333
Dec 23rd, 2006, 07:48 AM
oh, I see.

you mean Login form will be first and when you pass this form then Main Form will be shown.

BUT as for me, the Main form and Login form should be shown together. Main form being like Background. I mean Login form is being Modalless.

I think you code can not solve this problem (please see the first post again).

C# code

theMainform = new Form1();
theLoginform = new Form2();
theLoginform.show(theMainform); // <--- Show form Modalless


sorry i can't help you further with this. hope you get the problem solved :)

fifo
Dec 25th, 2006, 07:14 PM
:cry

thanks so muck, anyway