I'm having a problem with my windows form application program I am creating. The first form loads, which is a form for a user to log in, there are 2 text boxes and 4 buttons. 1 of the buttons, register, creates a new object of type frmRegister which just links to a windows form I created. When this new form loads, I hide the current one which is the login form. However, if I click the X button on frmRegister it closes the entire application. It does this even those I have a function in frmRegister that handles the formclosed event which all it does is hide the frmRegister. I'll do my best to paste code.
This is the method that handles the event from the register button on the login form being clicked.
public partial class frmRegister : Form
{
frmLogin objfrmLogin;
public frmRegister(frmLogin temp_objfrmLogin)
{
InitializeComponent();
this.objfrmLogin = temp_objfrmLogin;
}
private void btnRegister_Click(object sender, EventArgs e)
{
database db = new database();
db.registerUser(username.Text, password.Text);
this.Hide();
this.objfrmLogin.Show();
}
private void controlExit_Click(object sender, FormClosedEventArgs e)
{
this.Hide();
this.objfrmLogin.Show();
}
}
}
Keep in mind it acts the same way whether I exit out of the register form or actually invoke the method btnRegister_click through the event of my register button being clicked.
Thanks for your help, if you need more code let me know.
In other words. How do you close one form and open another without using hide. this.close and this.dispose close out the application all together, all I want to do is close the current form the person is on WITHOUT hiding it.
IMO, the easiest way to solve this problem isn't to use two forms in succession; it's to use a form and then a child form.
Simply create a MainForm this is hidden when the program starts up. In the OnLoad handler of the MainForm, display the Login Form. Check the DialogResult value of the Login form for successful login and unhide the main form. If the login wasn't successful, close the main form.
MainForm:
Code:
publicpartialclassMainForm : Form
{
public MainForm( )
{
Visible = false;
InitializeComponent( );
}
protectedoverridevoid OnLoad( EventArgs e )
{
if( DialogResult.OK != Login.ShowDlg( this ) )
{
Close( );
return;
}
Visible = true;
}
}
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.