public static void CrossThreadInvoke(TextBoxEx t, MethodInvoker m) { if (t.InvokeRequired) { t.Invoke(m); } }
/// <summary> /// TextBoxEx中只能输入数字和小数点 /// </summary> /// <param name="txt"></param> public static void TextToDecimal(TextBoxEx txt) { //键盘按下事件KeyPress txt.KeyPress += delegate(object sender, KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 46) { e.Handled = true; } if (e.KeyChar == 46 && txt.Caption.Contains(".")) { e.Handled = true; } }; //textbox失去焦点事件leave txt.Leave += delegate(object sender, EventArgs e) { string Money = txt.Caption.Trim(); Regex regex = new Regex(@"^([1-9][0-9]*|0)(\.[0-9]+)?$"); if (Money == "") { txt.Caption = "0"; } if (!regex.Match(Money).Success) { txt.Caption = "0"; } else { if (Money.IndexOf(".") == 0) { txt.Caption = "0"; } if (txt.Caption != "0") { txt.Caption = Math.Round(Convert.ToDecimal(Money), 2).ToString("0.##"); } } }; }
/// <summary> /// 计算税额、价税合计 /// </summary> /// <param name="txtM">货款</param> /// <param name="txtR">税率</param> /// <param name="txtC">税额</param> /// <param name="txtS">价税合计</param> private void RateMoney(TextBoxEx txtM, TextBoxEx txtR, TextBoxEx txtC, TextBoxEx txtS) { try { string strHMoney = !string.IsNullOrEmpty(txtM.Caption.Trim()) ? txtM.Caption.Trim() : "0.00"; string strHRate = !string.IsNullOrEmpty(txtR.Caption.Trim()) ? txtR.Caption.Trim() : "0.00"; txtC.Caption = Math.Round((Convert.ToDecimal(strHMoney) * Convert.ToDecimal(strHRate) / 100), 2).ToString("0.00"); txtS.Caption = Math.Round((Convert.ToDecimal(strHMoney) + Convert.ToDecimal(txtC.Caption.Trim())), 2).ToString("0.00"); txtShouldSum.Caption = Math.Round(Convert.ToDecimal(!string.IsNullOrEmpty(txtHSum.Caption.Trim()) ? txtHSum.Caption.Trim() : "0.00") + Convert.ToDecimal(!string.IsNullOrEmpty(txtFSum.Caption.Trim()) ? txtFSum.Caption.Trim() : "0.00") + Convert.ToDecimal(!string.IsNullOrEmpty(txtOSum.Caption.Trim()) ? txtOSum.Caption.Trim() : "0.00") - Convert.ToDecimal(!string.IsNullOrEmpty(txtPrivilegeCost.Caption.Trim()) ? txtPrivilegeCost.Caption.Trim() : "0.00"), 2).ToString("0.00");//应收总额 strShouldSum = txtShouldSum.Caption = Math.Round(Convert.ToDecimal(!string.IsNullOrEmpty(txtHSum.Caption.Trim()) ? txtHSum.Caption.Trim() : "0.00") + Convert.ToDecimal(!string.IsNullOrEmpty(txtFSum.Caption.Trim()) ? txtFSum.Caption.Trim() : "0.00") + Convert.ToDecimal(!string.IsNullOrEmpty(txtOSum.Caption.Trim()) ? txtOSum.Caption.Trim() : "0.00") - Convert.ToDecimal(!string.IsNullOrEmpty(txtPrivilegeCost.Caption.Trim()) ? txtPrivilegeCost.Caption.Trim() : "0.00"), 2).ToString("0.00");//应收总额 //本次欠款金额 string Smoney = !string.IsNullOrEmpty(txtReceivedSum.Caption) ? txtReceivedSum.Caption : "0.00";//实收总额 string Ymoney = !string.IsNullOrEmpty(txtShouldSum.Caption) ? txtShouldSum.Caption : "0.00";//应收总额 if (Convert.ToDecimal(Smoney) > Convert.ToDecimal(Ymoney)) { MessageBoxEx.Show("实收总额不能大于应收总额!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None); txtReceivedSum.Caption = "0.00"; return; } //txtDebtCost.Caption = (Convert.ToDecimal(Ymoney) - Convert.ToDecimal(Smoney)).ToString(); } catch (Exception ex) { HXCPcClient.GlobalStaticObj.GlobalLogService.WriteLog(ex); } }
/// <summary> /// 计算税额、价税合计 /// </summary> /// <param name="txtM">货款</param> /// <param name="txtR">税率</param> /// <param name="txtC">税额</param> /// <param name="txtS">价税合计</param> private void RateMoney(TextBoxEx txtM, TextBoxEx txtR, TextBoxEx txtC, TextBoxEx txtS) { string strHMoney = !string.IsNullOrEmpty(txtM.Caption.Trim()) ? txtM.Caption.Trim() : "0"; string strHRate = !string.IsNullOrEmpty(txtR.Caption.Trim()) ? txtR.Caption.Trim() : "0"; txtC.Caption = (Convert.ToDecimal(strHMoney) * Convert.ToDecimal(strHRate) / 100).ToString(); txtS.Caption = (Convert.ToDecimal(strHMoney) + Convert.ToDecimal(txtC.Caption.Trim())).ToString(); txtShouldSum.Caption = Convert.ToString(Convert.ToDecimal(!string.IsNullOrEmpty(txtHSum.Caption.Trim()) ? txtHSum.Caption.Trim() : "0") + Convert.ToDecimal(!string.IsNullOrEmpty(txtFSum.Caption.Trim()) ? txtFSum.Caption.Trim() : "0") + Convert.ToDecimal(!string.IsNullOrEmpty(txtOSum.Caption.Trim()) ? txtOSum.Caption.Trim() : "0") - Convert.ToDecimal(!string.IsNullOrEmpty(txtPrivilegeCost.Caption.Trim()) ? txtPrivilegeCost.Caption.Trim() : "0"));//应收总额 strShouldSum = txtShouldSum.Caption = Convert.ToString(Convert.ToDecimal(!string.IsNullOrEmpty(txtHSum.Caption.Trim()) ? txtHSum.Caption.Trim() : "0") + Convert.ToDecimal(!string.IsNullOrEmpty(txtFSum.Caption.Trim()) ? txtFSum.Caption.Trim() : "0") + Convert.ToDecimal(!string.IsNullOrEmpty(txtOSum.Caption.Trim()) ? txtOSum.Caption.Trim() : "0") - Convert.ToDecimal(!string.IsNullOrEmpty(txtPrivilegeCost.Caption.Trim()) ? txtPrivilegeCost.Caption.Trim() : "0"));//应收总额 //本次欠款金额 string Smoney = !string.IsNullOrEmpty(txtReceivedSum.Caption) ? txtReceivedSum.Caption : "0";//实收总额 string Ymoney = !string.IsNullOrEmpty(txtShouldSum.Caption) ? txtShouldSum.Caption : "0";//应收总额 if (Convert.ToDecimal(Smoney) > Convert.ToDecimal(Ymoney)) { MessageBoxEx.Show("实收总额不能大于应收总额!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None); txtReceivedSum.Caption = string.Empty; return; } txtDebtCost.Caption = (Convert.ToDecimal(Ymoney) - Convert.ToDecimal(Smoney)).ToString(); }
/// <summary> /// TextBoxEx中只能输入数字和小数点 /// </summary> /// <param name="txt">TextBoxEx</param> /// <param name="isint">是否只能输入整数,isint=true可以输入小数否则仅能输入整数</param> /// <param name="isint">是否只能输入整数并且只能输入1-100之间的数字,islimt=true不做限制否则限制只能输入1-100的数字</param> public static void TextToDecimal(TextBoxEx txt,bool isint=true,bool islimt=true) { //键盘按下事件KeyPress txt.KeyPress += delegate(object sender, KeyPressEventArgs e) { if (isint) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 46) { e.Handled = true; } if (e.KeyChar == 46 && txt.Caption.Contains(".")) { e.Handled = true; } } else { if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (byte)(e.KeyChar) == 8)//8就是回格,backspace(删除). { } else { e.Handled = true; } } }; //textbox失去焦点事件leave txt.Leave += delegate(object sender, EventArgs e) { string Money = txt.Caption.Trim(); if (isint) { Regex regex = new Regex(@"^([1-9][0-9]*|0)(\.[0-9]+)?$"); if (Money == "") { txt.Caption = "0.00"; } if (!regex.Match(Money).Success) { txt.Caption = "0.00"; } else { if (Money.IndexOf(".") == 0) { txt.Caption = "0.00"; } if (txt.Caption != "0.00") { txt.Caption = Math.Round(Convert.ToDecimal(Money), 2).ToString("0.00"); } } } else { Regex regex = new Regex(@"^([1-9][0-9]*|0)(\.[0-9]+)?$"); if (Money == "") { txt.Caption = "0"; } if (!regex.Match(Money).Success) { txt.Caption = "0"; } else { txt.Caption = Math.Round(Convert.ToDecimal(Money), 0).ToString("0"); } if (!islimt) { if (Convert.ToDecimal(txt.Caption) > 100 || Convert.ToDecimal(txt.Caption) < 1) { txt.Caption = "1"; } } } }; }
/// <summary> /// 手机号输入限制-只能输入数字并且可以实现组合件功能如:Ctrl+A,Ctrl+C,Ctrl+V等 /// </summary> /// <param name="txt">文本框</param> public static void TextForPhone(TextBoxEx txt) { txt.KeyPress += delegate(object sender, KeyPressEventArgs e) { if (nonNumberEntered) e.Handled = true; }; txt.KeyDown += delegate(object sender, KeyEventArgs e) { nonNumberEntered = false; //组合键 if (e.KeyData == (Keys.C | Keys.Control) || e.KeyData == (Keys.A | Keys.Control) || e.KeyData == (Keys.V | Keys.Control) || e.KeyData == (Keys.X | Keys.Control)) return; //数字 if (!e.Shift && e.KeyValue >= '0' && e.KeyValue <= '9') return; if (e.KeyData == Keys.NumPad0 || e.KeyData == Keys.NumPad1 || e.KeyData == Keys.NumPad2 || e.KeyData == Keys.NumPad3 || e.KeyData == Keys.NumPad4 || e.KeyData == Keys.NumPad5 || e.KeyData == Keys.NumPad6 || e.KeyData == Keys.NumPad7 || e.KeyData == Keys.NumPad8 || e.KeyData == Keys.NumPad9) return; //操作键 if (e.KeyData == Keys.Back || e.KeyData == Keys.Delete || e.KeyData == Keys.Left || e.KeyData == Keys.Right) return; nonNumberEntered = true; }; }