Forms & Threads

p_nivalis

Active member
Joined
Oct 1, 2004
Messages
33
Programming Experience
5-10
Hi,
I am a new VB.NET programmer . I'm having a problem with forms.
I have a client application that has two forms. The first form has the login screen and the second is the main form.
The login validation is done over the internet. If the login is successful the second form is loaded and the first one is hidden:
form2.Show
Me.Hide
The login validation takes some time (not less than 2 seconds). I have a function that checks the login. This function is called when the user clicks on Enter Button.
To make the form responsive during the checking time, I create a new thread and pass it the address of this function.
Now the problem is: if inside this function I call form2.Show - Me.Hide the application disappears and becomes a background process.
Any Ideas?
Thanks
 
for this i would use Sub Main as the starting point to the app, i would also use a boolean for valid login as well

show the login form first from sub main (i would just run it as an application thread) and once the login form is closed (just use it's me.Close method) it'll go back to Sub Main where there you can check the valid login boolean to determin if the main form should be ran (again run it as an app) and if the vairble is false, dont do anything, the whole app will close and be removed from memory once sub main hits the End Sub statement

also by doing it this way you could allow your app to easily return to the login form once the user "logs out" as well
 
Thank you JuggaloBrotha for your reply
But closing the app if the login is not successful is not suitable. What I have to do if the login was not successful is to show an error msg and wait for another login trial.
But the Sub Main seems to be a good idea. I will try it and report back the results.

Thanks
 
Here's what I think you should do. You start your app in a Main method. In that method you display a login dialogue. That dialogue has fields for user name, password and anything else you need, plus OK and Cancel buttons. When the user presses OK the login dialogue itself validates the credentials. If the login succeeds the dialogue returns OK and the Main method creates a main form. If the login fails the login dialogue pops up an error message and waits for the user to enter new credentials. This continues until the credentials are valid or the user clicks the Cancel button. If the user clicks Cancel then the dialogue returns Cancel and the Main method does NOT create a main form and allows the app to exit. The Main method would look something like this:
VB.NET:
Public Sub Main()
    Application.EnableVisualStyles()
    Application.DoEvents()

    Dim loginDlg As New LoginForm
    Dim result As DialogResult = loginDlg.ShowDialog()

    loginDlg.Dispose()

    If result = Windows.Forms.DialogResult.OK Then
        'Only create a main form if the login was successful.
        Application.Run(New MainForm)
    End If
End Sub
The login form would look something like this:
VB.NET:
Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
    If Me.Login() Then
        Me.DialogResult = Windows.Forms.DialogResult.OK
    Else
        MessageBox.Show("The credentials provided were not valid.  Please try again.", _
                        "Login Failed", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
    End If
End Sub

Private Sub cancelOperationButton_Click(ByVal sender As System.Object, _
                                        ByVal e As System.EventArgs) Handles cancelOperationButton.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub

Private Function Login() As Boolean
    'Validate the users credentials here.
    'Return True if the credentials are valid and False otherwise.        
End Function
 
Thank you JuggaloBrotha for your reply
But closing the app if the login is not successful is not suitable. What I have to do if the login was not successful is to show an error msg and wait for another login trial.
But the Sub Main seems to be a good idea. I will try it and report back the results.

Thanks

jmcilhinney's post above this explains what i was talking about
 
Back
Top