Solved

File Choosers in C# Window Applications

Posted on 2004-10-04
3
530 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 50 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

756 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