// *********************************************************************** // Assembly : HZH_Controls // Created : 08-08-2019 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace HZH_Controls.Controls { /// /// Class UCNumTextBox. /// Implements the /// /// [DefaultEvent("NumChanged")] public partial class UCNumTextBox : UserControl { /// /// Occurs when [show key border event]. /// [Description("弹出输入键盘时发生"), Category("自定义")] public event EventHandler ShowKeyBorderEvent; /// /// Occurs when [hide key border event]. /// [Description("关闭输入键盘时发生"), Category("自定义")] public event EventHandler HideKeyBorderEvent; /// /// Occurs when [number changed]. /// [Description("数字改变时发生"), Category("自定义")] public event EventHandler NumChanged; /// /// 输入类型 /// /// The type of the input. [Description("输入类型"), Category("自定义")] public TextInputType InputType { get { return txtNum.InputType; } set { if (value == TextInputType.NotControl) { return; } txtNum.InputType = value; } } /// /// Gets or sets a value indicating whether this instance is number can input. /// /// true if this instance is number can input; otherwise, false. [Description("数字是否可手动编辑"), Category("自定义")] public bool IsNumCanInput { get { return txtNum.Enabled; } set { txtNum.Enabled = value; } } /// /// 当InputType为数字类型时,能输入的最大值 /// /// The maximum value. [Description("当InputType为数字类型时,能输入的最大值。")] public decimal MaxValue { get { return this.txtNum.MaxValue; } set { this.txtNum.MaxValue = value; } } /// /// 当InputType为数字类型时,能输入的最小值 /// /// The minimum value. [Description("当InputType为数字类型时,能输入的最小值。")] public decimal MinValue { get { return this.txtNum.MinValue; } set { this.txtNum.MinValue = value; } } /// /// The key board type /// private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderNum; /// /// Gets or sets the type of the key board. /// /// The type of the key board. [Description("键盘样式"), Category("自定义")] public KeyBoardType KeyBoardType { get { return keyBoardType; } set { keyBoardType = value; } } /// /// Gets or sets the number. /// /// The number. [Description("数值"), Category("自定义")] public decimal Num { get { return txtNum.Text.ToDecimal(); } set { txtNum.Text = value.ToString(); } } /// /// 获取或设置控件显示的文字的字体。 /// /// The font. /// /// /// /// /// /// [Description("字体"), Category("自定义")] public new Font Font { get { return txtNum.Font; } set { txtNum.Font = value; } } /// /// Occurs when [add click]. /// [Description("增加按钮点击事件"), Category("自定义")] public event EventHandler AddClick; /// /// Occurs when [minus click]. /// [Description("减少按钮点击事件"), Category("自定义")] public event EventHandler MinusClick; private decimal increment = 1; [Description("递增量,大于0的数值"), Category("自定义")] public decimal Increment { get { return increment; } set { if (value <= 0) return; increment = value; } } /// /// Initializes a new instance of the class. /// public UCNumTextBox() { InitializeComponent(); txtNum.TextChanged += txtNum_TextChanged; } /// /// Handles the TextChanged event of the txtNum control. /// /// The source of the event. /// The instance containing the event data. void txtNum_TextChanged(object sender, EventArgs e) { if (NumChanged != null) { NumChanged(txtNum.Text.ToString(), e); } } /// /// The m FRM anchor /// Forms.FrmAnchor m_frmAnchor; /// /// Handles the MouseDown event of the txtNum control. /// /// The source of the event. /// The instance containing the event data. private void txtNum_MouseDown(object sender, MouseEventArgs e) { if (IsNumCanInput) { if (KeyBoardType != HZH_Controls.Controls.KeyBoardType.Null) { switch (keyBoardType) { case KeyBoardType.UCKeyBorderAll_EN: UCKeyBorderAll keyAll = new UCKeyBorderAll(); keyAll.RetractClike += (a, b) => { m_frmAnchor.Hide(); }; keyAll.EnterClick += (a, b) => { m_frmAnchor.Hide(); }; m_frmAnchor = new Forms.FrmAnchor(this, keyAll); m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; m_frmAnchor.Show(this.FindForm()); break; case KeyBoardType.UCKeyBorderNum: UCKeyBorderNum keyNum = new UCKeyBorderNum(); keyNum.EnterClick += (a, b) => { m_frmAnchor.Hide(); }; m_frmAnchor = new Forms.FrmAnchor(this, keyNum); m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; m_frmAnchor.Show(this.FindForm()); break; } } } } /// /// Handles the VisibleChanged event of the m_frmAnchor control. /// /// The source of the event. /// The instance containing the event data. void m_frmAnchor_VisibleChanged(object sender, EventArgs e) { if (m_frmAnchor.Visible) { if (ShowKeyBorderEvent != null) { ShowKeyBorderEvent(this, null); } } else { if (HideKeyBorderEvent != null) { HideKeyBorderEvent(this, null); } } } /// /// Numbers the add click. /// public void NumAddClick() { btnAdd_MouseDown(null, null); } /// /// Numbers the minus click. /// public void NumMinusClick() { btnMinus_MouseDown(null, null); } /// /// Handles the MouseDown event of the btnAdd control. /// /// The source of the event. /// The instance containing the event data. private void btnAdd_MouseDown(object sender, MouseEventArgs e) { if (AddClick != null) { AddClick(this, e); } decimal dec = this.txtNum.Text.ToDecimal(); dec += increment; txtNum.Text = dec.ToString(); } /// /// Handles the MouseDown event of the btnMinus control. /// /// The source of the event. /// The instance containing the event data. private void btnMinus_MouseDown(object sender, MouseEventArgs e) { if (MinusClick != null) { MinusClick(this, e); } decimal dec = this.txtNum.Text.ToDecimal(); dec -= increment; ; txtNum.Text = dec.ToString(); } /// /// Handles the Load event of the UCNumTextBox control. /// /// The source of the event. /// The instance containing the event data. private void UCNumTextBox_Load(object sender, EventArgs e) { this.txtNum.BackColor = this.BackColor; } /// /// Handles the FontChanged event of the txtNum control. /// /// The source of the event. /// The instance containing the event data. private void txtNum_FontChanged(object sender, EventArgs e) { txtNum.Location = new Point(txtNum.Location.X, (this.Height - txtNum.Height) / 2); } /// /// Handles the BackColorChanged event of the UCNumTextBox control. /// /// The source of the event. /// The instance containing the event data. private void UCNumTextBox_BackColorChanged(object sender, EventArgs e) { Color c = this.BackColor; Control control = this; while (c == Color.Transparent) { control = control.Parent; if (control == null) break; c = control.BackColor; } if (c == Color.Transparent) return; txtNum.BackColor = c; } } }