Tuesday, March 24, 2009

Displaying the TAB escape character in a label

The '\t' character is a bit tricky. When you use the tab character, the compiler translates it to this squared character that we have all seen in class. This squared character is correctly interpreted, and displays perfectly in C# console. The only problem is that the tab character does not display properly in controls at form level.

Solution:
To display the escape character for '\t', I made use of the replace function.

private void button_Click(object sender, EventArgs e)
{
string s = "\tDenis\t";
s = s.Replace("\t", " ");
label1.Text = s.ToString();
}

3 comments:

  1. Hi, I found another way.
    -----------------------------------------------
    Firstly, change some properties of the label in properties window.

    Autosize : True ---> False
    FlatStyle : Standard --->System

    Next, write a code for display.

    myLabel.Text = "a\tb\naa\tb";
    -----------------------------------------------
    Actually, I don't know exactly what FlatStyle is...I have to check later.I refered to these pages:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/7ba39df1-1819-489d-b821-708cb7024b0c/

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/37d6f140-d708-4c49-b4e5-dd4cdeddee3f/

    ReplyDelete
  2. Wonderful! It works. Thanks Misa :)

    ReplyDelete
  3. Hey Misa,
    I was doing some research on textboxes in C#. Do you know how to display new lines in textboxes with multiple lines, given that their fixedsize is set to false?
    I cannot the \t and \n to work.

    However, \t and \n works fine with the RichTextEditor...

    ReplyDelete