I initiate OpenFileDialog from a btn and once the correct file is selected at OpenFileDialog1_FileOK * ,I want to load Form2 to do some file checking routines.

Private Sub OpenFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        dbfFileFolder = System.IO.Path.GetDirectoryName(OpenFileDialog1.FileName)
        dbfFileName = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
        dbfFileNameShort = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)

        If dbfFileName = "GNDITEM.DBF" Then
            frmFileCheck.Show() 'this is my Form2

        End If

End Sub

The new Form2 shows on top of the OpenFileDialog (which is obviously on top of the Form1) only AFTER completing those events.

If I don't have any events on Form2 all works perfect; the OpenFileDialog closes as I had hoped. What would possibly be keeping OpenFileDialog open?

Appreciate your help

Instead of using the FileOk event, get the file name, etc by using ShowDialog. This prevents further processing until the file dialog is closed.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Dim dbfFileFolder As String = System.IO.Path.GetDirectoryName(OpenFileDialog1.FileName)
            Dim dbfFileName As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
            Dim dbfFileNameShort As String = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)

            If dbfFileName = "GNDITEM.DBF" Then
                frmFileCheck.Show() 'this is my Form2
            End If

        End If

    End Sub

Rev, I must have missed the plot here because I can't get this to work either. In fact I've strated a whole new project and can't it right. For some reason the events on Form2 are executing BEFORE the form is drawn. See the code below and let me know where I missed it.....please! I've included the simple code from both forms.

Public Class Form1

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

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim dbfFileFolder As String = System.IO.Path.GetDirectoryName(OpenFileDialog1.FileName)
            Dim dbfFileName As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
            Dim dbfFileNameShort As String = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
            'If dbfFileName = "GNDITEM.DBF" Then
            '    Form2.ShowDialog() 'this is my Form2
            'End If
        End If

        'Form2.Show() 'this is my Form2
        Form2.ShowDialog() 'this is my Form2

    End Sub
End Class

.......

Public Class Form2

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        MsgBox("Here we are")

        sInit()


    End Sub

    Public Sub sInit()
        MsgBox("Here we are in the sub")
    End Sub
End Class

Try this...keep the form 2 code as it is....

Imports System.IO

Public Class Form1

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
          If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
               Dim dbfFileFolder As String = OpenFileDialog1.FileName
               Dim stFileName As String
               Dim MyFile As FileInfo = New FileInfo(dbfFileFolder)
               stFileName = MyFile.Name
               If stFileName = "GNDITEM.DBF" Then
                    Form2.ShowDialog() 'this is my Form2
               Else
                    MsgBox("error")
               End If
          End If
     End Sub
End Class

@ poojavb,

Same result, the events on Form2 occur before the form paints

What events on form2 are firing before the form is displayed?

Hey Rev, in the code I posted above all goes as expected until I select the 'correct' file and Ok.

I then get the first Msgbox from Form2 load followed by the 2nd Msgbox from Form2's Private Sub sInit. Then the form itself arrives on the scene.

I was under the impression that the form get delivered first and then may go off on it's various routines, events, functions and perhaps unexpected features :)

The Form_Load event is raised when the form is loaded, not when the rendering is completed. That's just the way it is. What are you doing that requires you to wait until the form is rendered?

I want Form2 to load and then do some file checking routines (if_exists) and then report the results. I guess I can always do that with a cmd btn. Damn software does things TOO fast sometimes :(
We can call this Case Closed, thanks

commented: You can try using Form2_Shown instead of Form2_Load +0

I'm not sure I see the problem. If the results are displayed in form2 then they will be shown when the form has finished rendering. If you want to post the zipped project I can try running it locally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.