默认确认按钮: AcceptButton
默认取消按钮: CancelButton
private void txtstart_KeyPress(object sender, KeyPressEventArgs e)
{
// sender存储触发该事件的对象,可以强制转换过来。
TextBox txt = sender as TextBox; //让所有的TextBox空间公用次事件
//只能输入0-9的数字
if (e.KeyChar < '0' || e.KeyChar > '9') //KeyChar当前读取的字符
e.Handled = true;//阻止读取该字符
//可以使用退格键
if (e.KeyChar == 8)
e.Handled = false;
if (txt.SelectionStart == 1 && e.KeyChar == '0')
e.Handled = true;
}
/// <summary> /// 18身份证检验 /// </summary> /// <param name="s"></param> /// <returns></returns> private bool checkId(string id) { string card17 = id.Substring(0,17);//前十七位字串 string card18 = id.Substring(17,1);//第十八位字串 int[] Wquan = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; string check = "10X98765432"; int result = 0; for(int i = 0; i < 17; ++i) result += (card17[i] - '0') * Wquan[i]; result %= 11; string id2 = check[result].ToString(); return card18.Equals(id2,StringComparison.CurrentCultureIgnoreCase); }
Stopwatch watch = new Stopwatch(); watch.Start(); watch.Stop(); MessageBox(string.Format("程序用了{0}",watch.ElapsedMilliseconds));
System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
// 其中的-号和:号可以用其他的字符互换,y代表年,M代表月,d代表天,H24小时制的小时,h12小时制的小时,m代表分,s代表秒,f代表毫秒部分(要显示毫秒,需要加参数见MSDN)