Width of a tab sequence
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Width of a tab sequence

  1. #1
    Join Date
    Aug 2010
    Posts
    39

    Width of a tab sequence

    Can anyone tell me what the width of a tab sequence ( string L"\t" ) is when typed on a certain TextBox control ? I mean, is there a function that returns an answer in some sense, either in pixels or in some other measurement ? Obviously, it doesn't depend on number of characters, since each character has its own width.
    The reason I'm asking is because I want to align strings on the textbox in somewhat a table so that each column starts at the same position. I want something like this :
    Code:
    word             number1            number2
    longword         number3            number4
    word2            number5            number6
    But what I get is
    Code:
    word             number1            number2
    longword                            number3            number4
    word2            number5            number6
    I add a string L"\t\t\t\t" to the left sided strings in my loop, and in case a word is too long or too short, it shifts the right cells to the right or to the left until a neighboring tab position. Is there a better solution than controlling the number of '\t' symbols that I add to my string ?
    Thanks.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: Width of a tab sequence

    The tab width of a text box obviously is 8 characters for a fixed-width font. That's pretty common and I haven't seen any way to change that. For a variable-width font (like the default of MS Sans Serif 8.25 pt) the tab width appears to be 8 digits.

    BTW, your "longword" example even fits with the default font! (See attached image.)
    Attached Images Attached Images  
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: Width of a tab sequence

    use String::Format with padding.

  4. #4
    Join Date
    Aug 2010
    Posts
    39

    Re: Width of a tab sequence

    Thanks for all who replied. I actually needed the answer right away and couldn't wait, so I alone figured things you said. I just switched to Courier font since it is of fixed-width, and started using padding method for strings, and everything now works the way I want.
    Eri523, that example with iiiiiiiii and WWWWWW is exactly the problem I had, and the padding alone could not help. BTW, how is digit measured ? (you said tab is 8 digits for non-fixed size fonts)

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: Width of a tab sequence

    The size of a string in a certain font and font size can, for instance, be measured with an event handler like this little hack:

    Code:
        System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
        {
          Graphics ^gr = Graphics::FromHwnd(textBox1->Handle);
          gr->PageUnit = GraphicsUnit::Point;
          lblSize->Text = String::Concat("Text size: ", (gr->MeasureString(textBox1->Text, textBox1->Font)).ToString());
        }
    Well, "little" doesn't really mean "quick" here, as I had to scratch together the objects involved from all corners of the CTS. The Handle property of the text box, for instance, is not found in the docs on the TextBox class itself. It is rather inherited indirectly from the IWin32Window implementation in System::Windows::Forms::Control. Note that the Graphics object here is not used to actually draw something, but merely for the size calculation.

    If you are interested in more elaborate information about this, feel free to ask. I was already thinking of writing a little text size calculator anyway and might post it here if you're interested. This would take some time to write, though.

    The text size specification in the attached image has been generated by the code posted above, for instance. Note that the height specification is not equal to the font size because it accounts the entire line spacing rather than just the height of a norm character.

    The width of a single digit in MS Sans Serif 8.25, BTW, is 7.47522 pt, measured using the same method. But the width measured for 8 digits is 40.15576 pt, which obviously is not 8 times the value for the single digit. Maybe I should investigate some more about how that measurement actually works...
    Attached Images Attached Images  
    Last edited by Eri523; October 18th, 2010 at 09:40 PM. Reason: Corrected where the text box' Handle property actually is derived from
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Width of a tab sequence

    Oh cool, I did not know that Graphics had the MeasureString method.
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center