313

I'm building an application where I should capture several values and build a text with them: Name, Age, etc.

The output will be a plain text into a TextBox.

I am trying to make those information appear in kind of columns, therefore I am trying to separate them with tab to make it clearer.

For example, instead of having:

Ann 26
Sarah 29
Paul 45

I would like it to show as:

Ann    26
Sarah  29
Paul   45

Any tip on how to insert the tabs into my text?

schlebe
  • 3,387
  • 5
  • 37
  • 50
Layla
  • 4,175
  • 7
  • 26
  • 20
  • 1
    A word of warning.....if the length of the name field is too long, you still won't get them to align. "Ann" is only 3 characters. "Jeremiah" is 8. A single tab added to "Ann" might make the ## appear before the end of jeremiah. I would suggest parsing by number of characters. "Split" by space, add spaces to the first member until 12 characters, and THEN add "26". If you have a name that might be longer than 12, adjust – KeachyPeenReturns Nov 10 '17 at 13:36
  • I just read comment of KeachyPeenReturns after having posted a solution. His remark is correct but his solution is imperfect. The BEST answer is already incorrect. Please take time to read my solution and to update status (BEST answer) for this question. Normally, I don't post this type of comment but the question is very old. – schlebe Jan 30 '19 at 07:01

9 Answers9

532

Try using the \t character in your strings

SteveC
  • 15,808
  • 23
  • 102
  • 173
DShook
  • 14,833
  • 9
  • 45
  • 55
482

Hazar is right with his \t. Here's the full list of escape characters for C#:

\' for a single quote.

\" for a double quote.

\\ for a backslash.

\0 for a null character.

\a for an alert character.

\b for a backspace.

\f for a form feed.

\n for a new line.

\r for a carriage return.

\t for a horizontal tab.

\v for a vertical tab.

\uxxxx for a unicode character hex value (e.g. \u0020).

\x is the same as \u, but you don't need leading zeroes (e.g. \x20).

\Uxxxxxxxx for a unicode character hex value (longer form needed for generating surrogates).

Dan R
  • 5,258
  • 2
  • 16
  • 10
81

It can also be useful to use String.Format, e.g.

String.Format("{0}\t{1}", FirstName,Count);
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
david valentine
  • 4,625
  • 2
  • 21
  • 15
  • 17
    This is the best answer because code speaks longer than one liners and long explanations. – Phil Aug 10 '10 at 19:44
5

Using Microsoft Winform controls, it is impossible to solve correctly your problem without an little workaround that I will explain below.

PROBLEM

The problem in using simply "\t" or vbTab is that when more than one TextBox are displayed and that alignment must be respected for all TextBox, the ONLY "\t" or vbTab solution will display something that is NOT ALWAYS correctly aligned.

Example in VB.Net:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.Text = "Bernard" + vbTab + "32"
    TextBox2.Text = "Luc" + vbTab + "47"
    TextBox3.Text = "François-Victor" + vbTab + "12"
End Sub

will display

enter image description here

as you can see, age value for François-Victor is shifted to the right and is not aligned with age value of two others TextBox.

SOLUTION

To solve this problem, you must set Tabs position using a specific SendMessage() user32.dll API function as shown below.

Public Class Form1

    Public Declare Function SendMessage _
        Lib "user32" Alias "SendMessageA" _
        ( ByVal hWnd As IntPtr _
        , ByVal wMsg As Integer _
        , ByVal wParam As Integer _
        , ByVal lParam() As Integer _
        ) As Integer

    Private Const EM_SETTABSTOPS As Integer = &HCB

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim tabs() As Integer = {4 * 25}

        TextBox1.Text = "Bernard" + vbTab + "32"
        SendMessage(TextBox1.Handle, EM_SETTABSTOPS, 1, tabs)
        TextBox2.Text = "Luc" + vbTab + "47"
        SendMessage(TextBox2.Handle, EM_SETTABSTOPS, 1, tabs)
        TextBox3.Text = "François-Victor" + vbTab + "12"
        SendMessage(TextBox3.Handle, EM_SETTABSTOPS, 1, tabs)
    End Sub

End Class

and following Form will be displayed

enter image description here

You can see that now, all value are correctly aligned :-)

REMARKS

Multiline property of the TextBox must be set to True. If this properties is set to False, the Tab is positioned as before.

How AcceptsTab property is assigned is not important (I have tested).

This question has already be treated on StackOverflow

Caution: the mesure Unit for Tab position is not character but something that seems to be 1/4 of character. That is why I multiply the length by 4.

C# SOLUTION

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, uint[] lParam);
        private const int EM_SETTABSTOPS = 0x00CB;
        private const char vbTab = '\t';

        public Form1()
        {
            InitializeComponent();

            var tabs = new uint[] { 25 * 4 };

            textBox1.Text = "Bernard" + vbTab + "32";
            SendMessage(textBox1.Handle, EM_SETTABSTOPS, 1, tabs);
            textBox2.Text = "Luc" + vbTab + "47";
            SendMessage(textBox2.Handle, EM_SETTABSTOPS, 1, tabs);
            textBox3.Text = "François-Victor" + vbTab + "12";
            SendMessage(textBox3.Handle, EM_SETTABSTOPS, 1, tabs);
        }
    }
}
schlebe
  • 3,387
  • 5
  • 37
  • 50
4
var text = "Ann@26"

var editedText = text.Replace("@", "\t");
Robert
  • 5,278
  • 43
  • 65
  • 115
MafazR
  • 41
  • 3
2

There are several ways to do it. The simplest is using \t in your text. However, it's possible that \t doesn't work in some situations, like PdfReport nuget package.

Amin Saqi
  • 18,549
  • 7
  • 50
  • 70
2

When using literal strings (start with @") this might be easier

char tab = '\u0009';
string A = "Apple";
string B = "Bob";
string myStr = String.Format(@"{0}:{1}{2}", A, tab, B);

Would result in Apple:<tab>Bob

Hecatonchires
  • 1,009
  • 4
  • 13
  • 42
1
string St = String.Format("{0,-20} {1,5:N1}\r", names[ctr], hours[ctr]);
richTextBox1.Text += St;

This works well, but you must have a mono-spaced font.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
CERI
  • 19
  • 1
1

In addition to the anwsers above you can use PadLeft or PadRight:

string name = "John";
string surname = "Smith";

Console.WriteLine("Name:".PadRight(15)+"Surname:".PadRight(15));
Console.WriteLine( name.PadRight(15) + surname.PadRight(15));

This will fill in the string with spaces to the left or right.

Heersert
  • 33
  • 6