Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File Choosers in C# Window Applications

Posted on 2004-10-04
3
Medium Priority
?
542 Views
Last Modified: 2010-04-17
I am new to C# windows programming. I would like to knowhow to use a drop down menu, IE "File -> Open..." to open a file chooser dialog?
Thank you
James Lawson
0
Comment
Question by:jameslawson976
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
erin010 earned 200 total points
ID: 12265300
//adding a menu object to your project will create code similar to the following:
            private System.Windows.Forms.MainMenu mainMenu1;
            private System.Windows.Forms.MenuItem menuItemFile;
            private System.Windows.Forms.MenuItem menuItemNew;
            private System.Windows.Forms.MenuItem menuItemOpen;
//------------------------------------------------------------------------------------------

private string m_strFileName = "";

/Hold the DialogResult enum result
//Ok or Cancel
private DialogResult dlgResult;

private void menuItemOpen_Click(object sender, System.EventArgs e)
{

    // dlgOpenFile is an OpenFileDialog object
    dlgResult = dlgOpenFile.ShowDialog();
    if(dlgResult == DialogResult.Cancel)
        return;

    try
    {
                // get the file name
        m_strFileName = dlgOpenFile.FileName;
                // open to read that file
        StreamReader sr = new StreamReader(m_strFileName);
                //read the whole file
        txtBody.Text = sr.ReadToEnd();
        sr.Close();
        m_bModified = false;
    }
    catch(Exception err)
    {
        MessageBox.Show(err.Message,"Error",
                         MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question