Establishing the length of a wx.TextCtrl

Hi,

I would like to establish the length of a wx.TextCtrl instance in terms of
number of characters (as opposed to pixels) i.e. if I only want the date
entered then I want the width of the wx.TextCtrl instance to be no more than
the largest possible entry e.g. possible formats might be limited to
dd-mm-yyyy, dd/mm/yyyy, dd/mmm/yyyy, dd mmm yyyy. So from these
possibilities, the maximum width would be 12 characters wide. But wxPython
seems to specify widths in pixels, which seems to be very low level and
specific.

Is there any way to specify the width of a control in multiples of character
width? Failing this, what would be the mechanism to calculate the width in
pixels given a string length and the current font?

Thanks
Peter

Peter Milliken
Software Engineer
ResMed
Phone: +61 2 9886-5059
Fax: +61 2 9878-5564

Warning: Copyright ResMed. Where the contents of this email and/or attachment includes materials prepared by ResMed, the use of those materials is subject exclusively to the conditions of engagement between ResMed and the intended recipient.

This communication is confidential and may contain legally privileged information. By the use of email over the Internet or other communication systems, ResMed is not waiving either confidentiality of, or legal privilege in,the content of the email and of any attachments.
If the recipient of this message is not the intended addressee, please call ResMed immediately on +61 2 9886 5000 Sydney, Australia.

Peter Milliken wrote:

Is there any way to specify the width of a control in multiples of character
width? Failing this, what would be the mechanism to calculate the width in
pixels given a string length and the current font?

The problem here is that most fonts are proportional, thus the width of a 12-character string could be significantly different depending on just what characters are in that string. Ones are much narrower than, say, zeros, so 11-11-2001 requires less space than 30-08-2000. You could probably construct a worst-case string, say MM/MMM/MMMM (Ms are usually the widest character in a given font), and measure the width of that. I believe that the measurement is done by wxDC.GetTextExtent(), which should return a size (in pixels) for a supplied string in the current font, as appropriate for the given DC. It may also be possible to get an average character width for the current font -- my memory is hazy (and I don't have the time to look anything up right now), but check for a wxDC.GetSystemMetrics(). I *think* it's part of wxDC, and I *think* that that's where you'd find this information, but it might be a property of the font object instead of system metrics...

Jeff Shannon
Technician/Programmer
Credit International