Question Check on app startup is login is needed else start mainform

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
Hi guy's,

I have a question.

I'm creating an app, with a splashscreen, loginform (just enter a name) and a mainform.
With I use my.settings.

Now here comes the issue I run into.

The splashscreen should check (onload) if the my.settings.user contains any info, if it does, the mainform should be shown, if not the loginform should be shown. On my app settings I have splashscreen set to the splashscreen and the first form is set to login form.

No on the onload event of the splashscreen I have the following code:
VB.NET:
If My.Settings.User > "" Then
            Dim mainform As New Form1
            mainform.Show()
            Me.Hide()
        Else
            'show login form
        End If

The my.settings.user contains a name, but still the loginform is shown.

When I use the code (above) in the onload event of the loginform, the mainform is loaded after the splashscreen, but just as the mainform is shown, the loginform pops up.

What do I do wrong?
 
The > sign isn't going to work when comparing strings. After all how do you tell what is greater between two pieces of text?

Try one of the following:

If My.Settings.User <> "" Then
mainform etc

If not My.Settings.User = "" Then mainform etc


If My.Settings.User = "" Then
login form
else
mainform etc
end if.
 
I've tried to use one of the codes but I still have the same result, the loginform is shown even if the my.settings.user isn't empty, it should show the mainform.

Is this because I have the startup form set to loginform.

Any idea?

Thanks
 
"Is this because I have the startup form set to loginform."

That would make sense.
Why don't you put it in the startup subroutine of the login form? If the login form is told to close while still loading, it will never be shown. It will just execute the preceding statements such as loading your main form and then exit.
 
in mainForm_load

if my.settings.user = "" then
loginForm.SHOWDIALOG
end if

if you show dialog it will show the login form before the mainform is loaded and wait until the dialog is closed
 
If you wanted to, here's another alternative/idea:
you could uncheck(disable) the "Enable Application Framework" in the project properties and set the "Startup object" as Sub Main(), then put the code in Main() and handle the splash screen and stuff yourself.

so for example::

    Sub main()
        Dim splash As New SplashScreen1
        splash.Show()

        Prepare() 'you may want to run this on a different thread than the splash

        splash.Close()

        Dim startupForm As Form = New Form2

        If My.Settings.FirstRun Then
            My.Settings.FirstRun = False
            My.Settings.Save()

            startupForm = New Form1
        End If

        Application.Run(startupForm)
    End Sub

    Private Sub Prepare()
        'code to load db or whatever instead of this
        Application.DoEvents()
        Threading.Thread.Sleep(3000)
    End Sub
 
Last edited:
That would make sense.
Why don't you put it in the startup subroutine of the login form? If the login form is told to close while still loading, it will never be shown. It will just execute the preceding statements such as loading your main form and then exit.

I have tried this, but with no succes.
 
in mainForm_load

if my.settings.user = "" then
loginForm.SHOWDIALOG
end if

if you show dialog it will show the login form before the mainform is loaded and wait until the dialog is closed

I tried something similar, but didn't work. The loginform still popup even if the my.settings.user isn't empty.
 
If you wanted to, here's another alternative/idea:
you could uncheck(disable) the "Enable Application Framework" in the project properties and set the "Startup object" as Sub Main(), then put the code in Main() and handle the splash screen and stuff yourself.

so for example::

    Sub main()
        Dim splash As New SplashScreen1
        splash.Show()

        Prepare() 'you may want to run this on a different thread than the splash

        splash.Close()

        Dim startupForm As Form = New Form2

        If My.Settings.FirstRun Then
            My.Settings.FirstRun = False
            My.Settings.Save()

            startupForm = New Form1
        End If

        Application.Run(startupForm)
    End Sub

    Private Sub Prepare()
        'code to load db or whatever instead of this
        Application.DoEvents()
        Threading.Thread.Sleep(3000)
    End Sub

Does this code goes in the onload event of the mainform?
 
Does this code goes in the onload event of the mainform?

i would put it in a module.



another option if you use the"Enable application framework" and set the "Splash screen" along with the "Startup object" as a form, is to handle the form activated event instead of the load, also your going to need to change the "Shutdown mode" to "When last form closes". The other issue would be that the splash screen may not close, so just handle the Deactivated event on the splash screen and put me.close

    'in splash screen file
    Private Sub SplashScreen1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
        Me.Close()
    End Sub

    'in the form that is set as the "Startup object"
    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If My.Settings.FirstRun Then
            My.Settings.FirstRun = False

            Form2.Show()
            Me.Close()
        End If
    End Sub



And here are the code samples, one uses sub main, and the other uses the activated and deactivated event.
View attachment ChangeStartupForm_AppFramework.zip

View attachment ChangeStartupForm_NoAppFramework.zip
 
And here are the code samples, one uses sub main, and the other uses the activated and deactivated event.

Thanks for the samples, I implement it and tried it, and yes it works, but...
When my.settings.user is empty it opens the loginform, I enter my name and then the mainform shows, but it shows twice.
Here is the code I use:

On the loginform activated event
VB.NET:
If My.Settings.User = "" Then
            'MessageBox.Show(My.Settings.User)
            Me.Show()   'this is the loginform
        Else
            Form1.Show()
            Me.Close()
        End If

And on the button click event of the loginform (when entered my name) the following code:
VB.NET:
If UsrnameBox2.Text = "" Then
            MessageBox.Show("Enter your name")
        Else
            My.Settings.User = UsrnameBox2.Text
            MessageBox.Show("Welcome to Songlist Editor - " + UsrnameBox2.Text)
            My.Settings.Save()
            Dim mainForm As New Form1
            mainForm.Show()
            Me.Hide()
        End If

I think why the mainform is showing twice this only happens after I enter my name and click the login button) is because both codes above contain the mainform (form1) show part.

Thanks
 
Thanks for the samples, I implement it and tried it, and yes it works, but...
yeah, np.

I think why the mainform is showing twice this only happens after I enter my name and click the login button) is because both codes above contain the mainform (form1) show part.

that sounds about right. so just fix the logic and i think you should be fine.
 
Back
Top