Solved

How to switch between forms?

Posted on 2008-09-29
6
458 Views
Last Modified: 2012-06-22
Hello,

I have a project, which used to start at Form1.  Recently I was asked to create a read only version of the system (which is called ReadOnlyForm), and a login box (LoginForm) which will check permissions from the database, and depending on the user either show the ReadOnlyForm or Form1.  

I don't know how to properly switch control from the LoginForm to either ReadOnlyForm or Form1.  I've tried (from LoginForm):
     Form1.Show()
     Me.Close()

This causes problems though because it immediately closes both windows.  

I tried:
     Me.Hide()
     Form1.ShowDialog()

This works, but when I close Form1 the hidden LoginForm is still running and the program doesn't shut down.  

What is the best way to take care of this?  Ideally, I would close the LoginForm, and open either Form1 or ReadOnlyForm.  Any ideas?  
0
Comment
Question by:kidl33t
6 Comments
 
LVL 11

Assisted Solution

by:ladarling
ladarling earned 100 total points
ID: 22596577
Ensure that you application is not set to close when the startup form closes. Right click on the project name in the solution explorer and look at the application tab.
0
 
LVL 3

Expert Comment

by:Diveblue
ID: 22596579
dim f as new Form1
f.Show
me.close
0
 

Author Comment

by:kidl33t
ID: 22596626
Hmmmm

It is working now, but I am not sure I am getting it to work in the best fashion.  Inside LoginForm I am instantiating FullControl and ReadOnly like this:
     Dim ReadOnly as new ReadOnlyForm
    Dim FullControl as new Form1

Then inside my permission checking I am launching them like so:

     Me.hide() '(hide LoginForm)
     FullControl.ShowDialog()
     Me.Close()

This seems to accomplish all of my goals.  I tried FullControl.Show() and Me.close() like DiveBlue suggested, but that just causes both forms to close instantly.  
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Assisted Solution

by:Diveblue
Diveblue earned 100 total points
ID: 22596697
in your application properties, change the shutdown mode to 'when last form closes'
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 22596734
Like ladarling said, you need to change the "Shutdown Mode" in your Project --> Properties.

Change it from "When startup form closes" to "When last form closes".

Then you can close the login form like you originally wanted to:

     ' ...from inside the login form...
     Form1.Show()
     Me.Close()

Alternatively, you can validate the user in the Application.Startup() event and cancel the application if it fails.  In this scenario you would leave Form1 as the "Startup Object".

See: http://msdn.microsoft.com/en-us/library/t4zch4d2(VS.80).aspx

Namespace My
 

    ' The following events are availble for MyApplication:

    ' 

    ' Startup: Raised when the application starts, before the startup form is created.

    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.

    ' UnhandledException: Raised if the application encounters an unhandled exception.

    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 

    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.

    Partial Friend Class MyApplication
 

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

            Dim login As New frmLogin

            ' validate user login ~somehow~ and cancel the main form if necessary

            If login.ShowDialog <> DialogResult.OK Then

                e.Cancel = True ' this cancels the application...Form1 will NOT show at all...

            End If

        End Sub
 

    End Class
 

End Namespace

Open in new window

0
 

Author Closing Comment

by:kidl33t
ID: 31501208
Ladarling, DiveBlue:
Thank you for your help.  I wasn't quite sure as to why you were giving me that advice, but it turned out to be correct.
Idle_Mind:
I realize you gave the same advice as Ladarling and DiveBlue, but you explained it a bit more clearly.  As such I think your answer will help others most in the future, and I am awarding you the majority of the points.
Thanks all!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now