CEL_Payroll/Payroll.Controls/CustomControls/NumControl.cs
2024-09-17 14:30:13 +06:00

199 lines
4.9 KiB
C#

using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace Payroll.Controls
{
#region Number Control
public class NumControl : System.Windows.Forms.UserControl
{
#region Declaration
private string m_sFormat;
private System.Windows.Forms.TextBox txtNumber;
private System.ComponentModel.Container components = null;
public event System.EventHandler ValueChanged;
public NumControl()
{
m_sFormat="0.00";
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Component Designer generated code
private void InitializeComponent()
{
this.txtNumber = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtNumber
//
this.txtNumber.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtNumber.Font = new System.Drawing.Font("Tahoma", 8.25F);
this.txtNumber.Location = new System.Drawing.Point(1, 1);
this.txtNumber.Name = "txtNumber";
this.txtNumber.Size = new System.Drawing.Size(144, 21);
this.txtNumber.TabIndex = 0;
this.txtNumber.Text = "0.00";
this.txtNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtNumber.TextChanged += new System.EventHandler(this.txtNumber_TextChanged);
this.txtNumber.Validated += new System.EventHandler(this.txtNumber_Validated);
this.txtNumber.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtNumber_KeyPress);
//
// NumControl
//
this.Controls.Add(this.txtNumber);
this.Name = "NumControl";
this.Size = new System.Drawing.Size(147, 23);
this.Resize += new System.EventHandler(this.ctlNumber_Resize);
this.ForeColorChanged += new System.EventHandler(this.EaseNum_ForeColorChanged);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
#region Conreol Related fucntion
[Description("Set border style of the control"), Category("Behavior")]
public BorderStyle BorderStyle
{
get{return txtNumber.BorderStyle;}
set{txtNumber.BorderStyle=value;}
}
[Description("Sets the numeric value format"), Category("Behavior")]
public string Format
{
get{return m_sFormat;}
set
{
m_sFormat=value;
txtNumber.Text=m_sFormat;
}
}
[Description("Sets the numeric type value"), Category("Behavior")]
public double Value
{
get{return double.Parse(txtNumber.Text);}
set{txtNumber.Text= value.ToString(m_sFormat);}
}
public override string ToString()
{
return txtNumber.Text;
}
private void ctlNumber_Resize(object sender, System.EventArgs e)
{
if (this.Width<=10){this.Width=10;}
this.Height=20;
txtNumber.Top=0;
txtNumber.Left=0;
txtNumber.Width=this.Width;
txtNumber.Height=this.Height;
}
private void txtNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
int i=0, nCount=0; string sVal="";
switch (e.KeyChar)
{
case '\b':
e.Handled=false;
break;
case '.':
while (i < txtNumber.Text.Length)
{
sVal=txtNumber.Text.Substring(i,1);
if (sVal=="."){nCount=nCount+1;}
i=i+1;
}
e.Handled=(nCount!=0);
break;
case '-':
if (txtNumber.SelectionStart>1)
{e.Handled=true;}
else
{
while (i < txtNumber.Text.Length)
{
sVal=txtNumber.Text.Substring(i,1);
if (sVal=="-"){nCount=nCount+1;}
i=i+1;
}
e.Handled=(nCount!=0);
}
break;
default:
if (Char.IsDigit(e.KeyChar)==false)
{e.Handled=true;}
break;
}
}
private void txtNumber_Validated(object sender, System.EventArgs e)
{
if(txtNumber.Text.Trim().Length<=0){txtNumber.Text="0.00";}
string sValue=txtNumber.Text;
try
{
double dbl=double.Parse(sValue);
sValue=dbl.ToString(m_sFormat);
txtNumber.Text=sValue;
}
catch(Exception exp)
{
MessageBox.Show(exp.Message,"Error");
txtNumber.Text="0.00";
this.Focus();
}
}
private void txtNumber_TextChanged(object sender, System.EventArgs e)
{
try
{
ValueChanged(sender, e);
return;
}
catch
{
}
}
private void EaseNum_ForeColorChanged(object sender, System.EventArgs e)
{
txtNumber.ForeColor=this.ForeColor;
}
#endregion
#region Lately Added Functions & Properties
private int _nMaxLength=32767;
public int MaxLength
{
get{return _nMaxLength;}
set{_nMaxLength=value;}
}
public void SetSelection(int nStart, int nLength)
{
txtNumber.SelectionStart=nStart;
txtNumber.SelectionLength=nLength;
}
#endregion
}
#endregion
}