winfrom窗体应用

winform窗体输入身份证号码从txt文档中获取省份信息并判断出性别和出生年月

img

private void button1_Click(object sender, EventArgs e)
{
    idNumber = textBox1.Text;
    
    if (idNumber.Length != 18)
    {
        MessageBox.Show("身份证号码无效!");
        return;
    }
    
    var provinceData = System.File.IO.ReadAllLines("sfzsb.txt", System.Text.Encoding.UTF8)
        .ToDictionary(x => x.Split(',')[0], x => x.Split(',')[1]);
    string province = provinceData[idNumber.Substring(0, 2)];  
    
    string genderDigit = idNumber.Substring(16, 1);
    string gender = "";

    if (int.Parse(genderDigit) % 2 == 0)
        gender = "女";
    else
        gender = "男";

    string year = idNumber.Substring(6, 4);
    string month = idNumber.Substring(10, 2);
    string day = idNumber.Substring(12, 2);
    string birthday = $"{year}-{month}-{day}";

    MessageBox.Show($"省份:{province}\n性别:{gender}\n生日:{birthday}");
}

  • 这篇博客: C# Winfrom 常用功能整合-1中的 Winform 用线程写入TXT文件,并更新UI和进度 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 效果:

    UI控件定义如下

    共有三个控件,一个按钮:Button_Write,一个进度条:ProgressBar_WriteProgress,一个Label:Label_WriteStatus

    新建脚本 DataWrite 用来管理写入操作

    using System;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
     
    namespace Winform_vs2022
    {
        public class DataWrite
        {
            //声明一个更新主线程的委托
            public delegate void UpdateUI(int step);
            public UpdateUI UpdateUIDelegate;
            //声明一个在完成任务时通知主线程的委托
            public delegate void AccomplishTask();
            public AccomplishTask TaskEndCallBack;
     
            private string Path = Application.StartupPath + "\\test.txt";
     
            public void Write(object lineCount)
            {
                StreamWriter writeIO = new StreamWriter(Path, false, Encoding.GetEncoding("gb2312"));
                string head = "编号,省,市";
                writeIO.Write(head);
                for (int i = 0; i < (int)lineCount; i++)
                {
                    writeIO.WriteLine(i.ToString() + ",湖南,衡阳");
                    //写入一条数据,调用更新主线程ui状态的委托
                    UpdateUIDelegate(1);
                }
                //任务完成时通知主线程作出相应的处理
                TaskEndCallBack();
                writeIO.Close();
            }
     
            public DataWrite()
            {
                if (!File.Exists(Path))
                {
                    Console.WriteLine("文件不存在");
                }
            }
        }
    }

    Form1.cs

    
    using System;
    using System.Threading;
    using System.Windows.Forms;
     
    namespace Winform_vs2022
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private delegate void AsynUpdateUI(int step);
            private DataWrite DataWrite = new DataWrite();
     
            private void Button_Write_Click(object sender, EventArgs e)
            {
                //任务量为10000
                int taskCount = 10000; 
                this.ProgressBar_WriteProgress.Maximum = taskCount;
                this.ProgressBar_WriteProgress.Value = 0;
                //绑定更新任务状态的委托
                DataWrite.UpdateUIDelegate += UpdataUIStatus;
                //绑定完成任务要调用的委托
                DataWrite.TaskEndCallBack += Accomplish;
     
                Thread thread = new Thread(new ParameterizedThreadStart(DataWrite.Write));
                thread.IsBackground = true;
                thread.Start(taskCount);
            }
     
            //更新UI
            private void UpdataUIStatus(int step)
            {
                //如果是跨线程访问,就切换线程,否则直接更新UI
                if (this.InvokeRequired)
                {
                    this.Invoke(new AsynUpdateUI(delegate (int s)
                    {
                        this.ProgressBar_WriteProgress.Value += s;
                        this.Label_WriteStatus.Text = this.ProgressBar_WriteProgress.Value.ToString() + "/" + this.ProgressBar_WriteProgress.Maximum.ToString();
                    }), step);
                }
                else
                {
                    this.ProgressBar_WriteProgress.Value += step;
                    this.Label_WriteStatus.Text = this.ProgressBar_WriteProgress.Value.ToString() + "/" + this.ProgressBar_WriteProgress.Maximum.ToString();
                }
            }
     
            //完成任务时需要调用
            private void Accomplish()
            {
                MessageBox.Show("任务完成");
            }
        }
    }

    这里可以看出:

    更新UI数据用的是委托,和网上一些帖子的区别是,这里在UI线程中定义了一个委托进行执行的,而进度条的进度则是遍历时,for循环中的 i 的值,线程除了UI线程之外,也只是开启了一个线程

    源码下载地址:点击下载