开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 28|回复: 0
收起左侧

[C#源码] C# 中文对话框控件类

[复制链接]
发表于 2 小时前 | 显示全部楼层 |阅读模式   湖南省怀化市
using System.Windows.Forms;

namespace 中文控件库;

public static class 对话框
{
    public static string? 选择文件(string 标题 = "选择文件", string 过滤器 = "所有文件(*.*)|*.*")
    {
        using (OpenFileDialog dialog = new OpenFileDialog())
        {
            dialog.Title = 标题;
            dialog.Filter = 过滤器;
            dialog.CheckFileExists = true;

            if (dialog.ShowDialog() == DialogResult.OK)
                return dialog.FileName;
        }
        return null;
    }

    public static string[]? 选择多个文件(string 标题 = "选择文件", string 过滤器 = "所有文件(*.*)|*.*")
    {
        using (OpenFileDialog dialog = new OpenFileDialog())
        {
            dialog.Title = 标题;
            dialog.Filter = 过滤器;
            dialog.Multiselect = true;
            dialog.CheckFileExists = true;

            if (dialog.ShowDialog() == DialogResult.OK)
                return dialog.FileNames;
        }
        return null;
    }

    public static string? 保存文件(string 标题 = "保存文件", string 默认文件名 = "",
        string 过滤器 = "所有文件(*.*)|*.*")
    {
        using (SaveFileDialog dialog = new SaveFileDialog())
        {
            dialog.Title = 标题;
            dialog.FileName = 默认文件名;
            dialog.Filter = 过滤器;

            if (dialog.ShowDialog() == DialogResult.OK)
                return dialog.FileName;
        }
        return null;
    }

    public static string? 选择文件夹(string 标题 = "选择文件夹")
    {
        using (FolderBrowserDialog dialog = new FolderBrowserDialog())
        {
            dialog.Description = 标题;
            dialog.ShowNewFolderButton = true;

            if (dialog.ShowDialog() == DialogResult.OK)
                return dialog.SelectedPath;
        }
        return null;
    }

    public static string? 输入文本(string 提示 = "请输入", string 标题 = "输入",
        string 默认值 = "")
    {
        Form inputForm = new Form
        {
            Width = 400,
            Height = 150,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = 标题,
            StartPosition = FormStartPosition.CenterScreen,
            MaximizeBox = false,
            MinimizeBox = false
        };

        Label label = new Label { Left = 20, Top = 20, Text = 提示, Width = 350 };
        TextBox textBox = new TextBox
        {
            Left = 20,
            Top = 50,
            Width = 350,
            Text = 默认值
        };

        Button buttonOk = new Button
        {
            Text = "确定",
            Left = 200,
            Width = 80,
            Top = 85,
            DialogResult = DialogResult.OK
        };

        Button buttonCancel = new Button
        {
            Text = "取消",
            Left = 290,
            Width = 80,
            Top = 85,
            DialogResult = DialogResult.Cancel
        };

        buttonOk.Click += (s, e) => { inputForm.Close(); };
        buttonCancel.Click += (s, e) => { inputForm.Close(); };

        inputForm.Controls.Add(label);
        inputForm.Controls.Add(textBox);
        inputForm.Controls.Add(buttonOk);
        inputForm.Controls.Add(buttonCancel);
        inputForm.AcceptButton = buttonOk;
        inputForm.CancelButton = buttonCancel;

        if (inputForm.ShowDialog() == DialogResult.OK)
            return textBox.Text;

        return null;
    }

    public static string? 输入密码(string 提示 = "请输入密码", string 标题 = "输入密码")
    {
        Form inputForm = new Form
        {
            Width = 400,
            Height = 150,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = 标题,
            StartPosition = FormStartPosition.CenterScreen,
            MaximizeBox = false,
            MinimizeBox = false
        };

        Label label = new Label { Left = 20, Top = 20, Text = 提示, Width = 350 };
        TextBox textBox = new TextBox
        {
            Left = 20,
            Top = 50,
            Width = 350,
            UseSystemPasswordChar = true
        };

        Button buttonOk = new Button
        {
            Text = "确定",
            Left = 200,
            Width = 80,
            Top = 85,
            DialogResult = DialogResult.OK
        };

        Button buttonCancel = new Button
        {
            Text = "取消",
            Left = 290,
            Width = 80,
            Top = 85,
            DialogResult = DialogResult.Cancel
        };

        buttonOk.Click += (s, e) => { inputForm.Close(); };
        buttonCancel.Click += (s, e) => { inputForm.Close(); };

        inputForm.Controls.Add(label);
        inputForm.Controls.Add(textBox);
        inputForm.Controls.Add(buttonOk);
        inputForm.Controls.Add(buttonCancel);
        inputForm.AcceptButton = buttonOk;
        inputForm.CancelButton = buttonCancel;

        if (inputForm.ShowDialog() == DialogResult.OK)
            return textBox.Text;

        return null;
    }

    public static int? 选择列表项(string 提示, string 标题, params string[] 选项)
    {
        Form selectForm = new Form
        {
            Width = 350,
            Height = 250,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = 标题,
            StartPosition = FormStartPosition.CenterScreen,
            MaximizeBox = false,
            MinimizeBox = false
        };

        Label label = new Label { Left = 20, Top = 15, Text = 提示, Width = 300 };
        ListBox listBox = new ListBox
        {
            Left = 20,
            Top = 40,
            Width = 290,
            Height = 130
        };

        listBox.Items.AddRange(选项);
        if (选项.Length > 0)
            listBox.SelectedIndex = 0;

        Button buttonOk = new Button
        {
            Text = "确定",
            Left = 150,
            Width = 80,
            Top = 180,
            DialogResult = DialogResult.OK
        };

        Button buttonCancel = new Button
        {
            Text = "取消",
            Left = 240,
            Width = 80,
            Top = 180,
            DialogResult = DialogResult.Cancel
        };

        buttonOk.Click += (s, e) => { selectForm.Close(); };
        buttonCancel.Click += (s, e) => { selectForm.Close(); };

        selectForm.Controls.Add(label);
        selectForm.Controls.Add(listBox);
        selectForm.Controls.Add(buttonOk);
        selectForm.Controls.Add(buttonCancel);
        selectForm.AcceptButton = buttonOk;
        selectForm.CancelButton = buttonCancel;

        if (selectForm.ShowDialog() == DialogResult.OK)
            return listBox.SelectedIndex;

        return null;
    }

    public static Color? 选择颜色(Color 当前颜色)
    {
        using (ColorDialog dialog = new ColorDialog())
        {
            dialog.Color = 当前颜色;
            dialog.FullOpen = true;

            if (dialog.ShowDialog() == DialogResult.OK)
                return dialog.Color;
        }
        return null;
    }

    public static Font? 选择字体(Font 当前字体)
    {
        using (FontDialog dialog = new FontDialog())
        {
            dialog.Font = 当前字体;
            dialog.ShowApply = false;
            dialog.ShowEffects = true;

            if (dialog.ShowDialog() == DialogResult.OK)
                return dialog.Font;
        }
        return null;
    }

    public static DateTime? 选择日期(DateTime? 当前日期 = null, string 标题 = "选择日期")
    {
        Form dateForm = new Form
        {
            Width = 300,
            Height = 200,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = 标题,
            StartPosition = FormStartPosition.CenterScreen,
            MaximizeBox = false,
            MinimizeBox = false
        };

        DateTimePicker picker = new DateTimePicker
        {
            Left = 20,
            Top = 30,
            Width = 250,
            Format = DateTimePickerFormat.Short
        };

        if (当前日期.HasValue)
            picker.Value = 当前日期.Value;

        Button buttonOk = new Button
        {
            Text = "确定",
            Left = 100,
            Width = 80,
            Top = 120,
            DialogResult = DialogResult.OK
        };

        Button buttonCancel = new Button
        {
            Text = "取消",
            Left = 190,
            Width = 80,
            Top = 120,
            DialogResult = DialogResult.Cancel
        };

        buttonOk.Click += (s, e) => { dateForm.Close(); };
        buttonCancel.Click += (s, e) => { dateForm.Close(); };

        dateForm.Controls.Add(picker);
        dateForm.Controls.Add(buttonOk);
        dateForm.Controls.Add(buttonCancel);
        dateForm.AcceptButton = buttonOk;
        dateForm.CancelButton = buttonCancel;

        if (dateForm.ShowDialog() == DialogResult.OK)
            return picker.Value;

        return null;
    }

    public static (DateTime? 开始日期, DateTime? 结束日期)? 选择日期范围(string 标题 = "选择日期范围")
    {
        Form rangeForm = new Form
        {
            Width = 350,
            Height = 180,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = 标题,
            StartPosition = FormStartPosition.CenterScreen,
            MaximizeBox = false,
            MinimizeBox = false
        };

        Label label1 = new Label { Left = 20, Top = 20, Text = "开始日期:", Width = 70 };
        DateTimePicker startPicker = new DateTimePicker
        {
            Left = 100,
            Top = 17,
            Width = 200,
            Format = DateTimePickerFormat.Short
        };

        Label label2 = new Label { Left = 20, Top = 60, Text = "结束日期:", Width = 70 };
        DateTimePicker endPicker = new DateTimePicker
        {
            Left = 100,
            Top = 57,
            Width = 200,
            Format = DateTimePickerFormat.Short
        };

        Button buttonOk = new Button
        {
            Text = "确定",
            Left = 150,
            Width = 80,
            Top = 110,
            DialogResult = DialogResult.OK
        };

        Button buttonCancel = new Button
        {
            Text = "取消",
            Left = 240,
            Width = 80,
            Top = 110,
            DialogResult = DialogResult.Cancel
        };

        buttonOk.Click += (s, e) => { rangeForm.Close(); };
        buttonCancel.Click += (s, e) => { rangeForm.Close(); };

        rangeForm.Controls.Add(label1);
        rangeForm.Controls.Add(startPicker);
        rangeForm.Controls.Add(label2);
        rangeForm.Controls.Add(endPicker);
        rangeForm.Controls.Add(buttonOk);
        rangeForm.Controls.Add(buttonCancel);
        rangeForm.AcceptButton = buttonOk;
        rangeForm.CancelButton = buttonCancel;

        if (rangeForm.ShowDialog() == DialogResult.OK)
            return (startPicker.Value, endPicker.Value);

        return null;
    }
}


download.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备2025452707号) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表