Shovan Saha

Shovan Saha

  • NA
  • 321
  • 86.9k

Please check my code

Aug 18 2017 5:56 PM
C# visual studio 2015, Windows Form. I have a text box and three labels. I want to write in text box only number and decimal place two. But I can not write in the text box as 0.52 or 052. If I write with . or 0 the program does not continue. I need, I can start write in textbox with 0.## if first 0 is problem then message box will show but the program will continue. Please help me at this regard.  
 
private void txtAPenPension_TextChanged(object sender, EventArgs e)
{
if (Regex.IsMatch(txtAPenPension.Text, " ^ [0-9]"))
{
txtAPenPension.Text = "";
}
double tk1, tkS1, tkD1;
tk1 = double.Parse(txtAPenPension.Text);
tkS1 = tk1 * 0.01;
tkD1 = tk1 + tkS1;
if (txtAPenPension.Text != "")
{
lblAPenPorishodh.Text = string.Format("{0:F2}", tk1);
lblAPenServiceCharge.Text = string.Format("{0:F2}", tkS1);
lblAPenDabi.Text = string.Format("{0:F2}", tkD1);
return;
}
}
private void txtAPenPension_KeyPress(object sender, KeyPressEventArgs e)
{
string senderText = (sender as TextBox).Text;
string senderName = (sender as TextBox).Name;
string[] splitByDecimal = senderText.Split('.');
int cursorPosition = (sender as TextBox).SelectionStart;
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& (e.KeyChar != '.'))
{
e.Handled = true;
}
if (e.KeyChar == '.'
&& senderText.IndexOf('.') > -1)
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar)
&& senderText.IndexOf('.') < cursorPosition
&& splitByDecimal.Length > 1
&& splitByDecimal[1].Length == 2)
{
e.Handled = true;
}
}

Answers (1)