If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > Trbl with open Dialog box in C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-05, 03:29
Crash1hd Crash1hd is offline
Registered User
 
Join Date: Jan 2004
Posts: 38
Trbl with open Dialog box in C#

Well everything was going great until I clicked on cancle lol ok here goes...

When I click on the file open in my program and then decide I dont want to open something I get an error below is the code I used for the open dialog box in C#

private void menuFileOpen_Click(object sender, System.EventArgs e)
{
// for opening file
openFileDialog1 = new OpenFileDialog();
openFileDialog1.ShowDialog();
StreamReader sr = new StreamReader(openFileDialog1.FileName);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}

Now when I click on the cancel button it gives me this error

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Empty path name is not legal.
Reply With Quote
  #2 (permalink)  
Old 02-26-05, 03:56
Crash1hd Crash1hd is offline
Registered User
 
Join Date: Jan 2004
Posts: 38
Ok so I updated the code to the following

{
Stream myStream;
openFileDialog1 = new OpenFileDialog();

//openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = openFileDialog1.OpenFile())!= null)
{
// for opening file
StreamReader sr = new StreamReader(openFileDialog1.FileName);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
myStream.Close();

this.Text = Path.GetFileName(openFileDialog1.FileName) + " - Notepad 2";
this.StatusBar.Text = openFileDialog1.FileName;
}
}
}

and that has fixed the problem I just cant help but think there is too much code there or at least it could be shortened up maybe someone can help me with that or even better if someone can break this code down and explain what each part does?

Thankyou in advance!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On