简体   繁体   中英

Preselection in the save dialog (c#)

Goal:
Save a notepad file in the computer. (C#)

Problem:
I don't know how to make a preselection as "TXT Files(*.txt)" in the "Save as type:" when save dialog display?

// Fullmetalboy

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

namespace Labb2_application
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void mnuFileOpen_Click(object sender, EventArgs e)
        {

            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Öppna";
            fDialog.Filter = "Text files|*.txt";
            fDialog.InitialDirectory = @"C:\Windows";
            fDialog.ShowHelp = true;

            DialogResult result = fDialog.ShowDialog(); // Show the dialog and get result.

            if (result == DialogResult.OK)
            {
                string fileAdress = fDialog.FileName;

                try
                {
                    string textContent = File.ReadAllText(fileAdress);

                    rtxtDisplay.Text = textContent;
                }
                catch (IOException)
                {


                }



            } // If syntax

        }


        private void mnuFileSave_Click(object sender, EventArgs e)
        {
            saveAsFileDialog.ShowDialog();
        }






        private void mnuFileSaveAs_Click(object sender, EventArgs e)
        {

            saveAsFileDialog.Filter = "Text files|*.txt";
            saveAsFileDialog.ShowDialog();

        }


        private void mnuFileExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void saveAsFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            string fileNameAddress = saveAsFileDialog.FileName;

            File.WriteAllText(fileNameAddress, rtxtDisplay.Text);
        }










    } // Partial Class
}

You can select the currently active filter by specifying a FilterIndex .

In addition, you can specify the default file extension to use when saving by changing the DefaultExt property.

Off the top of my head, the file saveas dialog box protocol can be bit more non-intuitive as expected.

I think you need to parenthesis into the filter.

Best, J

This will create a new filter for your SaveFileDialog:

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Text files(*.txt)|*.txt"

The first part is the text shown to the user and the second one is the extension. If you want to add more, just do so by separating them by '|' character. The default behaviour is to select the first filter but if you want to change that, use "FilterIndex" property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM