WPF OpenFileDialog runs twice?

CodeLiftsleep

Member
Joined
Mar 11, 2016
Messages
20
Programming Experience
3-5
Not sure what the issue is, I am working on a project in WPF, and have the following code for a OpenDB Button.click event handler:

VB.NET:
    Private Sub OpenDB_Click(sender As Object, e As RoutedEventArgs) Handles OpenDB.Click 'Allows user to choose which DB to open
        Dim MyOFD As New OpenFileDialog
        MyOFD.Filter = "SQLite DataBase Files (*.sqlite)|*.sqlite"
      
        If MyOFD.ShowDialog = DialogResult.OK Then
            MyFilePath = MyOFD.FileName
            MyFilePath = (Replace(MyFilePath, "Football.sqlite", ""))
        End If
       
    End Sub

The code runs once, exits the sub and then runs a second time...the button is only getting pushed one time, so I don't understand why its being run again for a second time through.

Any help would be appreciated...it's a minor nuisance but its really annoying to have to select the same file twice...
 
The OpenFileDialog is irrelevant. If that method is being executed twice then any code inside it will be executed twice. My guess is that you you have registered that method to handle another event, either the same event a second time or a different event. Try deleting the Handles clause from the method and see if it still gets executed. If so then that is definitely the case. You don't have an AddHandler statement anywhere do you?
 
The OpenFileDialog is irrelevant. If that method is being executed twice then any code inside it will be executed twice. My guess is that you you have registered that method to handle another event, either the same event a second time or a different event. Try deleting the Handles clause from the method and see if it still gets executed. If so then that is definitely the case. You don't have an AddHandler statement anywhere do you?

You are exactly right...

I went and clicked on view all references and it showed two. I clicked on the second one and it pulled up the XAML I had written where I had Click = "OpenDB_Click" in addition to having a handler written. Once I removed this, it worked as it should, only opening once...

I'm getting a crash course in XAML on the fly as I am converting from using WinForms to WPF and mostly loving it, although it can be a little overwhelming at times with all the new things you are able to do...
 

Latest posts

Back
Top