Invariant US format in NumericUpDown

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pamela fluente

    Invariant US format in NumericUpDown

    My numericUpDowns show numbers in the format 1.500,56 (italy).

    Instead, I need *invariantly* that they show and accept the format
    1,500.56, as in the USA.

    What's the right way to do that? I have tried to set the current
    culture to
    the US culture, but nothing changed.

    For instance I tried without success (vb) :
    Application.Cur rentCulture =
    Globalization.C ultureInfo.GetC ultureInfo("en-US")

    Both a vb or c# example will be very useful.

    Thanks a lot.


    -P
  • Peter Duniho

    #2
    Re: Invariant US format in NumericUpDown

    On Wed, 22 Oct 2008 17:24:08 -0700, pamela fluente
    <pamelafluente@ libero.itwrote:
    My numericUpDowns show numbers in the format 1.500,56 (italy).
    >
    Instead, I need *invariantly* that they show and accept the format
    1,500.56, as in the USA.
    >
    What's the right way to do that? [...]
    I'm not sure you can. You could sub-class NumericUpDown, and override
    UpdateEditText( ) and ValidateEditTex t(). But ParseEditText() isn't
    virtual, so I'm not sure you can completely override the built-in behavior.

    That said, I think as a user I'd be at least somewhat annoyed if an
    application ignored my current culture settings. I hope you have a
    _really_ good reason for doing that.

    Pete

    Comment

    • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

      #3
      RE: Invariant US format in NumericUpDown


      "pamela fluente" wrote:
      My numericUpDowns show numbers in the format 1.500,56 (italy).
      >
      Instead, I need *invariantly* that they show and accept the format
      1,500.56, as in the USA.
      >
      What's the right way to do that? I have tried to set the current
      culture to
      the US culture, but nothing changed.
      >
      For instance I tried without success (vb) :
      Application.Cur rentCulture =
      Globalization.C ultureInfo.GetC ultureInfo("en-US")
      >
      Both a vb or c# example will be very useful.
      >
      Thanks a lot.
      >
      >
      -P
      >
      Hi Pamela,

      Setting Application.Cul tureInfo will change the culture for the entire
      application. NumericUpDown will display 1,500.56 (if you set it after
      InitializeCompo nent it will initially display 1.500,56 and switch format when
      you start using it). If you set it as part of an inherited usercontrol you
      may even mess up the Visual Studio designer and need a restart to be able to
      reset the culture.

      As Peter pointed out, forcing a culture is rarely a good idea, if ever, and
      shouldn't be necessary either as it is only the displayed format that
      changes. Internally it doesn't matter if the decimal is ',' or the
      thousandseparat or is. The only time I can think it may be an issue if you in
      some way use the Text property of the NumericUpDown instead of its Value

      --
      Happy Coding!
      Morten Wennevik [C# MVP]

      Comment

      • pamela fluente

        #4
        Re: Invariant US format in NumericUpDown

        On 23 Ott, 07:48, Morten Wennevik [C# MVP]
        <MortenWenne... @hotmail.comwro te:
        "pamela fluente" wrote:
        My numericUpDowns show numbers in the format 1.500,56 (italy).
        >
        Instead, I need *invariantly* that they show and accept the format
        1,500.56, as in the USA.
        >
        What's the right way to do that?  I have tried to set the current
        culture to
        the US culture, but nothing changed.
        >
        For instance I tried  without success (vb) :
         Application.Cur rentCulture =
        Globalization.C ultureInfo.GetC ultureInfo("en-US")
        >
        Both a vb or c#  example will be very useful.
        >
        Thanks a lot.
        >
        -P
        >
        Hi Pamela,
        >
        Setting Application.Cul tureInfo will change the culture for the entire
        application.  NumericUpDown will display 1,500.56 (if you set it after
        InitializeCompo nent it will initially display 1.500,56 and switch format when
        you start using it).  If you set it as part of an inherited usercontrolyou
        may even mess up the Visual Studio designer and need a restart to be ableto
        reset the culture.
        >
        As Peter pointed out, forcing a culture is rarely a good idea, if ever, and
        shouldn't be necessary either as it is only the displayed format that
        changes.  Internally it doesn't matter if the decimal is ',' or the
        thousandseparat or is.  The only time I can think it may be an issue if you in
        some way use the Text property of the NumericUpDown instead of its Value
        >
        --
        Happy Coding!
        Morten Wennevik [C# MVP]- Nascondi testo citato
        >
        - Mostra testo citato -


        Yes, the reason is that this is a trading application on the NYSE,
        AMEX, etc. and working with dollars.

        I need to be consistent and use always the same format. I have noticed
        that several time the user here typed numbers as 1000.23 $ for
        instance (getting the wrong input)
        it's rare that someone does 1000,23 $ because they are used to see the
        US notation in stock market or exchanges.

        In my case it would be very nice that the application takes and
        displays the numbers
        in the same way everywhere.


        I have not tried Setting Application.Cul tureInfo before
        InitializeCompo nent. I will try.

        Let me know please if you see a way to do this the right way.


        -P

        Comment

        • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

          #5
          Re: Invariant US format in NumericUpDown


          "pamela fluente" wrote:
          On 23 Ott, 07:48, Morten Wennevik [C# MVP]
          <MortenWenne... @hotmail.comwro te:
          "pamela fluente" wrote:
          My numericUpDowns show numbers in the format 1.500,56 (italy).
          Instead, I need *invariantly* that they show and accept the format
          1,500.56, as in the USA.
          What's the right way to do that? I have tried to set the current
          culture to
          the US culture, but nothing changed.
          For instance I tried without success (vb) :
          Application.Cur rentCulture =
          Globalization.C ultureInfo.GetC ultureInfo("en-US")
          Both a vb or c# example will be very useful.
          Thanks a lot.
          -P
          Hi Pamela,

          Setting Application.Cul tureInfo will change the culture for the entire
          application. NumericUpDown will display 1,500.56 (if you set it after
          InitializeCompo nent it will initially display 1.500,56 and switch format when
          you start using it). If you set it as part of an inherited usercontrol you
          may even mess up the Visual Studio designer and need a restart to be able to
          reset the culture.

          As Peter pointed out, forcing a culture is rarely a good idea, if ever, and
          shouldn't be necessary either as it is only the displayed format that
          changes. Internally it doesn't matter if the decimal is ',' or the
          thousandseparat or is. The only time I can think it may be an issue if you in
          some way use the Text property of the NumericUpDown instead of its Value

          --
          Happy Coding!
          Morten Wennevik [C# MVP]- Nascondi testo citato

          - Mostra testo citato -
          >
          >
          >
          Yes, the reason is that this is a trading application on the NYSE,
          AMEX, etc. and working with dollars.
          >
          I need to be consistent and use always the same format. I have noticed
          that several time the user here typed numbers as 1000.23 $ for
          instance (getting the wrong input)
          it's rare that someone does 1000,23 $ because they are used to see the
          US notation in stock market or exchanges.
          >
          In my case it would be very nice that the application takes and
          displays the numbers
          in the same way everywhere.
          >
          >
          I have not tried Setting Application.Cul tureInfo before
          InitializeCompo nent. I will try.
          >
          Let me know please if you see a way to do this the right way.
          >
          >
          -P
          >
          In this case I think I would have used a regular TextBox and handled the
          DataBinding format and parse events and possibly created my own up and down
          buttons, or created a UserControl containing of a NumericUpDown control with
          a TextBox control on top.

          Using a regular TextBox

          TextBox tb = new TextBox();
          Controls.Add(tb );

          BindingSource bs = new BindingSource(t his, "");

          Binding binding = new Binding("Text", bs, "Value", true,
          DataSourceUpdat eMode.OnPropert yChanged);
          binding.Format += new ConvertEventHan dler(binding_Fo rmat);
          binding.Parse += new ConvertEventHan dler(binding_Pa rse);
          tb.DataBindings .Add(binding);
          }

          void binding_Parse(o bject sender, ConvertEventArg s e)
          {
          string s = (string)e.Value ;
          e.Value = decimal.Parse(s , CultureInfo.Get CultureInfo("en-US"));
          }

          void binding_Format( object sender, ConvertEventArg s e)
          {
          decimal d = (decimal)e.Valu e;
          e.Value = d.ToString("N2" , CultureInfo.Get CultureInfo("en-US"));
          }

          public decimal Value { get; set; }
          --
          Happy Coding!
          Morten Wennevik [C# MVP]

          Comment

          • pamela fluente

            #6
            Re: Invariant US format in NumericUpDown

            >
            In this case I think I would have used a regular TextBox and handled the
            DataBinding format and parse events and possibly created my own up and down
            buttons, or created a UserControl containing of a NumericUpDown control with
            a TextBox control on top.
            >
            Using a regular TextBox
            >
                        TextBox tb = new TextBox();
                        Controls.Add(tb );
            >
                        BindingSource bs = new BindingSource(t his, "");
            >
                        Binding binding = new Binding("Text", bs, "Value", true,
            DataSourceUpdat eMode.OnPropert yChanged);
                        binding.Format += new ConvertEventHan dler(binding_Fo rmat);
                        binding.Parse += new ConvertEventHan dler(binding_Pa rse);
                        tb.DataBindings .Add(binding);
                    }
            >
                    void binding_Parse(o bject sender, ConvertEventArg s e)
                    {
                        string s = (string)e.Value ;
                        e.Value = decimal.Parse(s , CultureInfo.Get CultureInfo("en-US"));
                    }
            >
                    void binding_Format( object sender, ConvertEventArg s e)
                    {
                        decimal d = (decimal)e.Valu e;
                        e.Value = d.ToString("N2" , CultureInfo.Get CultureInfo("en-US"));
                    }
            >
                    public decimal Value { get; set; }
            --
            Happy Coding!
            Morten Wennevik [C# MVP]- Nascondi testo citato
            >
            Thanks a lot Morten for your code and kindness.

            I was actually hoping there was a simpler way to tell a control to
            show and accept numbers in the US format. But ... I guess I need to go
            your way!

            I will try that. Thanks a Lot. Very helpful.

            -P



            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Invariant US format in NumericUpDown

              Pamela,



              Strange as I am as well in a non English European Language Culture

              Does this show to me

              Application.Cur rentCulture =
              Globalization.C ultureInfo.GetC ultureInfo("en-US")
              MessageBox.Show (1000.1.ToStrin g)

              1000.1

              Are you sure you are not filling the combobox with strings?

              Cor


              "pamela fluente" <pamelafluente@ libero.itschree f in bericht
              news:e9d9c1f1-593e-4463-b665-95263ef2068b@y2 9g2000hsf.googl egroups.com...
              My numericUpDowns show numbers in the format 1.500,56 (italy).
              >
              Instead, I need *invariantly* that they show and accept the format
              1,500.56, as in the USA.
              >
              What's the right way to do that? I have tried to set the current
              culture to
              the US culture, but nothing changed.
              >
              For instance I tried without success (vb) :
              Application.Cur rentCulture =
              Globalization.C ultureInfo.GetC ultureInfo("en-US")
              >
              Both a vb or c# example will be very useful.
              >
              Thanks a lot.
              >
              >
              -P

              Comment

              • pamela fluente

                #8
                Re: Invariant US format in NumericUpDown

                On 24 Ott, 13:13, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                wrote:
                Pamela,
                >
                Strange as I am as well in a non English European Language Culture
                >
                Does this show to me
                >
                Application.Cur rentCulture =
                Globalization.C ultureInfo.GetC ultureInfo("en-US")
                MessageBox.Show (1000.1.ToStrin g)
                >
                1000.1
                >
                Are you sure you are not filling the combobox with strings?
                >
                Cor
                Thank you Cor, Actually now that you say that, I have tried a little
                experiment (code below)
                and it seems to be working (?).

                Now I want to go back to my original code and see why it was not
                working there ...

                -P

                Public Class Form1

                Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
                As System.EventArg s) Handles Button1.Click

                Dim f As New Form1
                Dim n As New NumericUpDown
                n.DecimalPlaces = 2
                n.ThousandsSepa rator = True
                f.Controls.Add( n)

                n.Maximum = 100000000
                n.Value = 1000.1
                f.Show()

                End Sub

                Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
                System.EventArg s) Handles MyBase.Load
                Application.Cur rentCulture =
                Globalization.C ultureInfo.GetC ultureInfo("en-US")
                End Sub

                End Class

                Comment

                Working...