Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align numbers in RichTextBox

I want to print a multiplication table in C# but it's not aligned! When we type a number "n" in textbox means: n*n table. What can I do?

for (int i = 1; i <= Convert.ToInt32(textBox1.Text); i++)
{
    for (int j = 1; j <= Convert.ToInt32(textBox1.Text); j++)
    {
        richTextBox1.Text += Convert.ToString(i * j) + " ";
    }
    richTextBox1.Text += "\n";
}

enter image description here

like image 501
Sina Jazani Avatar asked Jan 06 '23 13:01

Sina Jazani


2 Answers

Set the font of RichTextBox to the monospaced font Courier New, then add the text to RichTextBox using String.Format and setting alignment for the result of multiplication. Use a positive number to align right and use a negative number to align left:

var n = 5;
for (int i = 1; i <= n; i++)
{
    for (int j = 1; j <= n; j++)
    {
        this.richTextBox1.AppendText(string.Format("{0,3}", i * j));
    }
    this.richTextBox1.AppendText(Environment.NewLine);
}

Instead of format the result by {0,3} you can use below code to format based on maximum length of characters is a number which belongs to n*n:

Left Aligned:

string.Format("{0,-" +((n*n).ToString().Length + 1).ToString() +"}", i * j)

enter image description here

Right Aligned:

string.Format("{0," +((n*n).ToString().Length + 1).ToString() +"}", i * j)

enter image description here

like image 90
Reza Aghaei Avatar answered Jan 09 '23 02:01

Reza Aghaei


If you want to align using spaces, you need to use a monospaced font (like Courier, or Consolas), otherwise you can use tabs: numbers won't be aligned this way though, and since numbers in your routine can get considerably big, you may end up having your numbers occupy more than the tab separation, and will get inconsistencies in the alignment if that happens.

As a general rule, if you want to align any kind of text box, go with a monospaced font.

You can pad with spaces, using, for example, String.PadLeft or String.PadRight.

This would be as simple as changing:

richTextBox1.Text += Convert.ToString(i * j) + " ";

With

richTextBox1.Text += Convert.ToString(i * j).PadLeft(5);

However this would assume all numbers are at maximum 5 characters in width.

For your precise routine, you could calculate the maximum width though, so you'd end up with something like:

 // convert your input only once
 int myNumber = Convert.ToInt32(textBox1.Text);
 // pad with the maximum possible length, plus one space
 int padAmount = (myNumber * myNumber).ToString().Length + 1; 
 for (int i = 1; i <= myNumber; i++)
 {
   for (int j = 1; j <= myNumber; j++)
   {
     // pad your input by the amount of spaces needed to fit all possible numbers
     richTextBox1.Text += (i*j).ToString().PadLeft(padAmount);
   }
 }
 // use Environment.NewLine instead of `\n`
 richTextBox1.Text += Environment.NewLine;

Here's a fiddle. It's (for obvious reasons) for console, so in my fiddle the input number is fixed (it's in myNumber) and the output is just a string (instead of richTextBox1.Text), but it should show how it works.

Although I've made a few changes (I only convert the input number once, and use Environment.NewLine instead of \n), this is far from optimal though, you should build your string (using a StringBuilder) and assign it at once, instead of adding to the Text property. I've made a fiddle with this approach, and memory consumption has gone down by over 30mb (to just a handful of kb) just by using StringBuilder.

like image 23
Jcl Avatar answered Jan 09 '23 02:01

Jcl