Thursday, April 23, 2020

How to open different types of files using OpenFileDialog in c#



This code shows how we can select different types of files using Openfilediaog in our windows form application using c#.




public partial class frm_OpenFileDialogue : Form
    {
        public frm_OpenFileDialogue()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Text Files|*.txt";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox7.Text = dlg.FileName;
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Word Documents|*.doc;*.docx";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = dlg.FileName;
            }
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Excel Worksheets|*.xlx;*.xlsx";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = dlg.FileName;
            }
        }

        private void btnPowerPoint_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Powerpoint Presentations|*.ppt;*.pptx";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = dlg.FileName;
            }
        }

        private void btnOfficeFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Office Files|*.doc;*.docx;*.xls;*.xlsx;*.ppt; *.pptx";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = dlg.FileName;
            }
        }

        private void btnAllFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "All Files|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox5.Text = dlg.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            // Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations 
            //           OR Office Files 
            //           OR All Files
            dlg.Filter = "Word Documents|*.doc;*.docx|Excel Worksheets|*.xls;*.xlsx|PowerPoint Presentations|*.ppt;*.pptx" +
                         "|Office Files|*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx" +
                         "|All Files|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox6.Text = dlg.FileName;
            }
        }

    }

No comments:

Post a Comment

How to use combobox using c#

This code shows how we can use combobox in our winforms application using c#.     public partial class frmComboBox_Demo : Form     {...