C#中Windows 窗体对话框组件SaveFileDialog 、OpenFileDialog、ColorDialog、FontDialog和FolderBrowserDialog

目录

一、SaveFileDialog 组件(Windows 窗体)

二、OpenFileDialog 组件(Windows 窗体)

三、ColorDialog 组件(Windows 窗体)

1.更改 Windows 窗体 ColorDialog 组件的外观

2.使用 ColorDialog 组件显示调色板

四、FontDialog 组件(Windows 窗体)

五、FolderBrowserDialog 组件(Windows 窗体)


        汇总一下几个常用的对话框组件,省的需要的时候到处去找。

        所谓组件:就是要在Windows窗体(.NET Framework 4.x)上使用的控件。

        要使用的控件 - Windows Forms .NET Framework | Microsoft Learn  https://learn.microsoft.com/zh-cn/dotnet/desktop/winforms/controls/controls-to-use-on-windows-forms?view=netframeworkdesktop-4.8

一、SaveFileDialog 组件(Windows 窗体)

         详见作者文章:

         写文章-优快云创作中心  https://mp.youkuaiyun.com/mp_blog/creation/editor/134655503

二、OpenFileDialog 组件(Windows 窗体)

        显示一个标准对话框,提示用户打开文件。 此类不能被继承。

        详见作者文章:

        写文章-优快云创作中心  https://mp.youkuaiyun.com/mp_blog/creation/editor/134655503

       C#中openFileDialog控件的使用方法-优快云博客  https://blog.youkuaiyun.com/wenchm/article/details/134630583?spm=1001.2014.3001.5501

三、ColorDialog 组件(Windows 窗体)

        Windows 窗体 ColorDialog 组件是一个预先配置的对话框,用户利用它可从调色板选择颜色以及将自定义颜色添加到该调色板。 此对话框与在其他基于 Windows 的应用程序中看到的用于选择颜色的对话框相同。 在基于 Windows 的应用程序中,使用它作为一种替代配置自己对话框的简单解决方案。

1.更改 Windows 窗体 ColorDialog 组件的外观

        将 AllowFullOpen、AnyColor、SolidColorOnly 和 ShowHelp 属性设置为所需的值。

colorDialog1.AllowFullOpen = true;  
colorDialog1.AnyColor = true;  
colorDialog1.SolidColorOnly = false;  
colorDialog1.ShowHelp = true;

2.使用 ColorDialog 组件显示调色板

//colorDialog1 
namespace WinFormsApp4
{
    public partial class Form1 : Form
    {
        private readonly Button? button1;
        private readonly ColorDialog? colorDialog1;

        public Form1()
        {
            InitializeComponent();

            //按钮控件及事件
            button1 = new Button()
            {
                Location = new Point(167, 90),
                Name = "button1",
                Size = new Size(80, 23),
                Text = "选择字体",
                UseVisualStyleBackColor = true,
            };
            button1.Click += new EventHandler(Button1_Click);

            //fontDialog1控件
            colorDialog1 = new ColorDialog();

            //Form1()
            ClientSize = new Size(265, 170);
            StartPosition = FormStartPosition.CenterScreen;
            Name = "Form1";
            Text = "colorDialog1_test";
            Controls.Add(button1);
            PerformLayout();
        }
        /// <summary>
        /// 按钮背景色=颜色对话框选定的字体
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            if (colorDialog1?.ShowDialog() == DialogResult.OK)
            {
                button1!.BackColor = colorDialog1.Color;
            }
        }
    }
}

 

四、FontDialog 组件(Windows 窗体)

        Windows 窗体 FontDialog 组件是一个预先配置的对话框,这一标准 Windows“字体”对话框用于公开系统上当前安装的字体。 在基于 Windows 的应用程序中,使用它作为一种替代配置自己对话框的简单字体选择解决方案。

        默认情况下,对话框显示“字体”、“字体样式”和“大小”的列表框;“删除线”和“下划线”等效果的复选框;脚本的下拉列表;以及字体将如何显示的示例。

//fontDialog1_test
namespace WinFormsApp3
{
    public partial class Form1 : Form
    {
        private readonly TextBox? textBox1;      
        private readonly Button? button1;
        private readonly FontDialog? fontDialog1;

        public Form1()
        {
            InitializeComponent();

            //文本框控件
            textBox1 = new TextBox()
            {
                Location = new Point(81, 15),
                Name = "textBox1",
                Size = new Size(145, 21),
                Text = "",
            };

            //按钮控件及事件
            button1 = new Button()
            {
                Location = new Point(167, 90),
                Name = "button1",
                Size = new Size(80, 23),
                Text = "选择字体",
                UseVisualStyleBackColor = true,              
            };
            button1.Click += new EventHandler(Button1_Click);

            //fontDialog1控件
            fontDialog1 = new FontDialog();

            //Form1()
            ClientSize = new Size(265, 170);
            StartPosition = FormStartPosition.CenterScreen;
            Name = "Form1";
            Text = "fontDialog1_test";
            Controls.Add(textBox1);
            Controls.Add(button1);
            PerformLayout();
        }
        /// <summary>
        /// 文本框文本字体=字体对话框选定的字体
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            if (fontDialog1?.ShowDialog() == DialogResult.OK)
            {
                textBox1!.Font = fontDialog1.Font;
            }
        }
    }
}

         先在文本框汇总输入一些文字,然后选择字体,则文本框中的文字的字体变更为选择的字体。

 

五、FolderBrowserDialog 组件(Windows 窗体)

        Windows 窗体 FolderBrowserDialog 组件显示一个界面,用户可以通过该界面浏览和选择文件夹或创建新文件夹。 该界面是对用于浏览和选择文件的 OpenFileDialog 组件组件的补充。         

提示用户选择文件夹。 此类不能被继承。

         详见作者文章:

         C#文件夹基本操作(判断文件夹是否存在、创建文件夹、移动文件夹、删除文件夹以及遍历文件夹中的文件)-优快云博客  https://blog.youkuaiyun.com/wenchm/article/details/134633343?spm=1001.2014.3001.5501

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wenchm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值