Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

How to switch between forms?

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
kidl33t
Asked:
kidl33t
3 Solutions
 
ladarlingCommented:
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
 
DiveblueCommented:
dim f as new Form1
f.Show
me.close
0
 
kidl33tAuthor Commented:
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
DiveblueCommented:
in your application properties, change the shutdown mode to 'when last form closes'
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
kidl33tAuthor Commented:
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

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now