Solved

How to switch between forms?

Posted on 2008-09-29
6
457 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

8 Experts available now in Live!

Get 1:1 Help Now