Visual Basic Strip menus

August 1, 2009 at 11:43:09
Specs: Windows Vista
Hi,

I was experimenting with the strip menu in Microsoft visual basic 2008 express edition but experienced a problem when opening a windows explorer window to access files. I was under the impression that to open this i had to use 'OpenFD.ShowDialog()'; however when I add this to my code the Dialog (windows explorer window) opens twice... Can you explain why? It appears to open once if i do not add this code, so I cannot understand how the Dialog is opening.... There is a copy of my code included in this email:

Private Sub MSFOpen_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles MSFOpen.Click

Dim FileTitle As String

Dim Result

OpenFD.InitialDirectory = "C:\Users"

OpenFD.Title = "Which File Do You Want To Open?"

OpenFD.Filter = "TextFile|*.txt"

Result = OpenFD.ShowDialog()

FileTitle = OpenFD.FileName

If Result = DialogResult.Cancel Then

MsgBox("You Clicked Cancel")

Else : MsgBox(FileTitle)

End If

End Sub


Thanks for your help in advance, I am sorry if my problem has not been clear, if I can help by clarifying I would happily do so.

W.Akhurst



See More: Visual Basic Strip menus

Report •


#1
August 2, 2009 at 09:34:44
It appears to open once if i do not add this code, so I cannot understand how the Dialog is opening
Apperently something else is also displaying the dialog. What it is, I don't know. Have you tried setting a breakpoint on OpenFileDialog.ShowDialog(), and see where it's called?

Report •

#2
August 2, 2009 at 10:30:14
Fantastic, I'd never used a breakpoint before but it really did the trick.

I identified that somehow setting the Result variable to 'OpenFD.ShowDialog()' somehow meant that the dialog was called... I solved this by removing the variable and using DialogResult in my If statement instead of the Result variable.


One more thing you could help me with: when i open my windows explorer from the Open Button in my script, it defults to 'OpenFileDialog1'. Even when i click the cancel button my text box opens and has the text 'OpenFileDialog1', it should return 'You clicked cancel'.

Do you know why?

I include a revised version of my script:

Private Sub MSFOpen_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MSFOpen.Click

Dim FileTitle As String

OpenFD.InitialDirectory = "C:\Users"
OpenFD.Title = "Which File Do You Want To Open?"
OpenFD.Filter = "TextFile|*.txt"
OpenFD.ShowDialog()


FileTitle = OpenFD.FileName

If DialogResult = DialogResult.Cancel Then
MsgBox("You Clicked Cancel")
Else : MsgBox(FileTitle)

End If

End Sub


Thanks for you help so far


W.Akhurst


Report •

#3
August 3, 2009 at 02:33:52
Found the answer:

Here is a copy of my working code:

Private Sub MSFOpen_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MSFOpen.Click

Dim strFileName As String

OpenFD.InitialDirectory = "C:\"

OpenFD.Title = "Open a Text File"
OpenFD.Filter = "Text Files|*.txt"
Dim x As Integer = OpenFD.ShowDialog()

If x = Windows.Forms.DialogResult.Cancel Then
MsgBox("cancelled")
Else
strFileName = OpenFD.FileName
MsgBox(strFileName)
End If

End Sub


Report •

Related Solutions


Ask Question