• C# Winform编程(3)对话框


    消息提示对话框(MessageBox)

    • Show(string text);
    • Show(string text, string caption);
    • Show(string text, string caption, MessageBoxButtons buttons);
    • Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon);
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 对话框
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("只有消息文本的对话框!");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题");
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.OK);
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.OKCancel);
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.YesNo);
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.YesNoCancel);
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.RetryCancel);
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.AbortRetryIgnore);
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
    
            private void button10_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }
    
            private void button11_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            }
    
            private void button12_Click(object sender, EventArgs e)
            {
                MessageBox.Show("消息文本", "标题", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    在这里插入图片描述

    保存文件对话框(SaveFileDialog)

    保存文件SaveFileDialog类的使用方法使用的命名空间是:System.Windows.Forms.SaveFileDialog;

    常用属性:

    • Title:保存对话框的标题,默认为"另存为"。
    • InitialDirectory:设置文件选择框开始打开的位置。
    • Filter:设置保存文件的类型。
    • DefaultExt:设置默认文件拓展名。
    • FileName:保存文件默认的文件名。
    • RestoreDirectory:设置是否记忆上次文件保存的路径,true表示记住。

    示例代码

            private void button1_Click(object sender, EventArgs e)
            {
                saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                saveFileDialog1.Filter = "audio files (*.wav)|*.wav| all files (*.*)|*.*"; // 文件类型过滤
                saveFileDialog1.DefaultExt = "*.wav"; // 默认文件扩展名
                //saveFileDialog1.FileName = "1.wav"; // 默认文件名
    
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                { 
                    string fName = saveFileDialog1.FileName; // 获取文件名
                    textBox1.Text = fName;
                    button2.Enabled = true;
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    打开文件对话框(OpenFileDialog)

    保存文件SaveFileDialog类的使用方法使用的命名空间是:System.Windows.Forms.OpenFileDialog;

    属性:

    • InitialDirectory:对话框的初始目录
    • Filter: 获取或设置当前文件名筛选器字符串,例如,“文本文件(.txt)|.txt|所有文件(.)||.
    • FilterIndex 在对话框中选择的文件筛选器的索引,如果选第一项就设为1
    • RestoreDirectory 控制对话框在关闭之前是否恢复当前目录
    • FileName:第一个在对话框中显示的文件或最后一个选取的文件
    • Title 将显示在对话框标题栏中的字符
    • AddExtension 是否自动添加默认扩展名
    • CheckPathExists 在对话框返回之前,检查指定路径是否存在
    • DefaultExt 默认扩展名
    • DereferenceLinks 在从对话框返回前是否取消引用快捷方式
    • ShowHelp 启用"帮助"按钮
    • ValiDateNames 控制对话框检查文件名中是否不含有无效的字符或序列

    获取打开文件对话框文件名:

    • openfiledialog.FileName :获取或设置一个包含在文件对话框中选定的文件名字符串
    • openfiledialog.SafeFileName :获取选定对话框中的文件名和扩展名
    private void openFileDialogBTN_Click(object sender, System.EventArgs e)
    {
        OpenFileDialog openFileDialog=new OpenFileDialog();
        openFileDialog.InitialDirectory=@"d:\";  // 或d:\\
        
        openFileDialog.Filter="文本文件|*.*|C#文件|*.cs|所有文件|*.*";
        openFileDialog.RestoreDirectory=true;
        openFileDialog.FilterIndex=1;
        if (openFileDialog.ShowDialog()==DialogResult.OK)
        {
            fName=openFileDialog.FileName;
            File fileOpen=new File(fName);
            isFileHaveName=true;
            richTextBox1.Text=fileOpen.ReadFile();
            richTextBox1.AppendText("");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    打开文件夹对话框(FolderBrowserDialog)

    FolderBrowserDialog dialog = new FolderBrowserDialog();
    dialog.Description = "请选择文件路径";
    
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        savePath = dialog.SelectedPath;
        textBox2.Text = savePath;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    颜色对话框(ColorDialog)

    ColorDialog控件用于创建一个颜色选择对话框,允许用户选择颜色。ColorDialog提供了一种简单的方式来让用户选择颜色,并获取用户所选颜色的值。

    创建并显示颜色选择对话框:

    • 创建一个 ColorDialog 对象实例:ColorDialog colorDialog1 = new ColorDialog();
    • 调用 colorDialog1.ShowDialog() 方法来显示颜色选择对话框。

    处理用户的颜色选择:

    • 在用户选择颜色后,ColorDialog 对象的 Color 属性将包含用户所选颜色的值。
    • 使用 colorDialog1.Color 属性来获取用户所选的颜色。
    private void button1_Click(object sender, EventArgs e)
    {
        // 创建颜色选择对话框
        ColorDialog colorDialog1 = new ColorDialog();
    
        // 显示颜色选择对话框
        DialogResult result = colorDialog1.ShowDialog();
    
        // 处理用户的颜色选择
        if (result == DialogResult.OK)
        {
            // 获取用户所选颜色
            Color selectedColor = colorDialog1.Color;
    
            // 在 label1 中显示所选颜色的 RGB 值
            label1.Text = "所选颜色的 RGB 值为:" + selectedColor.R + ", " + selectedColor.G + ", " + selectedColor.B;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    字体对话框(FontDialog)

    FontDialog控件用于创建一个字体选择对话框,允许用户选择字体样式。FontDialog提供了一种简单的方式来让用户选择字体,并获取用户所选字体的相关信息。

    创建并显示字体选择对话框:

    • 创建一个 FontDialog 对象实例:FontDialog fontDialog1 = new FontDialog();
    • 调用 fontDialog1.ShowDialog() 方法来显示字体选择对话框。

    处理用户的字体选择:

    • 在用户选择字体后,FontDialog 对象的 Font 属性将包含用户所选字体的信息。
    • 使用 fontDialog1.Font 属性来获取用户所选的字体。
     private void button1_Click(object sender, EventArgs e)
     {
          // 创建字体选择对话框
          FontDialog fontDialog1 = new FontDialog();
    
          // 显示字体选择对话框
          DialogResult result = fontDialog1.ShowDialog();
    
          // 处理用户的字体选择
          if (result == DialogResult.OK)
          {
              // 获取用户所选字体
              Font selectedFont = fontDialog1.Font;
    
              // 在 label1 中显示所选字体的样式
              label1.Font = selectedFont;
              label1.Text = "所选字体样式为:" + selectedFont.Name + ", " + selectedFont.Size + "pt";
          }
      }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    比 O(nlog(n)) 做得更好——2.改变问题以及排序和填充数组
    TCP网络编程入门
    不同版本的谷歌浏览器跨域怎么设置?
    RocketMQ 消息投递解析—— 时序图、调用链、源码级解析
    二叉树知识点
    URP渲染管线场景优化实战 2.3静态资源导入及优化——Texture
    RPC及Dubbo和ZooKeeper的安装
    第十九章绘图
    【语义分割】FCN
    tiup dm reload
  • 原文地址:https://blog.csdn.net/u013420428/article/details/133864756