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.
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.)
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.
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)
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...
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.