CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2010
    Posts
    2

    Windows Form Application Problem

    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.
    Code:
            private void btnRegister_click(object sender, EventArgs e)
            {
                frmRegister objfrmRegister = new frmRegister(this);
                this.Hide();
                objfrmRegister.Show();
            }
    And my class frmRegister
    Code:
    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.

  2. #2
    Join Date
    Mar 2010
    Posts
    2

    Re: Windows Form Application Problem

    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.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Windows Form Application Problem

    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;
      }
    }


    Login:
    Code:
    publicpartialclassLogin : Form
    {
      publicstaticDialogResult ShowDlg( Form parent )
      {
        var dlg = newLogin() {Owner = parent};
        return dlg.ShowDialog();
      }
      public  Login( )
      {
        InitializeComponent( );
        textBoxUserName.Text = "CodeGuru";
        textBoxPassword.Text = "CodeGuru1";
      }
      privatevoid  btnLogin_Click( object sender, System.EventArgs e )
      {
        // Simulate login
        if( textBoxUserName.Text == "CodeGuru" 
          && textBoxPassword.Text == "CodeGuru1" )
        {
          DialogResult = DialogResult.OK;
        }
        else
        {
          DialogResult = DialogResult.Cancel;
        }
        Close( );
      }
    }



    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured