0

I'm using a TextBox to create an editable title on something that looks like a post-it note. I changed to a TextBox from a RichEditBox to see if that could solve my problem, which it didn't, so I'm willing to change back if that helps.

My problem is that I don't want the user to be able to enter more characters than fit in the set width of the TextBox, because I want the whole title to be visible. Setting a fixed limit to the amount of characters that can be entered doesn't really work since for example 10 large M's would fill the width of my TextBox, but other characters will only fill half of it. So I would like to compare the width of the text to the width of the TextBox, so I can restrict input beyond that point.

Edit: I'm using the Segoe UI font, and I don't really consider changing the font to one with characters of equal width as a solution.

oscar93
  • 13
  • 4
  • 1
    See http://stackoverflow.com/questions/4219798/determine-how-wide-a-rendered-character-is-in-net or http://stackoverflow.com/questions/721168/how-to-determine-the-size-of-a-string-given-a-font which may help – ChrisF Feb 23 '13 at 23:26
  • I've tried looking into the properties of the TextBox and RichEditBox and tried basically anything that could relate to this. I tried disabling wordwrap, the built-in scroll of RichEditBox and the AcceptsReturn property. I tried the DataWriter's MeasureString method, which turned out to measure the amount of characters. Couldn't find any other way of measuring the width of the string. – oscar93 Feb 23 '13 at 23:33
  • What about basing it off the width of the text box itself? – Brian Feb 23 '13 at 23:33
  • Don't have access to neither Graphics nor TextRenderer nor FormattedText in a Windows Store App. So the links don't really help. – oscar93 Feb 23 '13 at 23:42
  • The TextBox doesn't scale, there's like a space inside it that contains the text, so it can't extend the width of the TextBox. – oscar93 Feb 23 '13 at 23:44

2 Answers2

0

Set the TextBox Font to a Courier Font, then all characters are equal width and calculating max becomes trivial.

John
  • 633
  • 4
  • 9
  • Not really the perfect solution since I wanted the segoe font that is so iconic for the windows store apps. But still a nice backup solution! – oscar93 Feb 24 '13 at 11:44
  • Found the Segoe UI Mono font, which has the same property as the Courier Font. Close to perfect at least. I still find it strange that there is no better solution to this, so I'm gonna keep the question up and see if someone has a better solution. Thanks! – oscar93 Feb 24 '13 at 11:55
0

Max value telling by hard coding .

I am suck in programming world. i m try help you..

DOT.NET
  • 101
  • 14
  • Thx for the response, but as I stated in my question I already tried this, and it didn't really solve my problem. – oscar93 Feb 24 '13 at 11:40
  • I don't think you understood the question. He does not want to limit it to a specific *number* of characters; he wants to limit it to whatever the physical size of the text box is. – Andrew Barber Mar 03 '13 at 06:58