Problem when pressed "cancel" in OpenFileDialog

K

kimiraikkonen

Hi,
Vb.net 2005 express i've been working, the problem is: I use
openfiledialog to browse files, i created some code to execute file
with OK button, no problem. But if i press "cancel" button as the
result of openfiledialog box, for example my filename's are added into
listbox or my audio file is being played from the beginning.

To explain better:

openfileplaylist.ShowDialog()
ListBox1.Items.Add(openfileplaylist.FileName)

1-That code adds file path into list box if click OK, but if i click
"cancel" it adds "OpenFileDialog1" text into listbox. Why?

2-Also i want my files listed only as "filenames", not full path like
c:\blabla\blaba\kklj\bla.mp3.

These 2 questions are very important for me.

Thanks for the answer from now.
 
G

Guest

kimiraikkonen said:
Hi,
Vb.net 2005 express i've been working, the problem is: I use
openfiledialog to browse files, i created some code to execute file
with OK button, no problem. But if i press "cancel" button as the
result of openfiledialog box, for example my filename's are added into
listbox or my audio file is being played from the beginning.

To explain better:

openfileplaylist.ShowDialog()
ListBox1.Items.Add(openfileplaylist.FileName)

1-That code adds file path into list box if click OK, but if i click
"cancel" it adds "OpenFileDialog1" text into listbox. Why?

2-Also i want my files listed only as "filenames", not full path like
c:\blabla\blaba\kklj\bla.mp3.

These 2 questions are very important for me.

Thanks for the answer from now.
Hello,
Please try this code:
Dim openfileplaylist As New OpenFileDialog
....
If openfileplaylist.ShowDialog() = DialogResult.OK Then
Dim temp_string As String
Dim a As Integer
temp_string = openfileplaylist.FileName
'remove the .mp3
temp_string = temp_string.Substring(0, temp_string.Length - 4)
'find position of last "\"
a = temp_string.LastIndexOf("\")
'retrieve the plain file name
temp_string = temp_string.Substring(a + 1)
ListBox1.Items.Add(temp_string)
Else
MsgBox("No file was loaded.")
End If

Regards,
Oliver
 
S

Stephany Young

Youv'e certainly shown the OP a 'way' to achieve what he wants but a much
simpler way would be:

If openfileplaylist.ShowDialog() = DialogResult.OK Then
ListBox1.Items.Add(Path.GetFileName(openfileplaylist.FileName))
Else
MsgBox("No file was loaded.")
End If
 
G

G Himangi

1. Check the return value of ShowDialog. If its DialResult.OK, do your
processing.

2. To get just the file name, use System.Io.Path.GetFilename(fullpath)

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
 
K

kimiraikkonen

None of the advices worked :(

I tried and got some declaration errors.

The problem is not limited only with listbox, for example if i try to
open a file via openfiledialog control while a audio is being played,
if i press cancel the playing audio returns to beginning instead of
resuming without interruption.

Something wrong with openFileDialog control, whenever i press cancel
button it returns to beginning position of code block, isn't there a
way to avoid interruption and continue current playing operation or
not to add any item into listbox if i press cancel?

Very thanks.
 
K

kimiraikkonen

Update :) Ok, finally i've solved the problem when pressed the cancel
button with help of Mr. Young at least.

But a problem arised again:
If i display files' names only by coding
"ListBox1.Items.Add(System.IO.Path.GetFileName(openfileplaylist.FileName))",
i cannot set a linkage between selected item and play button for a
double-click launch for selected item. Why?
 
G

Guest

kimiraikkonen said:
Update :) Ok, finally i've solved the problem when pressed the cancel
button with help of Mr. Young at least.

But a problem arised again:
If i display files' names only by coding
"ListBox1.Items.Add(System.IO.Path.GetFileName(openfileplaylist.FileName))",
i cannot set a linkage between selected item and play button for a
double-click launch for selected item. Why?
When you store a filename like "jungle bill" in your list box and click the
play button, how should your application know to play which file ?
Should it play 'c:\yello\jungle bill.mp3' or 'd:\mymusic\5stars\jungel
bill.mp3' or 'e:\root\subfolder\subfolder\subfolder\jungle bill.mp3' ?
Being able to play a file you do need the full path ! But you dont store the
full path in the listbox, just the pure filename. I suggest that you store
the full path of all files in a list (please have a look at
System.Collections.Generic). If the user then selects an item of the listbox,
you do not evaluate the 'selected item' but you evaluate the 'selected
index'. With the 'selected index' you use list.item(selected index) and get
your full file name.
 
R

rowe_newsgroups

Update :) Ok, finally i've solved the problem when pressed the cancel
button with help of Mr. Young at least.

But a problem arised again:
If i display files' names only by coding
"ListBox1.Items.Add(System.IO.Path.GetFileName(openfileplaylist.FileName))",
i cannot set a linkage between selected item and play button for a
double-click launch for selected item. Why?

Here's my solution. Basically, we are going to use a custom object to
pass information to the ListBox when we add the item. Then all is
needed is to unbox the object and retrieve our properties. To use this
sample create a new Window's Forms Application and add a ListBox to
Form1. Then add the following to the code-behind:

///////////////////////
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'// Simulate items being added
Me.ListBox1.Items.Add(New FileInformation("FileName", "C:
\FilePath\FileName.MP3"))
Me.ListBox1.Items.Add(New FileInformation("FileName2", "C:
\FilePath2\FileName2.MP3"))
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
Try
Dim item As FileInformation =
DirectCast(Me.ListBox1.SelectedItem, FileInformation)
MsgBox(String.Format("The path for item '{0}' is '{1}'",
item.FileName, item.FilePath), MsgBoxStyle.OkOnly, "File Information")
Catch ex As InvalidCastException
MsgBox("The selected file was not found.")
'// Remove the item to prevent further troubles
Me.ListBox1.Items.Remove(Me.ListBox1.SelectedItem)
End Try
End Sub

End Class

Public Class FileInformation

Public Sub New()

End Sub

Public Sub New(ByVal fileName As String, ByVal filePath As String)
Me.FileName = fileName
Me.FilePath = filePath
End Sub

Public Property FileName() As String
Get
Return _FileName
End Get
Set(ByVal value As String)
_FileName = value
End Set
End Property
Private _FileName As String = String.Empty

Public Property FilePath() As String
Get
Return _FilePath
End Get
Set(ByVal value As String)
_FilePath = value
End Set
End Property
Private _FilePath As String = String.Empty

Public Overrides Function ToString() As String
Return FileName
End Function

End Class
///////////////////////

Thanks,

Seth Rowe
 
K

kimiraikkonen

When you store a filename like "jungle bill" in your list box and click the
play button, how should your application know to play which file ?
Should it play 'c:\yello\jungle bill.mp3' or 'd:\mymusic\5stars\jungel
bill.mp3' or 'e:\root\subfolder\subfolder\subfolder\jungle bill.mp3' ?
Being able to play a file you do need the full path ! But you dont store the
full path in the listbox, just the pure filename. I suggest that you store
the full path of all files in a list (please have a look at
System.Collections.Generic). If the user then selects an item of the listbox,
you do not evaluate the 'selected item' but you evaluate the 'selected
index'. With the 'selected index' you use list.item(selected index) and get
your full file name.


Yes and thanks, i've understood the player cannot reach to file due
not to presense of full path. Could you give a simple example about
system.collections.generic with selected index?
 
K

kimiraikkonen

Hi there again,
I've thought for a long time then found a solution :)

I created a label2.text which shows full file path.

Then listbox shows only file by using
ListBox1.Items.Add(System.IO.Path.GetFileName(openfileplaylist.FileName))

Here is some code i've achived:

'Assigns full path filename to label2.text
Private Sub ListBox1_click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.Click
Label2.Text = openfileplaylist.FileName
End Sub

'Gets file path from label2.text
Me.AxWindowsMediaPlayer1.URL = Label2.Text
 
K

kimiraikkonen

Update regarding my previous message: If i do that coding (previous
message), when more then one items added to the listbox, it it brings
more problems such as not to recognize selected item by label2.text.

Private Sub ListBox1_click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.Click
Label2.Text = openfileplaylist.FileName
End Sub

That code only allows me to get the last added file name to the
listbox, the previously added files' name cannot be recognized by
label2.text.

why?
 
R

rowe_newsgroups

Update regarding my previous message: If i do that coding (previous
message), when more then one items added to the listbox, it it brings
more problems such as not to recognize selected item by label2.text.

Private Sub ListBox1_click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.Click
Label2.Text = openfileplaylist.FileName
End Sub

That code only allows me to get the last added file name to the
listbox, the previously added files' name cannot be recognized by
label2.text.

why?

Did you try the code I provided? It saves everything in one piece to
the listbox. There is no need to use a label, or a generic list,
everything is done from the ListBox's Items collection.

Please give it a try (all you need to do is copy and paste it into a
new form that contains a listbox named "ListBox1") and let me know how
it works!

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top