Question Loading one form from within another

navbor

New member
Joined
Apr 14, 2009
Messages
3
Programming Experience
Beginner
Hi All,

I am starting out at VB.NET and almost immediately I am stuck on what seems to be such a simple problem....

I have created a MySQL database and have created a login form in VB.NET. The connection is fine and provided the user enters the correct login details verything works great up to this point.

The problem is,... once the user has entered his details (successfully) into the login screen the main application should start up. To do this I have created a second form with menu bar etc, etc,. However, the new form only temporarily displays and then vanishes. I have created a line break in the " Private Sub frm_Main_Load...." procedure and it is definately being called, but seems to pass control back to the calling form (login screen) instantly and then exits the application.

My apologies for such a fundamental question, but I can't seem to find the solution. I have come across a comment that says that the "frm_Main.Show()" call is "not persistent", but frankly this means little to me and searching for details regarding this comment is simply confusing.

I am pasting the code from the two forms below, any advice / help / assistance would be much appreciated:

frm_login code
VB.NET:
Imports MySql.Data.MySqlClient

Public Class frm_Login

    Private Sub cmd_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Cancel.Click
        Application.Exit()
    End Sub

    Public Sub cmd_Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Login.Click
        Dim conn = New MySqlConnection()
        conn.ConnectionString = "server=localhost;user id=" & txt_User.Text & ";password=" & txt_Password.Text & ";database=db"
        Try
            conn.Open()
            Me.Hide()
            frm_Main.Show()
            'MessageBox.Show("Connection Opened Successfully")
            'conn.Close()
        Catch myerror As MySqlException
            MessageBox.Show("Error Connecting to Database: " & myerror.Message)
        Finally
            conn.close()
            conn.Dispose()
            Application.Exit()
        End Try
    End Sub
End Class

frm_Main code
VB.NET:
Public Class frm_Main
    Private Sub frm_Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

I thought that once the control has been passed to frm_Main that the program should run in this class until the form control is closed?!?!?

Regards
Rob:confused:
 
Last edited by a moderator:
You are calling Application.Exit() in the click handler... :)
I think it would be better if you set the main form as startup form and handled the Startup application event, where you showed the login form as a dialog and cancelled the startup if login failed.
 
Thanks John

Hello John,

Thanks for the reply. I will try this (thinking about it, it makes more sense).

Regards
Rob
 
I'm having a similar issue, We're trying to migrate an application from VB6 to VB.net, and the way the flow of the application is sub main --> frmLogin --> frm welcome (from frmwelcome they could load up to 6 different forms). but when frmlogin calls frmwelcome, it is only displayed for a split second and then exits.

this is the call I make to load frmlogin from sub main:

VB.NET:
 Form_Login = New frmLogin
        Application.Run(Form_Login)

then from within frmLogin, if username and password match our database we try to load the form

VB.NET:
 Private Sub cmdLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
        'check to see if username and password can be validated
        If User_Login(txtUName.Text, txtUPass.Text) Then
            'username and password validated
            'show welcome form
            Form_Welcome = New frmWelcome
            frmWelcome.Show()
            'close the login form
            Me.Close()
        End If
    End Sub

I've tried form_welcome.showdialog(), I've tried running application.run(form_welcome).

I see that control is passed back to sub main as soon as the form is closed or hidden, so I tried run a loop in main that would just check to see what form was running, and if it wasn't loaded run the application.run(form_name), but when I do that it still only displays for a brief second.

Can anyone clue me in as to how to get this to work? because I am soo confused coming from VB6 (yeah I know I'm behind the times lol)
 
... the way the flow of the application is sub main --> frmLogin --> frm welcome.....
...
..only displays for a brief second.

Is this application type a "Windows Forms Application"? If it is just change the Shutdown mode to "When Last Form Closes" and that should do the trick.

I believe the reason why your form_welcome is closing is because you closed form_login which is the baseform that opened the second form.


I think your application type is a "console application" that starts Sub Main? You can create a custom ApplicationContext class like the following .. and do something like this?

Module Module1
    Friend ac As New appContext(New Form1)

    Sub Main()
        Windows.Forms.Application.Run(ac)
    End Sub
End Module

Public Class appContext
    Inherits Windows.Forms.ApplicationContext

    Private forms As New List(Of Windows.Forms.Form)

    Public Sub New(ByVal startUp As Windows.Forms.Form)
        MyBase.New()

        AddHandler startUp.FormClosed, AddressOf OnFormClosed
        forms.Add(startUp)

        startUp.Show()
    End Sub

    Public Sub AddShowForm(ByVal form As Windows.Forms.Form)
        AddHandler form.FormClosed, AddressOf OnFormClosed
        forms.Add(form)

        form.Show()
    End Sub

    Private Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
        forms.Remove(CType(sender, Windows.Forms.Form))

        If forms.Count = 0 Then
            MyBase.ExitThread()
        End If
    End Sub
End Class


in form1 start the new form and close the previous

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Form_Welcome As New Form2
        ac.AddShowForm(Form_Welcome)

        Me.Close()
    End Sub
End Class



Hope that helps? I'm not sure if that is the best approach but it does work.

Maybe JohnH or someone else can suggest another method?
 
I wasn't aware there was a shutdown mode, I'll have to change that. You are correct. it's a windows form application, but we use the sub main to load the database connections, check program configuration files, check command line arguments, and then load the appropriate form. I created a work around similar to what you just said, but a bit sloppier. I had a select statement with a 'Current_form as string' variable, and each form would just set the text of the current_form variable before it closed so it would loop through the select statement until it found a match or end. I like your way much better!
 
Back
Top