using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// References.
// On the Assemblies > Framework tab, add a reference to:
//      Microsoft.Office.Interop.Excel
// On the COM > Type Libbraries tab, add a reference to:
//      Microsoft Office 14.0 Object Library
// Be sure to use the same version for both libraries. (14.0 for my system.)

using Excel = Microsoft.Office.Interop.Excel;
using Core = Microsoft.Office.Core;

namespace FolderDialog
{
    public partial class frm46FolderDialog : Form
    {
        public frm46FolderDialog()
        {
            InitializeComponent();
        }

        // Use the FolderBrowserDialog.
        private void folderBrowserDialogButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = folderTextBox.Text;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                folderTextBox.Text = dialog.SelectedPath;
            }
        }

        // Use the Excel folder browser.
        private void excelFolderBrowserButton_Click(object sender, EventArgs e)
        {
            Excel.Application excel = new Excel.Application();
            Core.FileDialog dialog = excel.FileDialog[
                Core.MsoFileDialogType.msoFileDialogFolderPicker];

            dialog.InitialFileName = folderTextBox.Text;
            dialog.ButtonName = "Select";
            dialog.Title = "Pick a Folder";

            const int ok = -1;
            const int cancel = 0;
            if (dialog.Show() == ok)
            {
                Core.FileDialogSelectedItems items = dialog.SelectedItems;
                folderTextBox.Text = items.Item(1);
            }
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.excelFolderBrowserButton = new System.Windows.Forms.Button();
            this.folderTextBox = new System.Windows.Forms.TextBox();
            this.folderBrowserDialogButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // excelFolderBrowserButton
            // 
            this.excelFolderBrowserButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.excelFolderBrowserButton.Location = new System.Drawing.Point(195, 15);
            this.excelFolderBrowserButton.Name = "excelFolderBrowserButton";
            this.excelFolderBrowserButton.Size = new System.Drawing.Size(126, 23);
            this.excelFolderBrowserButton.TabIndex = 1;
            this.excelFolderBrowserButton.Text = "Excel Folder Browser";
            this.excelFolderBrowserButton.UseVisualStyleBackColor = true;
            this.excelFolderBrowserButton.Click += new System.EventHandler(this.excelFolderBrowserButton_Click);
            // 
            // folderTextBox
            // 
            this.folderTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.folderTextBox.Location = new System.Drawing.Point(12, 60);
            this.folderTextBox.Name = "folderTextBox";
            this.folderTextBox.Size = new System.Drawing.Size(360, 20);
            this.folderTextBox.TabIndex = 2;
            this.folderTextBox.Text = "c:\\temp";
            // 
            // folderBrowserDialogButton
            // 
            this.folderBrowserDialogButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.folderBrowserDialogButton.Location = new System.Drawing.Point(63, 15);
            this.folderBrowserDialogButton.Name = "folderBrowserDialogButton";
            this.folderBrowserDialogButton.Size = new System.Drawing.Size(126, 23);
            this.folderBrowserDialogButton.TabIndex = 0;
            this.folderBrowserDialogButton.Text = "FolderBrowserDialog";
            this.folderBrowserDialogButton.UseVisualStyleBackColor = true;
            this.folderBrowserDialogButton.Click += new System.EventHandler(this.folderBrowserDialogButton_Click);
            // 
            // frm46FolderDialog
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 91);
            this.Controls.Add(this.folderBrowserDialogButton);
            this.Controls.Add(this.folderTextBox);
            this.Controls.Add(this.excelFolderBrowserButton);
            this.Name = "frm46FolderDialog";
            this.Text = "FolderDialog";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button excelFolderBrowserButton;
        private System.Windows.Forms.TextBox folderTextBox;
        private System.Windows.Forms.Button folderBrowserDialogButton;
    }
}