locked
How can I simulate MaxLines on a TextBox? RRS feed

  • Question

  • I need TextBox.MaxLines, but it's not there for the Silverlight TextBox.  When I opened the WPF version to look at the implementation, forget it, it's a massive web of dependencies.

    I just want to limit the text in my TextBox such that it doesn't go out of view.  MaxLength doesn't work because AcceptsReturn is true and the user could enter several short lines.

    Saturday, May 7, 2011 2:18 AM

Answers

  • The link to the source is busted, but I strongly suspect it's similar to things I've tried.  I had code with a decimal constant that I played with too, but there is no constant that works for all possible sentence structures.  I could change the constant to get that sample to work, but other longer sentences would break.

    Try this and let me know if it breaks. http://www.codeproject.com/KB/silverlight/XTextBox.aspx :)

    Wednesday, August 31, 2011 4:42 AM

All replies

  • add textchanged event and handle the maxlines in this event

    Saturday, May 7, 2011 2:39 AM
  • add textchanged event and handle the maxlines in this event

    How do I know whether I have 3 lines?  There doesn't seem to be any way to determine how much space text is occupying.  I need something like WPF's TextRenderer.MeasureText.

    Is my only option to drop down to PInvoke and call the OS APIs for measuring text like I used to do 10 years ago?

    Tuesday, May 10, 2011 12:45 AM
  •          Is my only option to drop down to PInvoke and call the OS APIs for measuring text like I used to do 10 years ago?

    Whoops, I was thinking one could do PInvoke in SL4; that won't be available until SL5.  So there's no way for me to accomplish this?

    Wednesday, May 11, 2011 5:20 PM
  • Handle the Keydown of the textbox and count the number of times the enter key is pressed (if thats the only way to create a new line). This way you can handle the number of lines you want to create.

    You mentioned something about blocking your textbox from going out of view. You can set the maxwidth/ht to prevent this or you can set a static width/ht and add a scrollbar to it.

    Wednesday, May 11, 2011 5:41 PM
  • Handle the Keydown of the textbox and count the number of times the enter key is pressed (if thats the only way to create a new line). This way you can handle the number of lines you want to create.

    I didn't explain my problem well enough.  If the user enters 3 short rows of text, that would be easy enough to handle in the way you describe.  The problem is that I can't allow overflow, nor can I allow scrollbars (printing the control as is).  Thus I need to limit the number of lines to 3 and also limit the full amount of text entered if they enter a bunch of it w/o any newlines (TextWrapping is turned on).  For the second problem, MaxLength doesn't work because I'm using a variable width font (that would only work if using a fixed width font).

    This is how I would interpret "MaxLines."  Make sense?

    Saturday, May 14, 2011 5:41 PM
  • Okay, Let me see if i got this right?

    "I can't allow overflow, nor can I allow scrollbars (printing the control as is).Thus I need to limit the number of lines to 3 and also limit the full amount of text entered if they enter a bunch of it w/o any newlines (TextWrapping is turned on)."

    You need a restriction on the number of characters(width) in a line as well as a restriction on the number of lines?

    It would be great if you could post a diagram of some sort explaining what you need.




    Monday, May 16, 2011 2:59 PM
  • I have the exact the same problem. My workaround is to extend the regular textbox added new property MaxLines and handle the limitation in my own logic. It's a pain in the ass and its not that accurate.

    Friday, August 5, 2011 5:14 AM
  • here's my solution. It seems to work perfectly now. Cool

    http://briggs69.blogspot.com/2011/08/solution-maxlines-property-in.html

    Thursday, August 18, 2011 2:03 PM
  • here's my solution. It seems to work perfectly now. Cool

    http://briggs69.blogspot.com/2011/08/solution-maxlines-property-in.html

    My first test didn't work.  Enter this text:

    one

    two

    three

    four

    five

    six.  On the sixth line, we want to limit the text, you see <-- text after "you" wraps to next line.

    Friday, August 19, 2011 10:13 AM
  • Get the source code. Try changing(reducing) the decimal value multiplied to the linewidth in the constructor of the Xtextbox. This is to calibrate the algorithm for your needs.

    Friday, August 19, 2011 8:32 PM
  • The link to the source is busted, but I strongly suspect it's similar to things I've tried.  I had code with a decimal constant that I played with too, but there is no constant that works for all possible sentence structures.  I could change the constant to get that sample to work, but other longer sentences would break.

    Saturday, August 20, 2011 11:05 PM
  • Dang.  I thought for sure this solution by Charles Petzold would work.  It's close, but no better than my own hack of setting the text of a hidden auto-sizing TextBlock on every keypress and getting it's width.  Maybe it works differently on Silverlight on the phone.

    Friday, August 26, 2011 11:41 PM
  • The link to the source is busted, but I strongly suspect it's similar to things I've tried.  I had code with a decimal constant that I played with too, but there is no constant that works for all possible sentence structures.  I could change the constant to get that sample to work, but other longer sentences would break.

    Try this and let me know if it breaks. http://www.codeproject.com/KB/silverlight/XTextBox.aspx :)

    Wednesday, August 31, 2011 4:42 AM