Click to See Complete Forum and Search --> : Width of a tab sequence
MajinSaha
October 12th, 2010, 05:17 AM
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 :
word number1 number2
longword number3 number4
word2 number5 number6
But what I get is
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.
Eri523
October 13th, 2010, 07:21 PM
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.)
Arjay
October 13th, 2010, 11:35 PM
use String::Format with padding.
MajinSaha
October 17th, 2010, 06:20 AM
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)
Eri523
October 17th, 2010, 08:57 PM
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:
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. :ehh: Maybe I should investigate some more about how that measurement actually works... :o
Zaccheus
October 22nd, 2010, 03:00 PM
Oh cool, I did not know that Graphics had the MeasureString method. :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.