In textbox after this character "." I want the user to put in only 2 characters something like this
100.00
. How can i do that ?

- 97,193
- 102
- 206
- 364

- 53
- 1
- 2
- 11
-
8ASP.NET, Winforms or WPF? In any case, a masked textbox probably is the best solution. – Daniel Hilgarth Jan 14 '13 at 09:44
-
1-1 Agree with Daniel, it's important to know which technology you're using. On the other side knowing what have you tried is usefulness to us to answer your question. – Ignacio Soler Garcia Jan 14 '13 at 09:49
-
You are asking a question similar to [[Textbox display formatting](http://stackoverflow.com/questions/7671148/textbox-display-formatting)] – Ken Kin Jan 14 '13 at 09:55
5 Answers
Implement the OnTextChanged event to limit and modify content
private void textBox1_TextChanged(object sender, EventArgs e)
{
int i = textBox1.Text.IndexOf(".");
if ((i != -1) && (i == textBox1.Text.Length - 4))
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
textBox1.SelectionStart = textBox1.Text.Length;
}
}

- 36,908
- 70
- 97
- 130
-
A summary would make this a much better answer, as it stands its usefulness relies wholly on the linked site, content of which we have no control over. – Jan 14 '13 at 10:07
-
-
A summarry of using it to solve the problem, why its useful, and perhaps an example implementation. The more information thats given in a clear context the better the answer and the more useful it will potentially be to future people facing this issue under slightly different terms – Jan 14 '13 at 13:24
-
As the solution is allready provided i will post it, but in general i'm against providing solutions, i prefer providing the idea, it helps to learn coding much more than a copy paste solutions. – CloudyMarble Jan 14 '13 at 13:28
-
Some people, myself included, learn better visually, for example once i see a solution it is much easier to understand and to manipulate into a range of different solutions for future use and to grasp a new concept – Jan 14 '13 at 13:46
if your goal is only numbers use:
winforms http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.aspx
wpf Good NumericUpDown equivalent in WPF?
asp.net http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/NumericUpDown/NumericUpDown.aspx
you can check the length of the textbox.text after the point, to do it. find the index of '.' and then if textbox.text.length is bigger than that index + 3 delete the last letter.
int indexofDot=textbox.Text.indexOf('.');
if(textbox.text.Length>indexofDot+3) {... }
to delete the last letter just copy the string to onther temp string and delete the last char and then return it to textbox.Text

- 35
- 1
- 5
You can use it's TextChanged
event to validate the input:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
double d;
if (!double.TryParse(txt.Text, out d))
{
MessageBox.Show("Please enter a valid number");
return;
}
string num = d.ToString();
string decSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
int numDecPlaces = 0;
int decSepIndex = num.LastIndexOf(decSeparator);
if (decSepIndex != -1)
numDecPlaces = num.Substring(decSepIndex).Length;
if (numDecPlaces > 2)
{
MessageBox.Show("Please enter two decimal places at a maximum");
return;
}
}

- 450,073
- 74
- 686
- 939
First you need to determine whether you functionally want to:
- Prevent i.e. by using a mask, or
- Validate i.e. by using a regular expression
The implementations vary based on the chosen technology.
When you choose to prevent, you can look for a MaskedTextBox control. It is provided in WinForms and can be found on the web for WPF.
When you choose to validate, use the best practises for either Windows Forms of WPF.

- 5,761
- 33
- 47