Displaying Multi-Line Text
by StPendl (talk) 20:42, 2 September 2020 (UTC)
Every now and then one needs to display text of multiple lines, which the user should not be able to change. Using one STATICTEXT control for each line is cumbersome, but the STATICTEXT control is a multi-line control. Many programmers are not aware that a static control has automatic text line wrapping.
The following are some ways to display multiple lines of text.
Using a Static-Text Control
This allows the user to read the text, but he cannot select and copy the contents. The text is broken into separate lines by adding a carriage-return character when a new line is desired. The carriage-return character is character 13 and is specified with the CHR$() statement as CHR$(13).
' turn main window off nomainwin ' specify text to display Message$ = "The world is turning"; CHR$(13); _ "until it gets stuck in the mud of the population." ' set up the GUI statictext #m.txt, Message$, 10, 10, 200, 100 ' open the window open "Multi-Line Message Demo" for window as #m ' trap closing the window, this line is mandatory #m "trapclose [quit]" ' wait for user action wait [quit] ' close the window and terminate close #m end
Using a Text-Box with Scroll-Bar
This allows the user to read the text and he can select and copy anything. You can display a whole plain text file in a small space.
' turn main window off nomainwin ' specify text to display filedialog "Open a Plain Text File ...", "*.txt;*.bas", FileName$ ' no file selected then terminate if FileName$ = "" then end ' read the text open FileName$ for input as #f Message$ = input$(#f, lof(#f)) close #f ' set up the GUI textbox #m.txt, 10, 10, 200, 100 ' add a scroll bar for vertical scrolling and make the control read-only ' remove automatic horizontal scrolling stylebits #m.txt, _WS_VSCROLL or _ES_READONLY, _ES_AUTOHSCROLL, 0, 0 ' open the window open "Multi-Line Message Demo" for window as #m ' trap closing the window, this line is mandatory #m "trapclose [quit]" ' fill the text box #m.txt Message$ ' wait for user action wait [quit] ' close the window and terminate close #m end