Sunday, 2 February 2020

C# - FolderBrowserDialog


// Open rich text files (rtf) into the RichTextBox using the FolderBrowserDialog
// to set the default directory for opening files.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form
{
    private FolderBrowserDialog folderBrowserDialog1;
    private OpenFileDialog openFileDialog1;

    private RichTextBox richTextBox1;

    private MainMenu mainMenu1;
    private MenuItem fileMenuItem, openMenuItem;
    private MenuItem folderMenuItem;

    private string openFileName, folderName;

    private bool fileOpened = false;

    // The main entry point for the application.
    [STAThreadAttribute]
    static void Main()
    {
        Application.Run(new FolderBrowserDialogExampleForm());
    }

    // Constructor.
    public FolderBrowserDialogExampleForm()
    {
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.fileMenuItem = new System.Windows.Forms.MenuItem();
        this.openMenuItem = new System.Windows.Forms.MenuItem();
        this.folderMenuItem = new System.Windows.Forms.MenuItem();
       
        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();

        this.mainMenu1.MenuItems.Add(this.fileMenuItem);
        this.fileMenuItem.MenuItems.AddRange(
                            new System.Windows.Forms.MenuItem[]  {this.openMenuItem,
                                                                 this.folderMenuItem});
        this.fileMenuItem.Text = "File";

        //(1)
        this.openMenuItem.Text = "Open...";
        this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);

        //(2)
        this.folderMenuItem.Text = "Select Directory...";
        this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click);

        this.openFileDialog1.DefaultExt = "TXT";
        this.openFileDialog1.Filter = "TXT files (*.txt)|*.txt";

        this.folderBrowserDialog1.Description =
                            "Select the directory that you want to use as the default.";
        this.folderBrowserDialog1.ShowNewFolderButton = false;
        this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;

        this.richTextBox1.AcceptsTab = true;
        this.richTextBox1.Location = new System.Drawing.Point(8, 8);
        this.richTextBox1.Size = new System.Drawing.Size(280, 344);
        this.richTextBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left |
                                   AnchorStyles.Bottom | AnchorStyles.Right;

        this.ClientSize = new System.Drawing.Size(296, 360);
        this.Controls.Add(this.richTextBox1);
        this.Menu = this.mainMenu1;
        this.Text = "TXT Document Browser";
    }
       
    private void openMenuItem_Click(object sender, System.EventArgs e)
    {      
        if (!fileOpened)
        {
            openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;
            openFileDialog1.FileName = null;
        }
      
        DialogResult result = openFileDialog1.ShowDialog();
      
        if (result == DialogResult.OK)
        {
            openFileName = openFileDialog1.FileName;
            try
            {
                // Output the requested file in richTextBox1.
                Stream s = openFileDialog1.OpenFile();
                richTextBox1.LoadFile(s, RichTextBoxStreamType.PlainText);
                //richTextBox1.LoadFile(
                //openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                s.Close();

                fileOpened = true;
            }
            catch (Exception exp)
            {
                MessageBox.Show(
                       "An error occurred while attempting to load the file. The error is:"
                                + System.Environment.NewLine + exp.ToString()
                                + System.Environment.NewLine);
                fileOpened = false;
            }
            Invalidate();           
        }       
        else if (result == DialogResult.Cancel)
        {
            return;
        }
    }  

    private void folderMenuItem_Click(object sender, System.EventArgs e)
    {       
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            folderName = folderBrowserDialog1.SelectedPath;
            if (!fileOpened)
            {              
                openFileDialog1.InitialDirectory = folderName;
                openFileDialog1.FileName = null;
                openMenuItem.PerformClick();
            }
        }
    }
}


No comments:

Post a Comment