0

For the project I am working on, the Startup Form (called HomeForm) needs to be invisible until the user has logged in to the program. Now I have set it up so that the form is not visible by setting HomeForm.Visible = False in the Login Form's Load method.

Private Sub LoginForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    HomeForm.Visible = False
End Sub

I am trying to set it up this way so that when the user logs out, the form also disappears.

Private Sub File_Logout_Click(sender As Object, e As EventArgs) Handles File_Logout.Click
    If MessageBox.Show("Are you sure you wish to log out?", "Logout Confirmation", MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then
        LoggedIn = False
        LoginForm.ShowDialog()
    End If
End Sub

Now I am having a small issue where on start up, HomeForm quickly flashes on screen before becoming invisible. How do I stop this from happening?

I've tried setting Me.Visible = False in both the constructor and Load methods of HomeForm and that didn't accomplish anything. So I am unsure how to solve this problem.

Public Sub New()
    InitializeComponent()
    Me.Visible = False
End Sub

Private Sub HomeForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.Visible = False
    LoginForm.ShowDialog()
End Sub
Skitzafreak
  • 1,797
  • 7
  • 32
  • 51
  • Without you posting any code the readers of your question are just guessing...................... – video.baba Oct 12 '18 at 12:17
  • `WindowsState` => `Minimized` in its designer. `ShowInTaskbar = false` if required. – Jimi Oct 12 '18 at 12:20
  • @Jimi That kind of works. Implementing your suggestion, there are two problems. First, I can still alt+tab and see the `HomeForm`. Second, when I log out of the program, the Login Screen doesn't come to the front and become the current active window, even when I add `Me.BringtoFront()` to the code. – Skitzafreak Oct 12 '18 at 12:30
  • 1
    Perhaps you are making this harder than it needs to be. Just don't create the form object until you're happy. In other words, make the login form the startup form instead. You can hide it if you need it again later, when the user logs out for example and you need to close the main window. – Hans Passant Oct 12 '18 at 12:30
  • You forgot `Me.Visible = false` in `Form.Load`. But I would take the advice of Hans Passant. Unless there's something else relevant in this Form startup proc or another procedure that's hard/annoying to undo. – Jimi Oct 12 '18 at 12:34
  • Let's make it hidden in the form properties at design time (visible = False), or set the login form as startup form. – Alessandro Mandelli Oct 12 '18 at 12:35
  • https://stackoverflow.com/a/3742980/17034 – Hans Passant Oct 12 '18 at 14:21
  • @HansPassant I had actually found that post yesterday and couldn't get it to work. I was probably implementing it wrong though. – Skitzafreak Oct 12 '18 at 14:35

1 Answers1

2

The proper way to do this is to handle the Startup event of the application, perform the login there and then set e.Cancel to True if the user doesn't login successfully. You can access the appropriate code file by clicking the 'View Application Events' button on the Application page of the project properties and then create the event handler using the drop-down lists at the top of the code editor. That event handler is executed before the startup form is created and, if e.Cancel is set to True, the application will exit without ever creating the startup form at all. More details here.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • So if I implement it as per your suggestion, how would I have to change my code for when the user goes to Logout? – Skitzafreak Oct 12 '18 at 13:24
  • That depends on exactly what you want the user to see. One option might be to write a method that displays the login dialogue and returns a `Boolean` that indicates whether the login was successful or not. You can then call that method from the `Startup` event handler. You can also call it from elsewhere, so you could handle the `Click` event of a logout `Button` or the like, `Hide` the main form then call the login method. If that method returns `True` then you reset the main form and `Show` it again, otherwise you `Close` it and the app exits. – jmcilhinney Oct 12 '18 at 13:39
  • Thank you very much. This gets my program working exactly as intended – Skitzafreak Oct 12 '18 at 14:34