View RSS Feed

Development Team Blog

How to break a text line in a Textbox

Rate this Entry
I received a support call from a developer that used a Textbox control in his program. The textbox wrapped on the screen when the text is too wide for the width of the control but the developer wanted to break the textline at a given location. The question was whether it is possible and how?

The text in a Textbox control can be wrapped at a given location by inserting a Character(13) in the label at the location where you want the new line to start.
Code:
Object oMyTextBox Is A TextBox
   Set Label To ("This is" + Character (13) + "a test")
End_Object
Inserting the character either requires an expression like shown above or inserting the character at a given position (see below).
Code:
Move (Left (sLabel, 4) + Character (13) + Right (sLabel, Length (sLabel) - 4)) to sLabel
This kind of coding is not really object oriented coding when needed in multiple text box objects. Therefor it is better to design a class that support the use of \n for a new line character and \t for a tab. The following code is what you want in such cases.
Code:
Use Windows.pkg

{ OverrideProperty = Auto_Size_State Visibility = Private }
{ OverrideProperty = Justification_Mode EnumList = "jMode_Left, jMode_Right, jMode_Center" }
Class cTextBox is a TextBox
    Procedure Construct_Object
        Forward Send Construct_Object
        
        Set Auto_Size_State to False
        Set Justification_Mode to JMode_Left
        Set Size to 40 100
    End_Procedure
    
    Procedure Set Label String sLabel
        Move (Replaces ("\n", sLabel, Character (13))) to sLabel
        Move (Replaces ("\t", sLabel, Character (9))) to sLabel
        
        Forward Set Label to sLabel
    End_Procedure
    
    Function Label Returns String
        String sLabel
        
        Forward Get Label to sLabel        
        Move (Replaces (Character (13), sLabel, "\n")) to sLabel
        Move (Replaces (Character (9), sLabel, "\t")) to sLabel
        
        Function_Return sLabel
    End_Function
    
    Procedure InsertNewLine Integer iCharPos
        String sLabel
        
        Get Label to sLabel
        Move (Left (sLabel, iCharPos) + Character (13) + Right (sLabel, Length (sLabel) - iCharPos)) to sLabel
        Set Label to sLabel
    End_Procedure
End_Class
The Justification_Mode property default value is changed to jMode_Left because the wrapping to a new line does not work with jMode_VCenter. The value jMode_VCenter is removed from the list of possible values.

The property Auto_Size_State is disabled because it is difficult (impossible?) to calculate the right size of the textbox object, you better size the object yourself.

You can use it with:
Code:
Use Windows.pkg
Use cHtmlHelp.pkg
Use cApplication.pkg
Use cTextBox.pkg

Object oHtmlHelp is a cHtmlHelp
End_Object

Object oApplication is a cApplication
    Set pbPreserveEnvironment to False
    Set peHelpType to htHtmlHelp
End_Object

Object oPanel is a BasicPanel
    Set Size to 200 200
    
    Object oTextBox is a cTextBox
        Set Size to 33 112
        Set Location to 10 10
        Set Label to "This is \naTe\tst"
    End_Object
    
    Object obutton is a Button
        Set Location to 95 32

        Procedure OnClick
            Send InsertNewLine of oTextBox 4
        End_Procedure
    End_Object
End_Object

Start_UI oPanel
You can choose to either use this class or merge the code inside with your own code.