Never underestimate the power of Passion!

Thursday, 2 March 2017

On 21:37 by Vardan Kumar in    No comments
Save Path C#

How to give save path through browse button c#

For an application to be user friendly, it is necessary to provide maximum accessibility features. Since our application should follow abstraction and should focus on hiding complexities to user, so a good GUI(Graphical user interface) with accessibility features will show your hard work while building the application. No matter how much you burn midnight oil while developing back-end of your application, if it lacks a good GUI with accessibility features it is definitely in vain.

If your application generates a file, which user is supposed to read or gather information then giving privilege of choosing the save  path for the file to user is always nice. Button control is the best way to do this.

Logic

  1. Firstly we'll create a button click event.
 private void btn_Browse_Click_1(object sender, EventArgs e)

2. Now we'll create an instance of SaveFileDIalog.

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

3.Now we'll set the filter for our save file dialog that is what kind of file user should be prompted to save which is shown under save as type drop down. Multiple filters are separated by vertical bar.

saveFileDialog1.Filter = "Excel|*.xlsx";

4.Now we'll give title to our dialog box.

 saveFileDialog1.Title = "Save Excel report";

5.Now we'll call ShowDialog method and if method return value seems correct then we'll assign the path to our desired label, off course we want to store the file path selected by user to use it further. If you don't want to display the path selected by user you can also use a string identifier to store the path.

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            lbl_Path.Text = saveFileDialog1.FileName;

Note: Don't forget to change other characteristics of label if any after changing its text such as tool tip etc.

Browse button save path c# Windows form application
Path Before clicking browse button


 
Save file dialog c#
Save File Dialog Box


Save path c# windows form application
Save Path after selecting path

Source Code 

 private void btn_Browse_Click_1(object sender, EventArgs e)
        {
            //Instance creation
            SaveFileDialog saveFileDialog_csp = new SaveFileDialog();
             // Seting filter 
            saveFileDialog_csp.Filter = "Excel|*.xlsx";
              // Giving title
            saveFileDialog_csp.Title = "Save Excel report";
             // show dialog box
            if (saveFileDialog_csp.ShowDialog() == DialogResult.OK)
             // Storing the path
            lbl_Path.Text = saveFileDialog_csp.FileName;
             // Changing tool tip
            tt.SetToolTip(lbl_Path, lbl_Path.Text);
             // Making label visible
            lbl_Path.Visible = true;
        }

0 comments:

Post a Comment