tags:

views:

239

answers:

4

This should be very simple.

I have a Label control on my Form and I am trying to put a tab character between text

Label.Text = "Is there a\ttab";

The output is "Is there atab";

What am I doing wrong?

A: 

Nothing, windows forms labels are very limited in functionality and don't support the \t character.

A (slightly awkward) alternative might be:

label1.Text = "test\ting\t123".Replace("\t","    ");
Ash
Really? Weird. Any alternative approaches?
Jon
Sadly, the only thing I can think of (not very helpful) is to use WPF instead :(
Ash
+1  A: 

Tab is actually a non-printing character—or rather, a control character. What it does is entirely dependent on the application. What exactly do you expect? 8 spaces? 4 spaces? As many spaces as needed to get to a multiple of 8 columns? Indentation of the following text by one cm?

To put it short: The Label control doesn't support tabs. Actually, Label just uses normal graphics routines for rendering its text and how should they know what you intend to do with your tab character?

If you need to display that character as a number of spaces, then you should replace it by that number of spaces.

Joey
I guess my code should be:string tab = " ";label.text = "Is there a" + tab + "tab";
Jon
If you want a space there, maybe. You still didn't specify what you expect it to display.
Joey
I want it to display whatever a tab is which is about 4-5 spaces
Jon
@Jon: Well, as noted, whatever a tab is *is up to your program*. There is no single right way of representing a tab. Go ahead, ask your printer, your favorite text editor and your favorite word processing application what a tab actually *is*. You'll get three very different answers.
Joey
@Jon: If you want to display spaces, then you need to put spaces there. It's as simple as that.
Joey
A: 

Right, to insert a tab, just add the spaces desired.

If you want to offset the next by a specified length, you could try

int offset_text = 20;
label1.Text = "Is there a".PadRight(offset_text)+"Tab";
label2.Text = "More Text".PadRight(offset_text)+"Too";
SwDevMan81
Are you sure this works? PadRight will have no effect in this case because "Is there a" is longer than 3 characters.
Ash
A: 

Just use a literal string and you should be good to go...

label1.Text = @"Test for            Tab";

Where that big space is where I actually hit tab three times...hope this helps

curtisk