4.C#WinForm基础图片(显示和隐藏)

本文介绍了一种通过验证用户输入的身份证号码来判断年龄是否大于18岁的方法,并据此显示或隐藏图片。利用C#中的DateTime、Substring及Convert.ToInt32函数实现功能。

要求:

软件上有一张图片,默认是隐藏的。用户在文本框中输入身份证号(131226198105223452),点击按钮,如果年龄大于18岁,则显示图片。

知识点:

 取当前年份,Date Time Now Year
所需用到的函数

函数一:                       

          string string.Substring(int startIndex,int length)(+1重载)
                从此实例检索字符串。子字符串从指定的字符位置开始且具有指定的长度
          异常:
                      System.ArgumentOutOfRangeException

函数二:

          int Convert.toInt32(string value)(+18重载)   
            将数字的指定 System.String 表示形式转换为等效的32位有符号整数
               异常:
                      System.FormatException
                      System.OverflowException

函数三:
                              struct System.DateTime
                  表示时间的一刻,通常以日期和当天的时间表示。        

源码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 图片显示
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string 身份证号 = textBox1.Text; 
  
            /* string string.Substring(int startIndex,int length)(+1重载)
             * 从此实例检索字符串。子字符串从指定的字符位置开始且具有指定的长度
             * 
             * 异常:
             *       System.ArgumentOutOfRangeException
             */
            string strYear=身份证号.Substring(6,4);


            /* int Convert.toInt32(string value)(+18重载)   
             * 将数字的指定 System.String 表示形式转换为等效的32位有符号整数
             * 异常:
             *        System.FormatException
             *        System.OverflowException
             */

            int year = Convert.ToInt32(strYear);
         
            /*  struct System.DateTime
             * 表示时间的一刻,通常以日期和当天的时间表示。
             */
            
            if (DateTime.Now.Year - year > 18)
            {
                pictureBox1.Visible = true;
            }
            else 
            {
                pictureBox1.Visible = false;
            }
        }
    }
}

运行截图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值