You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
9.4 KiB
C#

using JwKdsV.Core;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JwKdsV.Component
{
public class NumericTextBox : DevComponents.DotNetBar.Controls.TextBoxX
{
private const int WM_RBUTTONDOWN = 0x0204;
bool allowSpace = false;
bool allowNegativeSign = false;
bool allowDecimalSeparator = true;
int maxDecimalDigit = 2;
private bool m_EnableSoftKeyboard = true;
public delegate void EventHandler(object sender, EnterEventArg e);
public event EventHandler OnEnterClick;
public event EventHandler OnValueChanged;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.Height = 40;
this.Multiline = false;
this.TextAlign = HorizontalAlignment.Left;
this.Border.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
}
protected override void WndProc(ref Message m)
{
//if (m.Msg == 0x0302) //0x0302是粘贴消息
//{
// m.Result = IntPtr.Zero; //拦截此消息
// return;
//}
if (m.Msg == WM_RBUTTONDOWN)
return;//WM_RBUTTONDOWN是为了不让出现鼠标菜单
base.WndProc(ref m);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (OnEnterClick != null)
{
OnEnterClick(this, new EnterEventArg(this.Text.Trim()));
}
}
base.OnKeyDown(e);
}
protected override void OnTextChanged(EventArgs e)
{
String Text = this.Text.ToString();
//Double Num;
int offset = Text.IndexOf(".");
bool isDot = (!string.IsNullOrEmpty(Text) && offset >= 0);
if (isDot && Text.Substring(offset).Length >= (maxDecimalDigit + 1))
{
this.Text = Text.Substring(0, offset + (maxDecimalDigit + 1));
}
//if ((Text.Length > 0) && (Text.Substring(Text.Length - 1, 1) != "."))
//{
// this.Text = Text;
// if (Double.TryParse(Text, out Num))
// {
// Text = String.Format("{0:#,##0.###}", Num);
// this.Text = Text;
// }
//}
this.SelectionStart = Text.Length;
if (OnValueChanged != null)
{
OnValueChanged(this, new EnterEventArg(this.Text.Trim()));
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.GetCultureInfo("zh-CN").NumberFormat;
//小数点
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
//负值符号
string negativeSign = numberFormatInfo.NegativeSign;
string keyInput = e.KeyChar.ToString();
//是否已经输入小数点
if ((e.KeyChar == '.') && Text.IndexOf('.') > -1)
{
e.Handled = true;
}
//负号必须出现在第一位置
if (Text.Length == 0)
{
this.SelectionStart = this.Text.Length;
if ((e.KeyChar == '-') && (Text.LastIndexOf('-') > -1))
{
e.Handled = true;
}
}
else
{
if (((e.KeyChar == '-') && (Text.IndexOf('-') > -1)) || ((e.KeyChar == '-') && (this.SelectionStart != 0)))
{
e.Handled = true;
}
}
//输入必须为数字
if (Char.IsDigit(e.KeyChar))
{
// Digits are OK
}
else if (this.allowDecimalSeparator && keyInput.Equals(decimalSeparator))
{
// Decimal separator is OK
if (Text.Length == 0)
{
this.Text = "0";
this.SelectionStart = this.Text.Length;
}
}
else if (this.allowNegativeSign && keyInput.Equals(negativeSign))
{
// Negative Sign is OK
}
else if (e.KeyChar == '\b')
{
// Backspace key is OK
}
else if (this.allowSpace && e.KeyChar == ' ')
{
}
else if ((e.KeyChar == (char)Keys.Enter))
{
}
else
{
e.Handled = true;
}
}
public int MaxDecimalDigit
{
set
{
this.maxDecimalDigit = value;
}
get
{
return this.maxDecimalDigit;
}
}
public long LongValue
{
get
{
long result;
long.TryParse(this.Text, out result);
return result;
}
}
public int IntegerValue
{
get
{
int result;
int.TryParse(this.Text, out result);
return result;
}
}
public decimal DecimalValue
{
get
{
decimal result;
decimal.TryParse(this.Text, out result);
return result;
}
}
public bool AllowSpace
{
set
{
this.allowSpace = value;
}
get
{
return this.allowSpace;
}
}
public bool AllowNegativeSign
{
get
{
return allowNegativeSign;
}
set
{
allowNegativeSign = value;
}
}
public bool AllowDecimalSeparator
{
get
{
return allowDecimalSeparator;
}
set
{
allowDecimalSeparator = value;
}
}
public double DoubleValue
{
get
{
if (Text == "")
return 0;
double result;
double.TryParse(Text, out result);
return result;
}
}
private KeyboardType _keyboardType = KeyboardType.;
public KeyboardType Keyboard
{
get
{
return this._keyboardType;
}
private set
{
this._keyboardType = value;
}
}
/// <summary>
/// 不启用虚假键盘
/// </summary>
public bool EnableSoftKeyboard { get => m_EnableSoftKeyboard; set => m_EnableSoftKeyboard = value; }
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
//触摸屏模式下自动关闭虚拟键盘
IntPtr hwnd = WindowsAPI.FindWindow("IPTip_Main_Window", null);
if (hwnd != IntPtr.Zero)
{
WindowsAPI.SendMessage(hwnd, WindowsAPI.WM_SYSCOMMAND, WindowsAPI.SC_CLOSE, 0);
}
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if (!Global.Instance.GlobalConfigBoolValue(ConfigConstant.CONFIG_CASHIER_ENABLESOFTKEYBOARD, true) && this.EnableSoftKeyboard)
{
try
{
KeyboardType keyboardType = KeyboardType.;
var keyboard = Application.OpenForms["VirtualKeyboard"];
if (keyboard == null)
{
keyboard = new VirtualKeyboard(keyboardType);
((VirtualKeyboard)keyboard).ShowVirtualKeyboard(this, keyboardType);
keyboard.Location = new System.Drawing.Point((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2) - (keyboard.Width / 2), System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - keyboard.Height);
}
else if (keyboard.Visible != true)
{
((VirtualKeyboard)keyboard).ShowVirtualKeyboard(this, keyboardType);
keyboard.Location = new System.Drawing.Point((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2) - (keyboard.Width / 2), System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - keyboard.Height);
}
}
catch (Exception ex)
{
}
}
}
}
/// <summary>
/// 回车事件携带参数输出
/// </summary>
public class EnterEventArg : System.EventArgs
{
private readonly string _value;
public EnterEventArg(string value)
{
this._value = value;
}
/// <summary>
/// 回车事件携带的参数
/// </summary>
public string Value
{
get
{
return this._value;
}
}
}
}