473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem when pressed "cancel" in OpenFileDialog

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:

openfileplaylis t.ShowDialog()
ListBox1.Items. Add(openfilepla ylist.FileName)

1-That code adds file path into list box if click OK, but if i click
"cancel" it adds "OpenFileDialog 1" 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.

Sep 25 '07 #1
11 14275
"kimiraikko nen" wrote:
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:

openfileplaylis t.ShowDialog()
ListBox1.Items. Add(openfilepla ylist.FileName)

1-That code adds file path into list box if click OK, but if i click
"cancel" it adds "OpenFileDialog 1" 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 openfileplaylis t As New OpenFileDialog
....
If openfileplaylis t.ShowDialog() = DialogResult.OK Then
Dim temp_string As String
Dim a As Integer
temp_string = openfileplaylis t.FileName
'remove the .mp3
temp_string = temp_string.Sub string(0, temp_string.Len gth - 4)
'find position of last "\"
a = temp_string.Las tIndexOf("\")
'retrieve the plain file name
temp_string = temp_string.Sub string(a + 1)
ListBox1.Items. Add(temp_string )
Else
MsgBox("No file was loaded.")
End If

Regards,
Oliver
Sep 25 '07 #2
Youv'e certainly shown the OP a 'way' to achieve what he wants but a much
simpler way would be:

If openfileplaylis t.ShowDialog() = DialogResult.OK Then
ListBox1.Items. Add(Path.GetFil eName(openfilep laylist.FileNam e))
Else
MsgBox("No file was loaded.")
End If

"Oliver Tell" <Ol********@dis cussions.micros oft.comwrote in message
news:A5******** *************** ***********@mic rosoft.com...
"kimiraikko nen" wrote:
>Hi,
Vb.net 2005 express i've been working, the problem is: I use
openfiledial og 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:

openfileplayli st.ShowDialog()
ListBox1.Items .Add(openfilepl aylist.FileName )

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

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

These 2 questions are very important for me.

Thanks for the answer from now.

Hello,
Please try this code:
Dim openfileplaylis t As New OpenFileDialog
...
If openfileplaylis t.ShowDialog() = DialogResult.OK Then
Dim temp_string As String
Dim a As Integer
temp_string = openfileplaylis t.FileName
'remove the .mp3
temp_string = temp_string.Sub string(0, temp_string.Len gth - 4)
'find position of last "\"
a = temp_string.Las tIndexOf("\")
'retrieve the plain file name
temp_string = temp_string.Sub string(a + 1)
ListBox1.Items. Add(temp_string )
Else
MsgBox("No file was loaded.")
End If

Regards,
Oliver
Sep 25 '07 #3
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(ful lpath)

---------
- 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).
EZNamespaceExte nsions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensio ns.Net : Develop all shell extensions,expl orer bars and BHOs
rapidly in .Net
---------


"kimiraikko nen" <ki************ *@gmail.comwrot e in message
news:11******** *************@k 79g2000hse.goog legroups.com...
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:

openfileplaylis t.ShowDialog()
ListBox1.Items. Add(openfilepla ylist.FileName)

1-That code adds file path into list box if click OK, but if i click
"cancel" it adds "OpenFileDialog 1" 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.

Sep 25 '07 #4
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.

Sep 25 '07 #5
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.GetFileNam e(openfileplayl ist.FileName))" ,
i cannot set a linkage between selected item and play button for a
double-click launch for selected item. Why?

Sep 25 '07 #6
"kimiraikko nen" wrote:
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.GetFileNam e(openfileplayl ist.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\jungl e bill.mp3' or 'd:\mymusic\5st ars\jungel
bill.mp3' or 'e:\root\subfol der\subfolder\s ubfolder\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.Collecti ons.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(selec ted index) and get
your full file name.
Sep 26 '07 #7
On Sep 25, 7:29 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
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.GetFileNam e(openfileplayl ist.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'// Simulate items being added
Me.ListBox1.Ite ms.Add(New FileInformation ("FileName", "C:
\FilePath\FileN ame.MP3"))
Me.ListBox1.Ite ms.Add(New FileInformation ("FileName2" , "C:
\FilePath2\File Name2.MP3"))
End Sub

Private Sub ListBox1_Select edIndexChanged( ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
ListBox1.Select edIndexChanged
Try
Dim item As FileInformation =
DirectCast(Me.L istBox1.Selecte dItem, FileInformation )
MsgBox(String.F ormat("The path for item '{0}' is '{1}'",
item.FileName, item.FilePath), MsgBoxStyle.OkO nly, "File Information")
Catch ex As InvalidCastExce ption
MsgBox("The selected file was not found.")
'// Remove the item to prevent further troubles
Me.ListBox1.Ite ms.Remove(Me.Li stBox1.Selected Item)
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

Sep 26 '07 #8
>
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\jungl e bill.mp3' or 'd:\mymusic\5st ars\jungel
bill.mp3' or 'e:\root\subfol der\subfolder\s ubfolder\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.Collecti ons.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(selec ted 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.collecti ons.generic with selected index?

Sep 26 '07 #9
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.P ath.GetFileName (openfileplayli st.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.EventArg s) Handles ListBox1.Click
Label2.Text = openfileplaylis t.FileName
End Sub

'Gets file path from label2.text
Me.AxWindowsMed iaPlayer1.URL = Label2.Text
Sep 26 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2973
by: Hernán Castelo | last post by:
hi i'm doing a reload() in JS in the production site i get the message "send again information...... : retry / cancel" but in development site works fine what differencia could be between the configuration of both IIS that allow these refresh or reload() ?
1
10496
by: Jane | last post by:
How to set the default button to "Cancel" instead of "OK" when pop-up the message box using the codes below: if (window.confirm("Are you sure you want to delete?")) { //Proceed to delete } Please advise, many thanks!!!! Jane.
5
8486
by: Quinn | last post by:
When users clicked a unkown mime type link such as Zip on my website, a "Save/Open/Cancel" dialog box pops up. Is there a way to detect which button users clicked by using ASP? actually I only what to record the "valid" click -- when Open/Save was clicked. Thanks ahead. Quinn
3
5221
by: dhnriverside | last post by:
Hi peeps I'm developing an intranet application which uses a main window and various popups created with javascript ( window.open() ). When the user saves what they are doing in the popup, i use opener.location.reload() to refresh the parent window. Most of the time this works fine, but one parent in particular always asks if we want to refresh the window (re post the form data), and I get that "retry / cancel" dialog box.
5
3347
by: Diego | last post by:
How do I capture a cancel event of Printer dialog box? Regards, Diego
4
1493
by: Graham Charles | last post by:
Hello, I'm finding that if I set the "Cancel" property during the AppStartup event, the application's Splash form remains loaded. A call to the Close property of the SplashForm at that point returns a cross-threading error, since (it appears) the Splashform is loaded by a different thread. Any advice? I could ignore the SplashForm application setting and just
1
4976
by: Appu | last post by:
How to Check in the window print dialog box whether we clicked either "print" or "cancel". while clicking a button i call wnidow.print() to pop up the windows PRint Dialog box. I want to check whether the user clicked either "print" or "cancel" in that print dialog box of windows. Please Help soon. Thanks T.Appasamy
1
2565
by: shaizaban | last post by:
hello, my question is as follows (very urgent): i generated in an aspx page a modalpopup ajax control. i inserted a user control into the panel contained in the popup control. the panel contains "ok" and "cancel", with no connectio to the user control. how can i cause the popup stay visible after "ok"? any button i press causes the popup to disappear!!! is there a way for it???
2
2178
Claus Mygind
by: Claus Mygind | last post by:
Is there a newer version of javaScript that will allow other text on the confirm dialogue box in place of "Ok" and "Cancel" Specifically for FireFox. I know that I can use visual basic for internet explorer, but that cannot be used in FireFox.
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.