Click to See Complete Forum and Search --> : Windows Form Application Problem


vecowski
March 25th, 2010, 11:25 AM
Hey,

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.

private void btnRegister_click(object sender, EventArgs e)
{
frmRegister objfrmRegister = new frmRegister(this);
this.Hide();
objfrmRegister.Show();
}


And my class frmRegister

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.

vecowski
March 25th, 2010, 01:58 PM
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.

Arjay
March 25th, 2010, 03:26 PM
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:

public partial class MainForm : Form
{
public MainForm( )
{
Visible = false;
InitializeComponent( );
}

protected override void OnLoad( EventArgs e )
{
if( DialogResult.OK != Login.ShowDlg( this ) )
{
Close( );
return;
}
Visible = true;
}
}

Login:

public partial class Login : Form
{
public static DialogResult ShowDlg( Form parent )
{
var dlg = new Login() {Owner = parent};
return dlg.ShowDialog();
}
public Login( )
{
InitializeComponent( );
textBoxUserName.Text = "CodeGuru";
textBoxPassword.Text = "CodeGuru1";
}
private void btnLogin_Click( object sender, System.EventArgs e )
{
// Simulate login
if( textBoxUserName.Text == "CodeGuru"
&& textBoxPassword.Text == "CodeGuru1" )
{
DialogResult = DialogResult.OK;
}
else
{
DialogResult = DialogResult.Cancel;
}
Close( );
}
}