using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 身份证判断是否可见图片
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnChakan_Click(object sender, EventArgs e)
{
string yearString = "0"; //用来记住年
//判断15位的身份证
if (txtShengfenzheng.Text.Trim().Length == 15 )
{
yearString = "19" + txtShengfenzheng.Text.Substring(6, 2);
}
//判断18位的身份证
else if (txtShengfenzheng.Text.Trim().Length == 18)
{
if (!ShenFenPanDuane(txtShengfenzheng.Text))
{
MessageBox.Show("身份证输入有误,请重新输入!");
txtShengfenzheng.Focus();
return;
}
yearString = txtShengfenzheng.Text.Substring(6, 4);
}
else
{
MessageBox.Show("身份证位数有误!请重新输入哦!");
txtShengfenzheng.Focus();
return;
}
int shijiancha =DateTime.Now.Year - Convert.ToInt32(yearString) ;
if (shijiancha < 16)
{
MessageBox.Show("你是未成年人,不能观看!","提示!");
return;
}
else if (btnChakan.Text == "查看")
{
if (shijiancha >= 16 && shijiancha < 18)
{
DialogResult re = MessageBox.Show("你是未成年人哦,你确定要观看吗??", "提示!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (re == DialogResult.OK)
{
btnChakan.Text = "关闭";
PB1.Visible = true;
}
}
else
{
btnChakan.Text = "关闭";
PB1.Visible = true;
}
}
else if (btnChakan.Text == "关闭")
{
btnChakan.Text = "查看";
PB1.Visible = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
PB1.Visible = false;
PB1.Image= Image.FromFile("1.jpg");
}
private void txtShengfenzheng_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar !='x' || e.KeyChar !='X'))
{
e.Handled = true; //让我无法输入
}
if (e.KeyChar == 8)
{
e.Handled = false;
}
}
private bool ShenFenPanDuane(string id) //判断十八位的身份证是否正确
{
int[] xishu = { 7 ,9 ,10, 5 ,8 ,4 ,2 ,1 ,6 ,3 ,7 ,9 ,10, 5 ,8 ,4 ,2};
string number17 = id.Substring(0, 17); //身份证的前17位
string number18 = id.Substring(17); //身份证的第18位
string checkwei = "10X98765432";
int sum = 0; //统计求和
for (int i = 0; i < 17; i++)
{
sum = sum + Convert.ToInt32(number17[i].ToString()) * xishu[i];
}
int mod = sum % 11;
string result = checkwei[mod].ToString();
if (number18.Equals(result, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
private void txtShengfenzheng_TextChanged(object sender, EventArgs e)
{
PB1.Visible = false;
}
}
}
|