62

I have to detect decimal separator in current windows setting. Im using visual studio 2010, windows form. In particular, if DecimalSeparator is comma, if user input dot in textbox1, I need show zero in textbox2.

I tryed with this code, but not works:

private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
    {
        string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
        if (uiSep.Equals(","))
        {
            while (e.KeyChar == (char)46)
            {
                tbxConvertito.Text = "0";
            }
        } 
    }

I have tryed also this code, but not work:

private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
    {
        string uiSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
        if (uiSep.Equals(","))
        {
            if (e.KeyChar == (char)46)
            {
                tbxConvertito.Text = "0";
            }
        } 
    }
Vincenzo Lo Palo
  • 1,341
  • 5
  • 19
  • 32
  • 2
    "but not works" doesn't tell us what went wrong. – Sam Axe Jan 25 '13 at 00:41
  • 2
    Since we can't read your mind or see your screen from here, it would be really useful if you included an explanation of what "not works" means. – Ken White Jan 25 '13 at 00:43
  • Sorry Ken, I mean that with this code, if I put dot in textbox1, textbox2 not change content (not show zero). Sorry my bad english – Vincenzo Lo Palo Jan 25 '13 at 00:46
  • vincenzolopalo your code makes no sense if you put a `"."` in the text box this should be allowed since is a normal decimal separator are you saying that if they enter a comma that you don't want that..? if so that's incorrect too.. what if someone enters the number `1,000,001.50` ..? please try to show and or explain your example of what you are looking for better.. – MethodMan Jan 25 '13 at 00:50
  • I mean.... If I put dot where decimalSeparator is comma – Vincenzo Lo Palo Jan 25 '13 at 00:54
  • 2
    Vincenzolopalo, for starters you do not need a while loop second you need to explain what you want better.. are you saying that you do not want to allow "," also when checking `==` vs `.Equals` you should know when to use one over the other normally when comparing Objects the `.Equals` would work.. either way.. your current logic is flawed why don't you use a Masked Edit instead..? – MethodMan Jan 25 '13 at 00:54
  • example: in my computer separator decimal is comma. In my application, if I put 1500.00 in textbox, this number has no sense. Than, I need prevent this scenario – Vincenzo Lo Palo Jan 25 '13 at 00:57
  • 2
    to solve your issue just use a [MaskedTextBox](http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx) `<==click here` to learn more about it – MethodMan Jan 25 '13 at 00:57
  • 2
    the number makes sense it's telling me 1500.00 are you trying to represent Currency..? you can get around that issue as well – MethodMan Jan 25 '13 at 00:58

3 Answers3

87

Solution:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char a = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
    if (e.KeyChar == a)
    {
        e.Handled = true;
        textBox1.Text = "0";
    }
}

That way, when you hit . or , you will have a 0 in your TextBox.

EDIT:

If you want to insert a 0 everytime you hit the decimal separator, this is the code:

char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (e.KeyChar == a)
{
    e.KeyChar = '0';
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Andres
  • 2,729
  • 5
  • 29
  • 60
  • If you need a textbox where the user can input only numbers I have the code.. MaskedTextboxes are a solution too, but I don´t like them. – Andres Jan 25 '13 at 01:23
  • thanks Andres, this is really solution that I need!! :) But work perfectly with this: CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator – Vincenzo Lo Palo Jan 25 '13 at 10:04
  • Sure! Glad to help you out :) – Andres Jan 25 '13 at 13:40
42

Actually you should be using

CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator

instead of

CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator

Using the second one gives you the OS default settings, which might be different then user Regional Locales for particular user account logged to this PC.

Credits to berhir and Grimm for pointing out the [docs]

Bart N
  • 995
  • 8
  • 7
  • 1
    I don't think this is correct. This is what the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentculture?view=netcore-3.1) say: `The current culture is a property of the executing thread. Retrieving the value of the CultureInfo.CurrentCulture property is a more performant equivalent of retrieving the CultureInfo object returned by the Thread.CurrentThread.CurrentCulture property.` – berhir Aug 27 '20 at 10:56
  • berhir is right. The docs clearly recommend to use CultureInfo: `The current culture is a property of the executing thread. When you set this property to a CultureInfo object that represents a new culture, the value of the Thread.CurrentThread.CurrentCulture property also changes. However, we recommend that you always use the CultureInfo.CurrentCulture property to retrieve and set the current culture.` – Grimm Jan 05 '22 at 09:58
  • It was some time ago but I think what I've ment was not to use CurrentUICulture (with UI inside). But I agree you have found more performant way to do so. Thank you for spotting this. – Bart N Jan 12 '22 at 16:49
1

You shouldn't use a while loop, I think it will freeze the application, use if instead, the problem might be here

ppetrov
  • 3,077
  • 2
  • 15
  • 27
  • where is the changed code..? looks the same in your original question – MethodMan Jan 25 '13 at 00:50
  • 1
    are you sure that 46 is the dot code? You could also try this test: `e.KeyChar == '.'` – ppetrov Jan 25 '13 at 00:52
  • ppetrov that does = `"."` test it this way you will see `var somChar = (char)46` – MethodMan Jan 25 '13 at 01:00
  • I believe you @DJ KRAZE, just didn't tested it and thought this way there can't be any error with the char code. but as you said it in a previous comment, the main problem with the code was the `.Equals` – ppetrov Jan 25 '13 at 01:05
  • not a problem pptrov.. but his initial problem lies within a few things ..1 using a While loop which he does not need, checking for "," which he will never find because he needs to check the Key , 3rd using the incorrect Control, he should use a MaskedEdit instead – MethodMan Jan 25 '13 at 01:08