0

I've got a 2 form windows application in VB Net 2012

On loading form1, I want to check if some settings have been set. If not then form1 is hidden and form2 (the settings screen opened)

Private Sub Form1_Load() Handles Me.Load
    If My.Settings.ftpServer = Nothing Or My.Settings.userName = Nothing Or   My.Settings.passWord = Nothing Or My.Settings.path = Nothing Then
        Me.Hide()
        Dim oForm As Form2
        oForm = New Form2()
        oForm.Show()
        oForm = Nothing

    End If
End Sub

On first run, it's showing both forms. After settings are done, subsequent runs just show form1.

What have I done wrong?

andymoyle
  • 438
  • 4
  • 13
  • 1
    When you open it the first time do you fill in values for ftpserver, userName...? You can right click on your project and select Properties. Then open the tab Settings and see if your settings have values. If they have values then that is the reason why your second form does not display on subsequent runs. – EJD Jul 30 '15 at 21:12
  • if the settings are string, use `String.IsNullOrEmpty()` to test – Ňɏssa Pøngjǣrdenlarp Jul 30 '15 at 21:23
  • To clarify... On first run (or if setting have been deleted!), the if statement should close form1 and open form2 as there are no settings saved. Once settings have been saved then form2 is not opened on subsequent runs. Currently both forms open when no settings saved, but I only want form 2 to be open – andymoyle Jul 30 '15 at 21:35

1 Answers1

0

So the problem is that the Load event occurs after the form is already shown. As shown by the link Mark referenced. I changed the sub name and action to Shown and it works...

 Private Sub Form1_Shown() Handles Me.Shown
    If String.IsNullOrEmpty(My.Settings.ftpServer) Or String.IsNullOrEmpty(My.Settings.userName) Or String.IsNullOrEmpty(My.Settings.passWord) Or String.IsNullOrEmpty(My.Settings.path) Then

        Dim oForm As Form2
        oForm = New Form2()
        oForm.Show()
        oForm = Nothing
        Me.Hide()
    End If
End Sub
andymoyle
  • 438
  • 4
  • 13