• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

要求:

软件上有一张图片,默认是隐藏的。用户在文本框中输入身份证号(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;
            }
        }
    }
}

运行截图:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#调用cmd执行相关命令发布时间:2022-07-13
下一篇:
Winform(C#)限制程序只运行一个实例发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap