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 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.
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?
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.
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.
I've never really thought about it to be honest, just hidden the form and used Application.Exit(); on the close event.
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();
}
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();
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):
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.
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.