简体   繁体   English

在保存对话框中预选(C#)

[英]Preselection in the save dialog (c#)

Goal: 目标:
Save a notepad file in the computer. 将记事本文件保存在计算机中。 (C#) (C#)

Problem: 问题:
I don't know how to make a preselection as "TXT Files(*.txt)" in the "Save as type:" when save dialog display? 保存对话框显示时,我不知道如何在“另存为类型:”中预先选择为“ TXT Files(*。txt)”?

// Fullmetalboy // 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 . 您可以通过指定FilterIndex来选择当前活动的过滤器。

In addition, you can specify the default file extension to use when saving by changing the DefaultExt property. 此外,可以通过更改DefaultExt属性来指定保存时使用的默认文件扩展名。

Off the top of my head, the file saveas dialog box protocol can be bit more non-intuitive as expected. 让我烦恼的是,文件saveas对话框协议可能比预期的更不直观。

I think you need to parenthesis into the filter. 我认为您需要在过滤器中加上括号。

Best, J 最好,J

This will create a new filter for your SaveFileDialog: 这将为您的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. 默认行为是选择第一个过滤器,但是如果要更改它,请使用“ FilterIndex”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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