This is a migrated thread and some comments may be shown as answers.

Format value for textbox

3 Answers 420 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
phuoc tran ngoc
Top achievements
Rank 1
phuoc tran ngoc asked on 20 Oct 2011, 09:35 AM
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!

3 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 21 Oct 2011, 01:35 PM
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.

0
phuoc tran ngoc
Top achievements
Rank 1
answered on 25 Oct 2011, 09:10 AM
Hi Ivan

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

Thanks!
0
Ivan Petrov
Telerik team
answered on 28 Oct 2011, 11:27 AM
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.

Tags
TextBox
Asked by
phuoc tran ngoc
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
phuoc tran ngoc
Top achievements
Rank 1
Share this question
or