943,590 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 5757
  • C# RSS
Jan 4th, 2009
0

Open file dialog

Expand Post »
Dear guys,

Can anyone help me out to open up a windows default dialog box in C# to browse folder and the folder path be displayed in the lab/edit box.

I am new at C#. Just created a simple form application and now looking to do the backend coding

Regards,
Ali
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Open file dialog

If you are using VS 2008 or VS 2005 just drop an OpenFileDialog from the toolbox on your form
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 4th, 2009
0

Re: Open file dialog

Click to Expand / Collapse  Quote originally posted by ddanbe ...
If you are using VS 2008 or VS 2005 just drop an OpenFileDialog from the toolbox on your form

Yes, I am using VS 2008 and i've drag and drop an openFileDialog but how can I initiate it. It simply shows below the Main form i've created with buttons on it.

Do I need to initiate behind any button click event?

thx
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Open file dialog

Hello, gallian99.
This may help you: OpenFileDialog Class - that for case if you're not dropped it on your form.
and this: Opening Files Using the OpenFileDialog Component - if it is on your form.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 4th, 2009
0

Re: Open file dialog

You essentially do something like this
C# Syntax (Toggle Plain Text)
  1. private void button1_Click(object sender, System.EventArgs e)
  2. {
  3. if(openFileDialog1.ShowDialog() == DialogResult.OK)
  4. {
  5. // The ShowDialog does just what it says
  6. //Do whatever you like to do when you clicked OK button in the dialog, see also hints by Antenka
  7. }
  8. }
Last edited by ddanbe; Jan 4th, 2009 at 3:01 pm.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 4th, 2009
0

Re: Open file dialog

Thanks that helps...

Can you also show me how to return the selected folder path in a label once user selects/browses the folder and presses ok in folderBrowserDialog

thx

Click to Expand / Collapse  Quote originally posted by ddanbe ...
You essentially do something like this
C# Syntax (Toggle Plain Text)
  1. private void button1_Click(object sender, System.EventArgs e)
  2. {
  3. if(openFileDialog1.ShowDialog() == DialogResult.OK)
  4. {
  5. // The ShowDialog does just what it says
  6. //Do whatever you like to do when you clicked OK button in the dialog, see also hints by Antenka
  7. }
  8. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Open file dialog

Have you checked what properties and how to use the dialogbox as per the helpfile? If we hand you all the code you dont learn how to find these things and will end up frustrated as you have to ask for everything and it takes longer.

If you look at the dialogbox in the helpfile, it has examples, it shows how to get the filename and stuff back, then the label component has properties of its own and you can see how to apply them..

Far more pride and joy is got by doing it yourself.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 4th, 2009
0

Re: Open file dialog

Totally agree to your message. I just figured it out. Thanks to all the forum members who replied. Just one more thing as of now, can I truncate the selectedPath to only the folder name that the users selects. folderName shows the complete path and i don't have much space in the designed label to cater to the long and lengthy paths.


private void button2_Click(object sender, EventArgs e)
{

DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
folderName = folderBrowserDialog1.SelectedPath;
}
lPath.Text = folderName;

}



Click to Expand / Collapse  Quote originally posted by LizR ...
Have you checked what properties and how to use the dialogbox as per the helpfile? If we hand you all the code you dont learn how to find these things and will end up frustrated as you have to ask for everything and it takes longer.

If you look at the dialogbox in the helpfile, it has examples, it shows how to get the filename and stuff back, then the label component has properties of its own and you can see how to apply them..

Far more pride and joy is got by doing it yourself.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Open file dialog

Sure, you can, e.g. using method LastIndexOf() to find the last slash character in the returned string ... your folder name will be placed after the last slash.
Last edited by Antenka; Jan 4th, 2009 at 3:59 pm. Reason: Mistaken :(
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 4th, 2009
1

Re: Open file dialog

Or you could do it more simply with System.IO.Path.GetDirectoryName
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Need advice on matrix input
Next Thread in C# Forum Timeline: TagPrefix and Assembly error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC