Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Editors (TextBox, MaskedEdit, SpinEditor, BrowseEditor, ColorBox) > Format value for textbox

Not answered Format value for textbox

Feed from this thread
  • phuoc tran ngoc avatar

    Posted on Oct 20, 2011 (permalink)

    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!

    Reply

  • Ivan Petrov Ivan Petrov admin's avatar

    Posted on Oct 21, 2011 (permalink)

    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.

    Reply

  • phuoc tran ngoc avatar

    Posted on Oct 25, 2011 (permalink)

    Hi Ivan

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

    Thanks!

    Reply

  • Ivan Petrov Ivan Petrov admin's avatar

    Posted on Oct 28, 2011 (permalink)

    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.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Editors (TextBox, MaskedEdit, SpinEditor, BrowseEditor, ColorBox) > Format value for textbox
Related resources for "Format value for textbox"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]