Exception Handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AMP

    Exception Handling


    Hello,
    I have the following code in a menu function.
    If no fileis clicked (Cancel) it throws an exception.I try to write a
    catch try catch statement but opnFile is not visable in the catch
    statement I just want to close the dialog box if the user doesnt pick a
    file.I thought "Cancel" would just make it go away, but noooo.I will
    need "filename" for the next operation if they choose one.
    Help Please

    OpenFileDialog opnFile = new OpenFileDialog( );
    opnFile.Filter = "txt files (*.txt)|*.txt|A ll files
    (*.*)|*.*";
    opnFile.ShowDia log();
    filename = opnFile.FileNam e;
    // StreamReader openReader = new
    StreamReader(op enStream);
    textBox4.Text = File.ReadAllTex t(opnFile.FileN ame);
    this.button3.En abled = true;

    Thanks
    Mike

  • Kevin Spencer

    #2
    Re: Exception Handling

    Hi Mike,

    First of all, the ShowDialog() method of a
    System.Winodws. Forms.OpenFileD ialog class
    (http://msdn2.microsoft.com/en-us/lib...iledialog.aspx)
    returns a System.Windows. Forms.DialogRes ult
    (http://msdn2.microsoft.com/en-us/lib...logresult.aspx)
    enumeration value, which indicates the action that the user took, such as
    clicking the OK or Cancel buttons in the dialog box.

    Second, it is very important to take note of any class which implements a
    Dispose method, and ensure that it is properly Disposed. Otherwise, memory
    leaks are likely to occur.

    Here is an example, using a "using block" to ensure that the object is
    Disposed, and accounting for the DialogResult returned:

    using (OpenFileDialog opnFile = new OpenFileDialog( ))
    {
    opnFile.Filter = "txt files (*.txt)|*.txt|A ll files(*.*)|*.*" ;
    if (opnFile.ShowDi alog() == DialogResult.OK )
    {
    filename = opnFile.FileNam e;
    // etc.
    }
    }

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Software Composer
    Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


    A watched clock never boils.

    "AMP" <ampeloso@gmail .comwrote in message
    news:1159727909 .944984.138190@ k70g2000cwa.goo glegroups.com.. .
    >
    Hello,
    I have the following code in a menu function.
    If no fileis clicked (Cancel) it throws an exception.I try to write a
    catch try catch statement but opnFile is not visable in the catch
    statement I just want to close the dialog box if the user doesnt pick a
    file.I thought "Cancel" would just make it go away, but noooo.I will
    need "filename" for the next operation if they choose one.
    Help Please
    >
    OpenFileDialog opnFile = new OpenFileDialog( );
    opnFile.Filter = "txt files (*.txt)|*.txt|A ll files
    (*.*)|*.*";
    opnFile.ShowDia log();
    filename = opnFile.FileNam e;
    // StreamReader openReader = new
    StreamReader(op enStream);
    textBox4.Text = File.ReadAllTex t(opnFile.FileN ame);
    this.button3.En abled = true;
    >
    Thanks
    Mike
    >

    Comment

    Working...