When I always have doubts here I find solutions that help me to accomplish what I want....
For this reason today I come to contribute.
I don’t know if it’s the best way, but here’s how I use it.
I have a Static class called uUseful, in which I have three public methods.
Keypress method (Class cUseful)
public static void DecimalKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (sender.GetType().Name != "TextBox") return;
System.Windows.Forms.TextBox objTextBox = (sender as System.Windows.Forms.TextBox);
if (e.KeyChar == '.')
{
if (objTextBox.Text.IndexOf(",") >= 0)
{
e.Handled = true;
return;
}
if (objTextBox.Text.Trim().Length < 1)
{
e.Handled = true;
objTextBox.Text = "0,";
objTextBox.SelectionStart = objTextBox.Text.Length;
return;
}
else
{
e.KeyChar = ',';
}
}
}
Keydown method (Class cUseful)
public static void DecimalKeyDown(object sender, System.Windows.Forms.KeyEventArgs e, int pNumberDecimalPlaces = 2)
{
if (sender.GetType().Name != "TextBox") return;
System.Windows.Forms.TextBox objTextBox = (sender as System.Windows.Forms.TextBox);
if (objTextBox.SelectionLength == objTextBox.Text.Length || objTextBox.Text == ("0,").ToString().PadRight(pNumberDecimalPlaces, '0')) objTextBox.Text = "";
if (e.KeyCode < System.Windows.Forms.Keys.D0 || e.KeyCode > System.Windows.Forms.Keys.D9)
{
if (e.KeyCode < System.Windows.Forms.Keys.NumPad0 || e.KeyCode > System.Windows.Forms.Keys.NumPad9)
{
if (e.KeyCode != System.Windows.Forms.Keys.Back)
{
if (e.KeyCode == System.Windows.Forms.Keys.Oemcomma || e.KeyCode == System.Windows.Forms.Keys.Decimal || e.KeyCode == System.Windows.Forms.Keys.OemPeriod)
{
if (objTextBox.Text.IndexOf(",") >= 0) // só permite uma virgula
{
e.SuppressKeyPress = true;
return;
}
if (objTextBox.Text.Trim().Length < 1) // caso coloque uma virgula sem colocar valor
{
objTextBox.Text = "0,";
e.SuppressKeyPress = true;
objTextBox.SelectionStart = objTextBox.Text.Length;
return;
}
}
else
{
e.SuppressKeyPress = true;
return;
}
}
else
{
if (objTextBox.Text == "0,")
{
objTextBox.Text = "";
e.SuppressKeyPress = true;
return;
}
}
}
else
{
if (objTextBox.Text.IndexOf(",") >= 0)
{
if (((System.Convert.ToInt32(objTextBox.Text.IndexOf(",") + 1) - objTextBox.Text.Length) * -1) == pNumberDecimalPlaces)
{
e.SuppressKeyPress = true;
return;
}
}
}
}
}
Leave method (Class cUseful)
public static void DecimalLeave(object sender, System.EventArgs e, int pNumberDecimalPlaces = 2)
{
if (sender.GetType().Name != "TextBox") return;
System.Windows.Forms.TextBox objTextBox = (sender as System.Windows.Forms.TextBox);
if (!string.IsNullOrEmpty(objTextBox.Text))
{
if (objTextBox.Text.Trim().Length != 0) objTextBox.Text = System.Convert.ToDecimal(objTextBox.Text.Replace(".", ",")).ToString("n2");
else objTextBox.Text = ("0,").ToString().PadRight(pNumberDecimalPlaces, '0');
}
else
{
objTextBox.Text = ("0,").ToString().PadRight(pNumberDecimalPlaces, '0');
}
}
Already in the interface I have three methods of directing
Keypress Method (Form)
private void DecimalKeyPress(object sender, KeyPressEventArgs e)
{
BackEnd.cUseful.DecimalKeyPress(sender, e);
}
Keydown Method (Form)
private void DecimalKeyDown(object sender, KeyEventArgs e)
{
BackEnd.cUseful.DecimalKeyDown(sender, e);
}
Leave Method (Form)
private void DecimalLeave(object sender, System.EventArgs e)
{
BackEnd.cUseful.DecimalLeave(sender, e);
}
Finally, I start the Text property of Textbox objects with the value "0.00" and go to the Keypress, Keydown and Leave methods and select the private methods created on (Form).
Att.
Software systems in Latin America
Robson Knevitz
How do you feel about using the event
OnEnter
from the textbox to remove the "R$". And the eventOnLeave
to put back?– Latrova