Solved

How to switch between forms?

Posted on 2008-09-29
6
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

729 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