VB - Form 2 closes automatically whenever Form 1 closes?

Tags: vb.net
Question!

So right now, I'm so frustrated because there is ONE little problem that stops me from continue working on my programm.

I made a simple Login Form where the user must type in a password and when he failed after 3 attempts, the form will close. BUT if the user enters a correct password, a button will become visible and he can Login and another Form with the main programm will open. So I want to close the Login Form when the user presses the Login button and the Form 2 will appear. Simple Code:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    logint = 1
    Form2.Show()
    Me.Close()

logint handels the attempt counter... WHATEVER.. the MAIN PROBLEM is, that whenever I press Login the Login closes instantly and the form 2 pops up, stays for like 2 Seconds and closes right after that. I searched both codes and there is no "Form2.Close() " or "Me.Close()" which could cause that error. Form 2 always exits with Code 0 (0x0). What can I do if I want just the Login Form closed and the Main form loaded?



Answers

If you right click on your project and select the Propeties voice from the Popup menu and switch to the Application page you will notice a combobox that says:

Shutdown mode = When Startup Form close.

Your startup form is the Login form, so when it closes your app terminates. You can change this settings to

Shutdown mode = When last form closes.

However, pay attention to not have orphaned forms around your application otherwise it will never stop.

A better approach is to start your application with the normal form and in its constructor execute the login form. If the login form doesn't allow your application to continue, set a global variable and Close the main form in its Load event.

By : Steve


The "using" keyword makes a member of an inherited class visible, and resolvable, in the scope of its subclass. So, to make the privately-inherited member available to B's subclasses:

class A {

protected:

    void foo() {}
};

class B : private A {

protected:

    using A::foo;
};

class C : public B {

    void bar()
    {
        foo();
    }
};


An item of type undefined (and it is a type) has no length property - only items of type string and array do. Therefore, as @redneb notes, extend your condition to check for a non-falsy value before checking length.

if (myVar && myVar.length) { ... }
By : Utkanos


This video can help you solving your question :)
By: admin