Format value for textbox

4 posts, 0 answers
  1. phuoc tran ngoc
    phuoc tran ngoc avatar
    9 posts
    Member since:
    Nov 2009

    Posted 20 Oct 2011 Link to this post

    Hi All

    I'm using the textbox to load data from database, the data is 10000. So how can the textbox show "10.000" instead of "10000"?
    I also try MaskeditBox but it doesn't work, the first time I run the project, the value show "10.000"  but when this MaskEditBox lost focus, the value return to 0.

    Thanks!
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 21 Oct 2011 Link to this post

    Hello Phuoc Tran Ngoc,

    Thank you for contacting us.

    RadTextBox does not offer formatting out of the box, but your scenario can be achieved by a few lines of code. One possible solution I came up with is using the TextChanged event of the RadTextBox and manually formatting the text. Here is a code snippet:

    private void radTextBox1_TextChanged(object sender, EventArgs e)
    {
      string str = this.radTextBox1.Text;
     
      str = str.Replace(".", String.Empty);
     
      if (str.Length < 4)
      {
        return;
      }
     
      int index = str.Length - 1;
      int threeCounter = 0;
         
      while (index >= 0)
      {
        threeCounter++;
     
        if (threeCounter == 3)
        {
          threeCounter = 0;
     
          if (index != 0)
          {
            str = str.Insert(index, ".");
          }
        }
     
        index--;
      }
     
      this.radTextBox1.TextChanged -= radTextBox1_TextChanged;
      int cursorPosition = this.radTextBox1.SelectionStart;
      this.radTextBox1.Text = str;
      this.radTextBox1.SelectionStart = cursorPosition + 1;
      this.radTextBox1.TextChanged += radTextBox1_TextChanged;
    }

    I hope you find this useful. If you need further assistance, I would be glad to help. Kind regards,
    Ivan Petrov
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. phuoc tran ngoc
    phuoc tran ngoc avatar
    9 posts
    Member since:
    Nov 2009

    Posted 25 Oct 2011 Link to this post

    Hi Ivan

    Thanks for your help. I will try it but Is there any property to do this?

    Thanks!
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 28 Oct 2011 Link to this post

    Hello Phuoc Tran Ngoc,

    Thank you for your reply.

    If my previous solution is not appropriate for your scenario, you can try using the RadSpinEditor. You should set the ThousandsSeparator property to true. You can also set the ShowUpDownButtons to false if you want to hide the buttons of the spin editor. This will keep the text formatted, but will not format it while you are typing.

    I hope this will help. If you have further questions, I would be glad to help you.

    Greetings,
    Ivan Petrov
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

Back to Top