Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
On the text box, it should allow the user to enter only 6 decimal places. Ex: 1.012345 or 1,012345
If 7 decimal places are tried, the entry should not be allowed.
Please refer the below code
C#
private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
       {
           /// <summary>


           bool numericKeys = (
               (((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
               (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) && e.Modifiers != Keys.Shift)
               || e.KeyCode == Keys.OemMinus
               || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal || e.KeyCode == Keys.Oemcomma);

           bool navigationKeys = (
               e.KeyCode == Keys.Up ||
               e.KeyCode == Keys.Right ||
               e.KeyCode == Keys.Down ||
               e.KeyCode == Keys.Left ||
               e.KeyCode == Keys.Home ||
               e.KeyCode == Keys.End ||
               e.KeyCode == Keys.Tab);

           bool editKeys = (e.KeyCode == Keys.Delete ||
               e.KeyCode == Keys.Back);


           TextBox aTextbox = (TextBox)sender;
           aTextbox.Text = "2,33333";

           if (!string.IsNullOrEmpty(aTextbox.Text))
           {
               double maxIntensity;

               string aTextValue = aTextbox.Text;
               //convert it into the culture invariant type

               bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity);

               if (value)
               {
                   string aText = maxIntensity.ToString();

                   MessageBox.Show("the value is {0}", aText);

                   string[] args = aText.Split('.');

                   if (numericKeys)
                   {
                       bool handleText = true;

                       if (args.Length > 2)
                       {
                           handleText = false;
                       }
                       else
                       {
                           if (args.Length == 2 && args[1] != null && args[1].Length > 5)
                           {
                               handleText = false;
                           }
                       }

                       if (!handleText)
                       {
                           e.SuppressKeyPress = true;
                           e.Handled = true;
                       }

                   }
               }
           }


           if (!(numericKeys || editKeys || navigationKeys))
           {
               e.SuppressKeyPress = true;
               e.Handled = true;
           }
       }
   }


Splitting the strings to seperate the real part and mantissa part (both are stored are strings), then I check the length of the mantissa part. If the length of the string is more than 5, I'd like to suppress the key.

Is there any other approach to do this?
Posted
Updated 13-Apr-11 0:51am
v2
Comments
Pong D. Panda 13-Apr-11 7:02am    
Does your code run? If not, what's the error?
Raghav55 13-Apr-11 7:04am    
it works when the values is entered 23.333333
but when the values is entered as 23,333333
the entry is not restricted to 6 decimals places , user can enter more than 6 decimal places
Pong D. Panda 13-Apr-11 7:09am    
Then that,s not an error, since the character ',' is considered as digit separator and not a decimal place separator.
Prerak Patel 13-Apr-11 7:26am    
but in europian countries, ',' is decimal separator and '.' is digit separator. This may be the case.
Pong D. Panda 13-Apr-11 7:30am    
Alright, but it seems that Prerak Patel already solved the mystery. Mark it as solved if it does.

> Is there any other approach to do this?
Yes. Use a MaskedTextBox or a NumericUpDown.
Both of them can be easily set up to match the expected behaviour.
Just use the properties Mask, respectively Decimals, i believe.
 
Share this answer
 
Comments
Raghav55 13-Apr-11 7:00am    
i have to use only normal text box
Toli Cuturicu 13-Apr-11 7:19am    
No! You don't! A developer can use everything he wants.
But, if this is a homework, then we have a problem!
The question has to be tagged "homework", downvoted and / or deleted.
So, make up your mind.
Sergey Alexandrovich Kryukov 13-Apr-11 7:42am    
You're absolutely right!
Some developers makes it practically offensive: answer my question but not say A and B.
This is the case. OP's relationships with the "boss" is not our concern.

Also, please read this:
http://www.codeproject.com/Messages/3854264/Re-QA.aspx

Chris makes a sensible point, don't he?

--SA
Sergey Alexandrovich Kryukov 13-Apr-11 7:39am    
If you want to tell you know what to use, don't ask Questions!
--SA
Sergey Alexandrovich Kryukov 13-Apr-11 7:38am    
Good point. My 5.
--SA
You can use RegEx as follows and modify it as per your need. You can easily convert it to C#[^]
VB
If Not isValid(TextBox1.Text) Then
  'block action here
End If

Function isValid(ByVal value As String) As Boolean
  Dim rex As New Regex("^\d+((\.|,)\d{0,6})?$")
  Return rex.Match(value).Success
End Function


[Edit]
C#
if (!isValid(TextBox1.Text)) {
        //block action here
}

public bool isValid(string value)
{
    Regex rex = new Regex("^\d+((\.|,)\d{0,6})?$");
    return rex.Match(value).Success;
}
 
Share this answer
 
v5
Comments
Toli Cuturicu 13-Apr-11 7:19am    
What language is that?!
Pong D. Panda 13-Apr-11 7:23am    
VB using regular expression
Toli Cuturicu 13-Apr-11 7:53am    
Visual Basic?? The question is tagged c#!
Pong D. Panda 13-Apr-11 9:21am    
You cant blame me. Im just answering his question based on his code above.
Prerak Patel 13-Apr-11 7:24am    
It is call regular expressions. Google it to know more.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900