Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004
    Posts
    38

    Unanswered: 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.

  2. #2
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •