Reputation: 11
Here is the situation I need addressed:
I have a masked text box that has a mask of 00.0
, I need to be able to enter values in this box with a range from 2 to 20. The issue is, that I need to enter the values from the right and transfer over the decimal point instead of entering from the right.
For example, at the moment I can type in 160 and it will give me the correct 16.0
value... However if I enter 80
, I get 80
. instead of 8.0
. I do not want the users to have to enter leading zeros to get the value (080
). Is there a way to enter text from the right and have it cross over the decimal point. This seems like it should be easy, but not having luck so far.
Upvotes: 1
Views: 2499
Reputation: 1
Try this code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var _value = textBox1.Text.Trim();
if ((textBox1.Text.Length <= 2)) {
_value = textBox1.Text.PadLeft(3, "0", c);
}
var _changed = _value.Replace(c, "");
string _split1 = _changed.Substring(0, (_changed.Length - 2));
string _split2 = _changed.Substring(_split1.Length, 2);
_value = Convert.ToDecimal(string.Format("{0}.{1}", _split1, _split2)).ToString();
textBox1.Text = _value;
}
Upvotes: 0
Reputation: 3187
I don't think there is a native way to do that. Your best bet would be to implement a event handler on the TextChanged (or else) action of your textbox and do some modification here.
Upvotes: 0