Question

Okay, I am having a bit of trouble here. I am creating a log in window for an application, but I am trying to get the application to automatically log in (i.e. perform the functions that happen when the user logs in) when it starts, without showing the log in screen, if the settings already have a stored email and password. I have a notification System Tray Icon that shows when the app is running, and when the form is not visible, a balloon notification pops up so the user knows that it is still running, and click on the icon to open the log in screen.

Take a look at the following code. I know that this If Not event is being called and working correctly, because it performs everything inside the statement EXCEPT hiding the form. Why does it not change to invisible? I also tried Me.Hide, and same issue. The Balloon Notification pops up, the text boxes fill with the previously stored data...but the form stays visible...

Private Sub RadFrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'Checks settings to see if email and password have already been stored and enters them into text fields, proceeds to automatically update access list
    If Not String.IsNullOrEmpty(My.Settings.Email) And Not String.IsNullOrEmpty(My.Settings.Password) Then
        TxtEmail.Text = My.Settings.Email
        TxtPassword.Text = My.Settings.Password

        Me.Visible = False

        'Displays Balloon Tip
        ntfySystemTrayIcon.ShowBalloonTip(800)
    End If

End Sub

As an added note, I added a test button to hide the form, and it works perfectly:

Private Sub BtnHide_Click(sender As Object, e As EventArgs) Handles BtnHide.Click
    'Hides form(for testing notification tray icon and balloon tip
    Me.Visible = False
    ntfySystemTrayIcon.ShowBalloonTip(1000)
End Sub
Was it helpful?

Solution

(removed my stupid default debug instructions since they did not help at all)

Update

okay, so there were similar questions before, take a look here: C#/.NET - WinForms - Instantiate a Form without showing it

short explanation: usually something like form1.show is used, so it is always changed to visible = true after the form_load is finished.

Either use the instructed event form_shown and add the visible=false

or another user recommended to change start properties to minimized and activate to hide program in taskbar. This helps to prevent that annoying flickering. I guess after that you can change the options back.

Update 2 The following seems to work well:

Private _IsVisible As Boolean
Public Property IsVisible() As Boolean
    Get
        Return _IsVisible
    End Get
    Set(ByVal value As Boolean)
        _IsVisible = value
        If _IsVisible Then
            Me.WindowState = FormWindowState.Normal
            Me.ShowInTaskbar = True
            Me.Visible = True
            Me.Activate()
        Else
            Me.WindowState = FormWindowState.Minimized
            Me.ShowInTaskbar = False
            Me.Visible = False
        End If
    End Set
End Property

If you want to get rid of the small taskbar flickering, then change the forms property showInTaskbar. When it is changed during the form_load, then there seem to be a short movement at the taskbar.

And to make it perfect, in form.Shown add following code:

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Me.Visible = IsVisible
End Sub

now it is enough to use

IsVisible = False

in form_Load, or if you want to show it

IsVisible = True

OTHER TIPS

Just some ideas:

If all your tasks are completed in the _Load event try just calling End. Of course that would remove your tray icon as well.

Another possibility is to call Me.Visible in the _Shown event. This may cause a flash on the screen. If so perhaps you could position the form off the screen in _Load.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top