Solved

How to switch between forms?

Posted on 2008-09-29
6
459 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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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