J
John S
I have created an event handler that is trying to format each number as it
is typed. Example
If the first char is 1 = output is $1.00
second char is 3 = output is $13.00
next is 5 = $135.00
next is 7 = $1,3576.00
Here is my code but I cannot get it to work.
public static void ReturnFormattedNumber(object sender,
System.Windows.Forms.KeyPressEventArgs e){
//KeyPress event
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionStart;
tb.Text = tb.Text.Substring(tb.SelectionStart + tb.SelectionLength) +
Char.ToUpper(e.KeyChar) +
tb.Text.Substring(0, charPosition);
tb.SelectionLength = 0;
tb.SelectionStart = charPosition;
bool tf =IsNumeric(tb.Text,true);
if(tf==true){
tb.Text=String.Format("{0:C}",tb.Text);
Debug.WriteLine(String.Format("{0:C}",tb.Text));
}
e.Handled=true;
}
is typed. Example
If the first char is 1 = output is $1.00
second char is 3 = output is $13.00
next is 5 = $135.00
next is 7 = $1,3576.00
Here is my code but I cannot get it to work.
public static void ReturnFormattedNumber(object sender,
System.Windows.Forms.KeyPressEventArgs e){
//KeyPress event
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionStart;
tb.Text = tb.Text.Substring(tb.SelectionStart + tb.SelectionLength) +
Char.ToUpper(e.KeyChar) +
tb.Text.Substring(0, charPosition);
tb.SelectionLength = 0;
tb.SelectionStart = charPosition;
bool tf =IsNumeric(tb.Text,true);
if(tf==true){
tb.Text=String.Format("{0:C}",tb.Text);
Debug.WriteLine(String.Format("{0:C}",tb.Text));
}
e.Handled=true;
}