Limit number of lines of a Text Box

Started by lfe011969, January 27, 2011, 08:18:20 AM

Previous topic - Next topic

lfe011969

Is there some way of limiting the number of lines allowed inside a text box?  The titles on our drawings are a maximum of 4 lines and I project I'm working on allows the user to enter the title of the drawing into a text box which then updates the correct attribute of the title block.  However, right now if a user enters more than 4 lines of text into the text box then the title overlaps its boundary.  See below for an example of what I'm talking about.

I would appreciate any suggestions.  Thanks!


BazzaCAD

There isn't a property to limit the number of lines, but you could enable the "ReturnPressed" event & handle it there.
If the user presses return more then 4 times, then you could make the text box read-only or read the "text" value & truncate it if needed...

Code (autolisp) Select

(defun c:Untitled_Form1_TextBox1_OnReturnPressed (/)
)
a.k.a.
Barry Ralphs
barryDOTralphsATgmailDOTcom

lfe011969

I've actually tried that but I can't seem to get it to work.  Here is what I was using:

Code (autolisp) Select
(defun c:1_form1_dwgtitle_OnReturnPressed (/)
  (setq xxx (dcl_TextBox_GetLineCount 1_form1_dwgtitle))
  (if (< 4 xxx)
    (progn
      (dcl_MessageBox "WARNING!!!!\r\nDrawing Titles can only have 4 lines!!!" "WARNING")
      (dcl_TextBox_LineScroll 1_form1_dwgtitle 1)
    )
  )
)


I was hoping using the LineScroll property would make the cursor move back to the top line but it doesn't work with the code I have.

lfe011969

#3
Oops, I messed up the conditional statement.  I changed:

Code (autolisp) Select
(if (< 4 xxx)

to

Code (autolisp) Select
(if (<= 4 xxx)

and now it works.

Thanks for your help!

lfe011969

Even though my code works with ReturnPressed, I believe I've found a better solution for limiting the number of lines of text within a text box.

Using the EditChanged Text Box event, I've come up with some code that toggles the SetReturnAsTab value to accomplish my goal of limiting the number of lines.  If the number of lines equals 3 or less then SetReturnAsTab is set to nil and is then set to T when the number of lines is greater than 3.  This way once the user is typing his fourth line, the SetReturnAsTab is set to T resulting in setting the focus to the next control instead of acting as a carriage return within the text box.

Code (autolisp) Select
(defun c:1_form1_dwgtitle_OnEditChanged (NewValue /)
    (setq linecnt (dcl_TextBox_GetLineCount 1_form1_dwgtitle))
    (cond
      ((>= 3 linecnt)(dcl_Control_SetReturnAsTab 1_form1_dwgtitle nil))
      ((< 3 linecnt)(dcl_Control_SetReturnAsTab 1_form1_dwgtitle T))
    )
  )

owenwengerd


BazzaCAD

Ya great code, but I'm just curious, why don't you use attributes in your titleblock instead of Mtext. That would avoid this whole issue....
And they're much easier to code & script for....
a.k.a.
Barry Ralphs
barryDOTralphsATgmailDOTcom

lfe011969

Sorry for not answering back in a timely fashion, been busy at work....

Actually the object I'm updating with my Text Box is an attribute.  The reason I'm doing it this way is so that the user has an idea of what his title will look like on the drawing before he actually changes it.  Sometimes a title is just a little longer than a line and asthetically it looks better to break up the title so that the words are more equally distributed across multiple lines. See attached picture for an example.

Before this I was just using the Drawing Title in the Summary Info of the drawing to store the title which was then tied to a field within the titleblock.  By doing it this way there was just no way to break up the title (that I could figure out) as placing a \\N or \\P never made the title break where desired.  Many of the project leads requested this be changed so I began looking into using Open DCL.

If there is a better way then I'm all ears  :)

Lonnie

BazzaCAD

Oh you're using a multi-line attribute.
We just use single line attributes, so we don't get the extra line, like you showed in your original post...
But I guess you've worked around that now too. :)
a.k.a.
Barry Ralphs
barryDOTralphsATgmailDOTcom

lfe011969

Quote from: BazzaCAD on January 27, 2011, 09:41:21 AM
There isn't a property to limit the number of lines, but you could enable the "ReturnPressed" event & handle it there.
If the user presses return more then 4 times, then you could make the text box read-only or read the "text" value & truncate it if needed...

Based on your suggestion to truncate the text value of the box, I've come up with an enhancement to my code to prevent the user entering too many characters on the last line.  My original code only took into account if the user hits <ENTER> to go to the next line.  However my code wouldn't prevent the user from continuously typing while on the last line to the point where the text wraps down to the next line.  Theoretically the user could just keep typing without hitting <ENTER> forever  ;D

Hopefully with this little enhancement I've got everything covered.  Thanks for your suggestion.

Code (autolisp) Select
(defun c:1_form1_dwgtitle_OnEditChanged ( Newvalue / linecnt )
   (setq linecnt (dcl_TextBox_GetLineCount 1_form1_dwgtitle))
   (cond
     ((>= 3 linecnt)(dcl_Control_SetReturnAsTab 1_form1_dwgtitle nil))
     ((< 3 linecnt)(dcl_Control_SetReturnAsTab 1_form1_dwgtitle T))
   )
   (if (> linecnt 4)
     (progn
       (setq txt1 (dcl_TextBox_GetLine 1_form1_dwgtitle 0))
       (setq txt2 (dcl_TextBox_GetLine 1_form1_dwgtitle 1))
       (setq txt3 (dcl_TextBox_GetLine 1_form1_dwgtitle 2))
       (setq txt4 (dcl_TextBox_GetLine 1_form1_dwgtitle 3))
       (setq txtlst (strcat txt1 "\r\n" txt2 "\r\n" txt3 "\r\n" txt4))
       (dcl_Control_SetText 1_form1_dwgtitle txtlst)
       (dcl_Control_SetFocus 1_form1_custdwgnum)
     )
   )
   (princ)
 )