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"?
Printable View
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"?
VB Code:
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.Quote:
Originally Posted by ..:RUDI:..
Any ideas?
You need to change the start up form to form2.
Quote:
Originally Posted by fifo
VB Code:
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.
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.Quote:
Originally Posted by fifo
Or perhaps on Form2:
VB Code:
Owner.Close();
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.Quote:
Originally Posted by fifo
I got the same error as you.
Try digging into your mind :wave:
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.
then you can use the code that was first posted (slightly modified):Code:public Form newform = null;
public static void Main() {
...
since the new form is no longer a child of the loginform, it won't disappear when the loginform is closed.Code:// newform has already been declared as a public variable
newform = new Form2();
newform.Show();
this.Close();
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.
Ohh nevermind it's in the Program.cs file, d'oh.
That's great I guess!
:(Quote:
Originally Posted by tr333
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:
VB Code:
class variables { public string windowsuser; public mainos osform = new mainos(); }
In form1:
VB Code:
timer4.Enabled = false; vars.osform = new mainos(); vars.osform.Show(); this.Close();
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.Quote:
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(); }
This question in here :
It mean you creat a button in form 1 and click it to creat a form 1 and show form 2.Quote:
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"?
I think we should referent var form1 to form2, and in form 2 we could call form1.close();
better??Code:// 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;
}
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):
Code:button1_Click(Object sender, EventArgs e) {
this.close();
}
the error with this code is that you should be declaring 'newform' with the type 'Form' not the type 'Form2:Quote:
Originally Posted by RudiVisser
Code:this.Hide();
Form newform = new Form2();
newform.Show();
Sorry, I don't totally understand your idea.
Can you post whole code for this problem? Thanks
No, it's not.Quote:
Originally Posted by tr333
Follow-up: Because there was no error.. :afrog:Quote:
Originally Posted by RudiVisser
:afrog: (i thought i saw earlier in this thread that someone had an error with the code. didn't test it myself :) )Quote:
Originally Posted by RudiVisser
:cry
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!
The code is written in VB.NET, but you should be able to understand it.Quote:
Originally Posted by fifo
VB Code:
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.
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).
Code: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 :)Quote:
Originally Posted by fifo
:cry
thanks so muck, anyway