Download Horizon :: Staff Members :: Save Vault :: XboxMB YouTube


Old 03-27-2011   #1 (permalink)
Regular Member
B r a d's Avatar
Join Date: Oct 2010
Location: United Kingdom
Posts: 1,462
Thanks: 376
Default Working with openFileDialog - Help

I'm trying to make a notepad sort of program.
This is my code for the openFileDialog but I don't get where I've gone wrong..
There are no errors but nothing works that I've tried to code. (Title, Filter, InitialDirectory)


Code:
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            openFileDialog1.Title = "Open Text File";
            openFileDialog1.Filter = "TXT File|*.txt";
            openFileDialog1.InitialDirectory = @"C:\Users\user\Desktop";
        }
__________________

GT: Brad Was GB
Steam ID: CallMeBrad

B r a d is offline Send a message via AIM to B r a d
Reply With Quote


Old 03-27-2011   #2 (permalink)

Oscar's Avatar
Join Date: Oct 2010
Location: UK
Posts: 1,071
Thanks: 1,464
Default Re: Working with openFileDialog - Help

Try it like this..

Code:
//Set the properties
openFileDialog1.Title = "Open Text File";
openFileDialog1.Filter = "TXT File|*.txt";
openFileDialog1.InitialDirectory = @"C:\Users\user\Desktop";

//Check the user hasn't clicked the "Cancel" button
if(openFileDialog1.ShowDialog() != DialogResult.OK)
	return;
	
//The full path of the selected file can be retrieved with "openFileDialog1.FileName"
MessageBox.Show("The file that was selected is: " + openFileDialog1.FileName);

Oh and you'll want to put that on the openToolStripMenuItem_Click void, you shouldn't need the FileOk one.
Oscar is offline Send a message via AIM to Oscar
Reply With Quote
The following user thanked this post: B r a d


Old 03-27-2011   #3 (permalink)
Regular Member
B r a d's Avatar
Join Date: Oct 2010
Location: United Kingdom
Posts: 1,462
Thanks: 376
Default Re: Working with openFileDialog - Help

Quote:
Originally Posted by Oscar View Post
Try it like this..

Code:
//Set the properties
openFileDialog1.Title = "Open Text File";
openFileDialog1.Filter = "TXT File|*.txt";
openFileDialog1.InitialDirectory = @"C:\Users\user\Desktop";

//Check the user hasn't clicked the "Cancel" button
if(openFileDialog1.ShowDialog() != DialogResult.OK)
	return;
	
//The full path of the selected file can be retrieved with "openFileDialog1.FileName"
MessageBox.Show("The file that was selected is: " + openFileDialog1.FileName);

Oh and you'll want to put that on the openToolStripMenuItem_Click void, you shouldn't need the FileOk one.
Thank you, this worked!
How would I read what's in the text file that I'm trying to open?
__________________

GT: Brad Was GB
Steam ID: CallMeBrad


Last edited by B r a d; 03-27-2011 at 07:36 AM.
B r a d is offline Send a message via AIM to B r a d
Reply With Quote




Old 03-27-2011   #4 (permalink)

Oscar's Avatar
Join Date: Oct 2010
Location: UK
Posts: 1,071
Thanks: 1,464
Default Re: Working with openFileDialog - Help

Quote:
Originally Posted by boxxybabee View Post
Thank you, this worked!
How would I read what's in the text file that I'm trying to open?
Add this to the top of your code
Code:
using System.IO;
Then after you've selected the file with OpenFileDialog you need to read it:
Code:
textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
And writing it back would be:
Code:
File.WriteAllText(openFileDialog1.FileName, textBox1.Text);
Simple
Oscar is offline Send a message via AIM to Oscar
Reply With Quote
The following user thanked this post: B r a d


Old 03-27-2011   #5 (permalink)
Regular Member
B r a d's Avatar
Join Date: Oct 2010
Location: United Kingdom
Posts: 1,462
Thanks: 376
Default Re: Working with openFileDialog - Help

Quote:
Originally Posted by Oscar View Post
Add this to the top of your code
Code:
using System.IO;
Then after you've selected the file with OpenFileDialog you need to read it:
Code:
textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
And writing it back would be:
Code:
File.WriteAllText(openFileDialog1.FileName, textBox1.Text);
Simple
Lol, I really am a noob. xD
Then again, this is my first C# program.
__________________

GT: Brad Was GB
Steam ID: CallMeBrad

B r a d is offline Send a message via AIM to B r a d
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:08 PM.


 

Powered by vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
COPYRIGHT (c) 2010 - 2013 - XboxMB - DESIGN BY:
EDENWEBS.COM