How to close the former form?

Hi. I have built a login form which links to a main form. After check user's ID and password, i use such codes:

Dim newForm as New mainForm
mainForm.show()

Here i do not know how to close the form login form, me.close() and me.disposal() do not work well. anyone can help me thanks! 


Answer this question

How to close the former form?

  • asbuilts

    Hi David,

    just a short query with regard of your suggested code to display login form from MDI Parent form and not vice verse.

    Did i understood you correctly that to build modal Login form, one should add in MDIParent.Designer.vb the following lines of code:

    Dim loginForm As New fclsLogin()

    loginForm.ShowDialog()

    I tried this code and it works well in the run time. The login form shows first and after authentication on ok_click event it closes and opens MDI Parent and child forms. Saying this i have also to point that every time after debugging when i press on view MDIParent Design, it returns en error-warning:

    Warning 1 Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog. C:\ModellingFM\Examples\Ch17\APITotal\ParentForm\MDIParent1.Designer.vb 365 0

    I was trying to find in properties section of both login form and main form the visible property but it can't be find. Can you please tell me if this warning can affect the application deployment.



  • sagan69

    Try Form.Hide(). This may not be the most elegant of solutions.

    By the way, why is close() not working


  • Fulankazu

    Hello Joey,

    Do you really close your loginform or just hide it And on which form is the MessageLoop running (search for Application.Run)

    Greetings from the rainy Netherlands.



  • RalphTrent

    Instead of trying to dispose of the form in the constructor (bad idea), set a flag and call MainForm.Close in the Load event

  • Beverley

    since me.close() will close both login and main form , i do not know the reason
  • dmkp231

    You should not have your Login form as the first form, you should have the MainForm as the first form, then from MainForm you can add code to create this Login form from the InitializeComponent and from the login form you can then have a event handler to close the login form after user successfully login.

    Sample code

    In the MainForm

    Private Sub InitializeComponent()
            components = New System.ComponentModel.Container()
            mainMenu1 = New System.Windows.Forms.MainMenu()

            Me.Menu = mainMenu1
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
            Me.Text = "Form1"
            Me.AutoScroll = True

            Dim loginForm As New LoginForm()
            loginForm.ShowDialog()

    End Sub

    In the LoginForm add the following event handler


    Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click
            'Do login verification
            Me.Close()
    End Sub


    David



  • TLJ

    Hi David,

    I tried your suggestion to close the login form. But when Login form is closed, the MainForm shows up. Could you give me some help Thank you so much.

    -Joey


  • TommyKohler

    Thank you Alex!

    Here is my solution of the problem:



    private void MainForm_Load(object sender, EventArgs e)
    {
         if (Thread.CurrentPrincipal.Identity.IsAuthenticated == false)
         {
              this.Dispose();
         }
    }


     

  • baldrick98007

    Hello David,
    I've translated your VB code to C# for my needs. The login functionality works correctly.

    Well, I wanted to give the user an option to close the login form and exit the application, so I added a new, second button named "Cancel", next to my "Login" button. The button2_Click method is placed at the login form file, whitch is Form1.cs for me. At the MainForm.cs I've tryed any combination of:

    components.Dispose();
    Me.Dispose();



    public MainForm()
    {
        InitializeComponent();

        components = new System.ComponentModel.Container();
        MainMenu Me = new System.Windows.Forms.MainMenu();

        Form1 loginForm = new Form1();
        loginForm.ShowDialog();

       if
    (Thread.CurrentPrincipal.Identity.IsAuthenticated != true)
       {
          components.Dispose();
          Me.Dispose();
       }
    }

     



    What I got as a result was: login form is closed and the MainForm is started.

    How to make the login form (and the entire application) to be closed, and not the MainForm to be started


  • How to close the former form?