#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    451
    Rep Power
    0

    [C#.NET] Limit lines of a multiline textBox?


    I've written a small application that works wonders for me, but I realized that the textBox (multiline) I have only shows the first 5 lines (a size constraint, on purpose... think of the boxes of a school lunch menu). Is there a way to limit the textBox to accept only upto 5 lines of text before it just stops accepting any more text.

    I'm using C# .NET in Windows just incase you're curious!


    EDIT: Unforunately I started the project as a .NET 2.0 Project since I started this one many moons ago. I understand .NET 3.5 has a "MaxLines" property which 2.0 didn't have. Is there a way of doing this in 2.0?
    Last edited by phpdev4; June 23rd, 2009 at 10:22 PM.
    Compaq Presario C762NR
    Windows XP Home Edition - HP OEM (SP3; Self-downgraded from VHP SP1)
    Intel Pentium D (2 x 1.73GHz)
    2 x 1GB G-SkiLL RAM (DDR2 @ 800MHz)
    160GB SATA 1.5Gb/s HD @ 5400RPM
  2. #2
  3. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    Maybe think along these lines... I believe that text boxes are usually a fixed character width (ie - each character is X pixels wide, like when using the <pre> tag). So if you specify a width of 100 characters for the box, and a maximum length of 500, then, in effect, you have a 5 line maximum.
    Discontent is the first necessity of progress. - Edison
  4. #3
  5. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    Here's what I was looking for...

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.columns.aspx

    So if you're looking for 5 lines just and the # of columns you choose is 100, then make the maximum length property 500.

    Hope this makes sense.
    Discontent is the first necessity of progress. - Edison
  6. #4
  7. No Profile Picture
    Turning coffee into code
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    UT Austin, TX
    Posts
    164
    Rep Power
    99
    Originally Posted by mateoc15
    Maybe think along these lines... I believe that text boxes are usually a fixed character width (ie - each character is X pixels wide, like when using the <pre> tag). So if you specify a width of 100 characters for the box, and a maximum length of 500, then, in effect, you have a 5 line maximum.
    Very rarely will the lines take the full 100 character width, so 500 characters will probably allow more than 5 lines. You could derive a class from the TextBox control that checks the number of lines in the box before adding a typed character to the text. The advantage of this is you can make MaxLines a property that will show/be editable in the form designer.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    892
    See the Control.TextChanged event. You'll have to count the lines in the string and remove either the newest, first or last one if there's more than five.
    I no longer wish to be associated with this site.
  10. #6
  11. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    Originally Posted by jwdonahue
    See the Control.TextChanged event. You'll have to count the lines in the string and remove either the newest, first or last one if there's more than five.
    So what's wrong with using the Columns property?

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.columns.aspx
    Discontent is the first necessity of progress. - Edison
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    892
    Originally Posted by mateoc15
    So what's wrong with using the Columns property?
    The math just doesn't work unless you set the max columns to 1, then if the max bytes is 5 you just can't have more than 5 lines right? But what if columns is set to 2 with max bytes of 10, now I could have 10 lines 1 byte in length, 5 lines of 2 bytes in length, 1 line that is 2 bytes and 8 lines that are 1 byte, etc.
    I no longer wish to be associated with this site.
  14. #8
  15. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,319
    Rep Power
    1515
    From a usability standpoint, you want to allow the additional text to be entered, but show an error and not validate or accept a submission until they correct it. This will allow the user to do things like paste in text that might be too large from a different source, and then go and clean it up/trim it down until the error goes away.

    Comments on this post

    • mateoc15 agrees
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  16. #9
  17. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    Duh... why did no one (me) think of this before? That's probably the best way to go.
    Discontent is the first necessity of progress. - Edison

IMN logo majestic logo threadwatch logo seochat tools logo