当前位置: 首页 > article >正文

C#高级:Winform中的自定义窗体输入

目录

一、多样式输入(无封装)

1.代码

2.效果 

二、单输入框封装

1.使用

2.封装

3.效果

三、组合框批量输入封装

1.使用

2.封装

3.效果


一、多样式输入(无封装)

1.代码

private async void button1_Click(object sender, EventArgs e)
{
    // 在方法中直接创建一个新的窗体
    Form inputForm = new Form
    {
        Text = "输入对话框",
        Width = 300,
        Height = 300,
        MaximizeBox  = false,
        StartPosition = FormStartPosition.CenterScreen
    };

    // 创建控件
    Label textlabel = new Label { Text = "文本输入:", Width =80,Location = new System.Drawing.Point(20, 15) };
    TextBox textBox = new TextBox { Location = new System.Drawing.Point(110, 10), Width = 150 };
    CheckBox checkBox = new CheckBox { Text = "是否选中", Location = new System.Drawing.Point(20, 60) };
    RadioButton radioButton1 = new RadioButton { Text = "选项 1", Location = new System.Drawing.Point(20, 100), Checked = true };
    RadioButton radioButton2 = new RadioButton { Text = "选项 2", Location = new System.Drawing.Point(130, 100) };
    ComboBox comboBox = new ComboBox
    {
        Location = new System.Drawing.Point(20, 140),
        Width = 200,
        Items = { "选项 A", "选项 B", "选项 C" }
    };
    comboBox.SelectedIndex = 0; // 默认选中第一项

    NumericUpDown numericUpDown = new NumericUpDown
    {
        Location = new System.Drawing.Point(20, 180),
        Width = 200,
        Minimum = 0,
        Maximum = 100,
        Value = 10
    };

    // 创建确认按钮
    Button btnOK = new Button
    {
        Text = "确定",
        Height=30,
        Location = new System.Drawing.Point(20, 220)
    };
    btnOK.Click += (s, args) =>
    {
        // 获取控件的值
        string textBoxInput = textBox.Text;
        bool checkBoxChecked = checkBox.Checked;
        string radioButtonSelection = radioButton1.Checked ? "选项 1" : "选项 2";
        string comboBoxSelection = comboBox.Text;
        decimal numericValue = numericUpDown.Value;

        // 显示结果
        MessageBox.Show($"你输入的内容:\n" +
            $"文本框: {textBoxInput}\n" +
            $"复选框: {checkBoxChecked}\n" +
            $"单选框: {radioButtonSelection}\n" +
            $"下拉框: {comboBoxSelection}\n" +
            $"数字框: {numericValue}");

        // 关闭窗体
        inputForm.Close();
    };

    // 创建取消按钮
    Button btnCancel = new Button
    {
        Text = "取消",
        Height = 30,
        Location = new System.Drawing.Point(120, 220)
    };
    btnCancel.Click += (s, args) =>
    {
        // 取消操作,关闭窗体
        inputForm.Close();
    };

    // 将控件添加到窗体
    inputForm.Controls.Add(textlabel);
    inputForm.Controls.Add(textBox);
    inputForm.Controls.Add(checkBox);
    inputForm.Controls.Add(radioButton1);
    inputForm.Controls.Add(radioButton2);
    inputForm.Controls.Add(comboBox);
    inputForm.Controls.Add(numericUpDown);
    inputForm.Controls.Add(btnOK);
    inputForm.Controls.Add(btnCancel);

    // 显示窗体
    inputForm.ShowDialog();
}

2.效果 

二、单输入框封装

1.使用

异步方法返回结果需要通过.Result获取

//点击触发自定义弹窗输入
private async void button1_Click(object sender, EventArgs e)
{
    var result = GetTextInput("请输入您的尊姓大名").Result;
}

2.封装

//新建自定义窗体,输入提示语,是否采用文本框(false=采用数字框)
private async Task<string> GetTextInput(string laebl,bool isUseTextBox=true)
{
    //多少个输入框:
    var InputLength = laebl.Length;
    int labelwidth = InputLength * 20 + 5;

    // 在方法中直接创建一个新的窗体
    Form inputForm = new Form
    {
        Text = "请输入",
        Width = labelwidth+300,
        Height = 180,
        MaximizeBox = false,
        StartPosition = FormStartPosition.CenterScreen
    };

    // 创建控件
    
    Label label = new Label { Text = laebl + ":", Width = labelwidth, Location = new System.Drawing.Point(20, 23), BackColor = Color.Transparent };
    TextBox textBox = new TextBox();
    NumericUpDown numericUpDown = new NumericUpDown();
    inputForm.Controls.Add(label);
    int input_x = labelwidth + 20;
    if (isUseTextBox)
    {
        textBox  = new TextBox { Location = new System.Drawing.Point(input_x, 20), Width = 200 };
        inputForm.Controls.Add(textBox);
    }
    else
    {
        numericUpDown = new NumericUpDown
        {
            Location = new System.Drawing.Point(input_x, 20),
            Width = 200,
            Minimum = 0,
            Maximum = 100000000000,
            Value = 1
        };
        inputForm.Controls.Add(numericUpDown);
    }


    // 创建确认按钮
    Button btnOK = new Button
    {
        Text = "确定",
        Height = 30,
        Location = new System.Drawing.Point(input_x, 70)
    };
    string result = "";
    btnOK.Click += (s, args) =>
    {
        //结果赋值
        result = isUseTextBox ? textBox.Text : numericUpDown.Value.ToString();
        // 关闭窗体
        inputForm.Close();
    };

    // 创建取消按钮
    Button btnCancel = new Button
    {
        Text = "取消",
        Height = 30,
        Location = new System.Drawing.Point(input_x+100, 70)
    };
    btnCancel.Click += (s, args) =>
    {
        // 取消操作,关闭窗体
        inputForm.Close();
    };

    // 将btn控件添加到窗体
    inputForm.Controls.Add(btnOK);
    inputForm.Controls.Add(btnCancel);

    // 显示窗体
    inputForm.ShowDialog();
    return result;
}

3.效果

三、组合框批量输入封装

1.使用

异步方法返回结果需要通过.Result获取

 //点击触发自定义弹窗输入
 private async void button1_Click(object sender, EventArgs e)
 {
     List<Dictionary<string, List<string>>> list = new List<Dictionary<string, List<string>>>
     {
         new Dictionary<string, List<string>> { { "选择一", new List<string> { "1", "2", "3" } } },
         new Dictionary<string, List<string>> { { "选择二", new List<string> { "4", "5", "6" } } },
         new Dictionary<string, List<string>> { { "选择三", new List<string> { "7", "8", "9" } } }
     };
     var result = GetComboboxInput(list).Result;
 }

2.封装

//新建自定义窗体,提示语:输入框选项
private async Task<List<string>> GetComboboxInput(List<Dictionary<string, List<string>>> list)
{
    //多少个输入框:
    var InputCount = list.Count;
    var InputLabels = list.SelectMany(x => x.Keys).ToList();
    var InputValues = list.SelectMany(x => x.Values).ToList();
    int addheight = 50;

    // 在方法中直接创建一个新的窗体
    Form inputForm = new Form
    {
        Text = "请输入",
        Width = 350,
        Height = InputCount * addheight + 130,
        MaximizeBox = false,
        StartPosition = FormStartPosition.CenterScreen
    };

    // 创建控件
    List<Label> LaeblList = new List<Label>();
    List<ComboBox> ComboBoxList = new List<ComboBox>();
    int y = 20;
    for (int i = 0; i < InputCount; i++)
    {
        Label en_label = new Label { Text = InputLabels[i] + ":", Width = 80, Location = new System.Drawing.Point(20, y+3),BackColor=Color.Transparent };
        ComboBox en_com = new ComboBox { DataSource = InputValues[i], Width = 200, Location = new System.Drawing.Point(100, y) };
        LaeblList.Add(en_label);
        ComboBoxList.Add(en_com);
        inputForm.Controls.Add(en_label);
        inputForm.Controls.Add(en_com);
        y += addheight;

    }

    // 创建确认按钮
    int btn_y = InputCount * addheight + 20;
    Button btnOK = new Button
    {
        Text = "确定",
        Height = 30,
        Location = new System.Drawing.Point(80, btn_y)
    };
    List<string> result = new List<string>();
    btnOK.Click += (s, args) =>
    {
        //结果赋值
        result = ComboBoxList.Select(x => x.Text).ToList();
        // 关闭窗体
        inputForm.Close();
    };

    // 创建取消按钮
    Button btnCancel = new Button
    {
        Text = "取消",
        Height = 30,
        Location = new System.Drawing.Point(180, btn_y)
    };
    btnCancel.Click += (s, args) =>
    {
        // 取消操作,关闭窗体
        inputForm.Close();
    };

    // 将btn控件添加到窗体
    inputForm.Controls.Add(btnOK);
    inputForm.Controls.Add(btnCancel);

    // 显示窗体
    inputForm.ShowDialog();
    return result;
}
如果输入的label太长,请手动调节窗体宽度、combobox的x坐标

3.效果


http://www.kler.cn/a/404892.html

相关文章:

  • 4.langchain中的prompt模板 (partially format prompt templates)
  • 图的存储、遍历以及Dijkstra/Floyd/Kruskal/Prim/拓扑排序/关键路径(实验8--作业)
  • Banana Pi BPI-CanMV-K230D-Zero 采用嘉楠科技 K230D RISC-V芯片设计
  • NVR接入录像回放平台EasyCVR视频融合平台加油站监控应用场景与实际功能
  • Redis五大基本类型——List列表命令详解(命令用法详解+思维导图详解)
  • core 不可变类型 线程安全 record
  • 向量数据库FAISS之四:向量检索和 FAISS
  • OpenCV基础(2)
  • React基础知识一
  • P8692 [蓝桥杯 2019 国 C] 数正方形:结论,组合数学
  • 使用ENSP实现静态路由
  • CPU详细介绍
  • Grafana监控PostgreSQL
  • 机器学习系列----关联分析
  • 京东最新黑边背景旋转验证码识别
  • ApiChain 从迭代到项目 接口调试到文档生成单元测试一体化工具
  • Docker+fastapi
  • 2.预备知识
  • SentenceTransformers×Milvus:如何进行向量相似性搜索
  • SAP PI/PO Proxy2JDBC SQL_QUERY动态接口示例
  • H.264/H.265播放器EasyPlayer.js视频流媒体播放器关于websocket1006的异常断连
  • 视频流媒体播放器EasyPlayer.js无插件直播流媒体音视频播放器Android端webview全屏调用无效问题
  • Hello-Go
  • 腾讯云单元化架构体系介绍
  • 深入探索Solana链上的Meme生态:创新、潜力与挑战#区块链开发#dapp开发
  • xml和xpath