C#根据身份证号获取生日,性别显示到另外的文本框,再根据获得的生日计算年龄显示到另一个文本框

本文介绍如何在C#中根据身份证号码获取用户的生日和性别,并将其显示在文本框中。同时,展示了如何计算并显示用户的年龄。在用户提交信息后,数据会被保存到后端数据库。

效果图:设置只读:文本框属性—>ReadOnly为true即可(性别框可以用textbox,毕竟是根据身份证获取到的,不可以更改)
在这里插入图片描述

点击提交按钮,将数据存到后端数据库

/// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
   
   
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = "Server=(local);Database=OnLine;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlConnection.Open();
            sqlCommand.CommandText =
            $"INSERT tb_Years(No,ID,dateTime,Yearss,Gender)"
               + $"VALUES"
               + $"('{this.textBox1.Text.Trim()}','{this.textBox2.Text.Trim()}','{this.textBox3.Text.Trim()}','{this.textBox4.Text.Trim()}','{this.comboBox1.Text.Trim()}')";
            Object obj = sqlCommand.ExecuteScalar();
            sqlConnection.Close();
            sqlCommand.Clone();
            
<think> 我们需要在C# WinForms应用程序中实现以下功能: 1. 有两个文本框(TextBox)和一个按钮(Button)。 2. 在第一个文本框中输入内容,点击按钮后,将第一个文本框中的内容显示在第二个文本框中。 步骤: 1. 创建一个Windows窗体应用程序项目。 2. 在窗体上放置两个文本框(例如:textBox1和textBox2)和一个按钮(例如:button1)。 3. 为按钮的Click事件创建事件处理程序。 4. 在事件处理程序中,将textBox1的Text属性赋值给textBox2的Text属性。 代码示例: 假设窗体类名为Form1,在Form1.Designer.cs中已经定义了这些控件(或者通过拖拽控件自动生成),我们需要在Form1.cs中编写按钮点击事件的代码。 以下是具体步骤的代码: </think>在C# WinForms中,将第一个文本框内容传递到第二个文本框的实现方法如下: ```csharp using System.Windows.Forms; namespace TextTransferExample { public partial class MainForm : Form { public MainForm() { InitializeComponent(); // 创建控件 TextBox sourceTextBox = new TextBox(); TextBox targetTextBox = new TextBox(); Button transferButton = new Button(); // 设置控件属性 sourceTextBox.Name = "sourceTextBox"; sourceTextBox.Location = new System.Drawing.Point(20, 20); sourceTextBox.Size = new System.Drawing.Size(200, 25); targetTextBox.Name = "targetTextBox"; targetTextBox.Location = new System.Drawing.Point(20, 60); targetTextBox.Size = new System.Drawing.Size(200, 25); targetTextBox.ReadOnly = true; // 设置为只读 transferButton.Text = "传输文本"; transferButton.Location = new System.Drawing.Point(20, 100); // 添加按钮点击事件 transferButton.Click += (sender, e) => { // 将源文本框内容复制到目标文本框 targetTextBox.Text = sourceTextBox.Text; // 可选:清空源文本框 // sourceTextBox.Clear(); }; // 添加控件到窗体 Controls.Add(sourceTextBox); Controls.Add(targetTextBox); Controls.Add(transferButton); // 设置窗体属性 Text = "文本传输示例"; ClientSize = new System.Drawing.Size(250, 150); } } } ``` **代码解释:** 1. **控件创建与布局**: - 创建两个文本框(`sourceTextBox`输入框,`targetTextBox`输出框) - 创建传输按钮 `transferButton` - 使用 `Location` 和 `Size` 属性设置控件位置和大小 2. **关键功能实现**: ```csharp transferButton.Click += (sender, e) => { targetTextBox.Text = sourceTextBox.Text; }; ``` - 使用Lambda表达式处理按钮点击事件 - 将源文本框的 `Text` 属性值赋给目标文本框 - `ReadOnly = true` 使目标文本框不可编辑 3. **扩展功能选项**: - 清空源文本框:取消注释 `sourceTextBox.Clear()` - 添加文本处理:可在赋值前添加处理逻辑(如 `ToUpper()` 转大写) **实际应用中的最佳实践**: 1. 使用Visual Studio的设计器创建控件更高效(拖拽控件自动生成代码) 2. 通过控件名称访问(设计器模式下): ```csharp private void transferButton_Click(object sender, EventArgs e) { targetTextBox.Text = sourceTextBox.Text; } ``` 3. 添加输入验证: ```csharp if (!string.IsNullOrWhiteSpace(sourceTextBox.Text)) { targetTextBox.Text = sourceTextBox.Text.Trim(); } else { MessageBox.Show("输入不能为空!"); } ``` **窗体设计器实现步骤**: 1. 创建Windows窗体应用项目 2. 从工具箱拖拽两个TextBox和一个Button到窗体 3. 设置目标文本框的ReadOnly属性为True 4. 双击按钮生成Click事件处理方法 5. 在事件处理方法中添加赋值代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值