' MacroName: TextArea ' MacroDescription: Use a ListBox field to view lengthy Textbox contents ' Macro created by: Rhoda Bilansky ' Macro altered by: Joel Hahn, Niles Public Library District ' Use a word-wrapping Text label to display the contents ' of a lengthy TextBox. Editing takes place in the ' single line scrollable TextBox. ' The text must contain Blank spaces, in order for word wrap to ' occur ' * * ' * Altered from Text Field to ListBox field, to allow vertical ' * scrolling, so that almost any length of text will still ' * display, rather than any text beyond the length of the Text ' * box being invisible. Also, altered display so that the ' * width of the ListBox will fit most 80-character lines. ' * * ' *** N.B. When testing for DialogControl IDs: Control names are CASE-SENSTIVE!!! ' "txtNOTES" is not the same as "txtNotes". Be consistent! option explicit const MaxNoteLength% = 500 ' if you want to set a maximum number of ' characters that a user is allowed to ' enter, alter this value. ' For this demo, 500 characters is used ' as enforced limit Dim AList$() ' This function is called continously by the Dialog Box while it is ' running. Function fnLongTextDlg(id$ , action% , suppvalue& ) static StoredNote$ ' We will be monitoring changes to the NOTES ' TextBbox. This static variable will be ' used to store its text value ' static variables retain their value ' between calls to the function dim CurrentNote$ static WarningGiven% ' Used in code section to warn user that field is ' value is too long Select Case Action ' Which Action caused call to Dialog Function ? case 1 ' Initialization ' The settings for these controls could have been done in ' the routine the defines the dialog box, ' but some Dialog functions & control values must be ' called or set during the initialization period, so ' here is an example of work performed at Initialization StoredNote$ = "Sample note. Edit as desired" DlgText "txtNotes", StoredNote$ ' Set default values ' DlgText "lblNotesContents", StoredNote$ ' update text label '============================================================== ReDim Preserve AList$(0) If AList$(0)="" Then AList$(0)=StoredNote$ DlgListBoxArray "lblNotesContents", AList$ ' update text label '============================================================== DlgText "lblNotesSize", "NOTES Length: " & str(len(StoredNote$)) ' If you want the dialog box to open with the cursor ' in the txtNotes TextBox, in edit mode, set the Focus to ' that field. DlgFocus "txtNotes" ' If the initial txtNotes text value is defined in ' the ShowNotesDialog routine, before the Dialog is called, ' the existing text will be in "select" mode, when the ' dialog box opens and focus moves to the field (similar ' to Tabbing to the field). If the user types anything, ' it will replace the existing text (unless starts with cursor ' movement or mouse click). ' With the initialization of the "txtNotes" TextBox as above, ' The initial setting of DlgFocus will not force "select" mode. ' Use whichever method yields your preferred behavior case 2 ' User clicked button or changed another dialog control ' (except text box or combo box) Select Case id$ ' Which button was clicked ? Case "btnPreview" ' In actual practice, one is not likely ' to wish to define very large areas for ' Text labels for the purpose shown in ' this sample. ' Since MsgBox's can hold more text than might be ' viewable in a the Text label as defined, ' here is a simple way to let the user ' peek at the current TextBox value in a ' word-wrapped view. ' N.B. Even MsgBoxes have their size limits. ' To overcome the latter would involve a ' completely different strategy. fnLongTextDlg = true ' Non-zero value = Do not close Dialog box MsgBox DlgText("txtNotes") ' This is the Preview in a message box dlgFocus "txtNotes" ' Return focus to Notes text box ' If you have several edit fields in your ' Dialog box, define a static variable ' to store the value of the last used ' edit-able textbox or combobox control, ' so you can return focus to the last edited ' field Case "btnOkay" ' I chose to a label to my OKAY button ' matching my notation & so test for my ID name ' sample validation routine CurrentNote$ = DlgText("txtNotes") if len(CurrentNote$) > MaxNoteLength% then MsgBox "Notes must not exceed" & str(MaxNoteLength%) _ & " characters." fnLongTextDlg = true ' do not close Dialog box end if End Select case 3 ' User edited text box or combo box contents ' case 4 ' control changed focus ' ' If you need to track changes in Control Focus, ' this is a good place to and capture and ' store values (or take action based on ' which control a user left or moved to for ' single field validations, etc. ) ' Id$ identitfies Control gaining focus ' SuppValue& identifies Control losing focus case 5 ' idle loop ' To prevent excessive screen flicker (and excessive action in the ' the idle loop), the value of the txtNotes field is stored ' in a static variable (StoredNotes$) ' During the idle loop, compare last stored value with the ' current value to see if it has changed ' When value of the txtNotes textbox changes, set the value of ' the lblNotesContents Text label = new contents of txtNotes texbox ' fnLongTextDlg = true ' Return non-zero value so idle message will ' be sent continuously ' (when Zero, message is sent when mouse moves) If DlgFocus <> "txtNotes" then exit function ' optional ' for this simple example the test for DlgFocus ' is not needed, but if you have ' a very complex DialogBox & corresponding ' .DlgFunction, you might wish to test which ' field is active and jump out quickly ' when the user is not working on the the long ' TextBox field (or not working on other fields ' that you design to trigger special actions ' during the "idle" loop) CurrentNote$ = DlgText("txtNotes") ' get current value inside ' NOTES textbox If StoredNote$ <> CurrentNote$ then ' has it changed? ' DlgText "lblNotesContents", CurrentNote$ ' update text label StoredNote$ = CurrentNote$ '============================================================== Dim szStoredNote as Integer Dim i as Integer Dim j as Integer Dim k as Integer Dim lt$ Dim rt$ ReDim Preserve AList$(0) AList$(0)=StoredNote$ szStoredNote=Len(StoredNote$) / 80 ReDim Preserve AList$(szStoredNote + 1) i=0 Do While i<=UBound(AList$) If AList$(i)="" Then j=i Do While j<=UBound(AList$) If j+1<=UBound(AList$) Then AList$(j)=AList$(j+1) Else Alist$(j)="" End if j=j+1 Loop ElseIf Len(AList$(i))>80 Then 'If i=10 Then ' AList$(i)=Left(AList$(i),80) 'Else ' If Mid(AList$(i), 80, 1)<>" " Then k=80 Do While Mid(AList$(i), k, 1)<>" " k=k-1 If k=0 Then Exit Do End If Loop If k=0 Then k=80 lt$=Left(AList$(i),k) rt$=Right(AList$(i),Len(AList$(i))-k) AList$(i)=lt$ AList$(i+1)=rt$+" "+AList$(i+1) ' End If 'End If End If i=i+1 Loop DlgListBoxArray "lblNotesContents", AList$ '============================================================== ' Update text label that displays Notes length ' Give one warning if user has just exceeded maxlength you permit DlgText "lblNotesSize", "NOTES Length: " & str(len(CurrentNote$)) If len(CurrentNote) < MaxNoteLength% then WarningGiven = false else ' Give one message box warning, but let user continue to ' add to the field, as user might need to add or paste extra ' text before removing excess text. Don't nag. ' btnOkay performs final validation of user entry If WarningGiven <> true then beep MsgBox "Notes field should not exceed" & str(MaxNoteLength%) _ & " characters" WarningGiven = true end if end if end if End Select End Function sub ShowNotesDialog dim response% dim DashLine$ DashLine$ = string(85,"=") Begin Dialog LongTextDialog 21, 17, 309, 170, "Sample echo of long TextBox values in word-wrapping Text labels", .fnLongTextDlg OkButton 157, 151, 50, 14, .btnOkay CancelButton 211, 151, 50, 14 Text 10, 10, 27, 12, "NOTES", .lblNotes TextBox 45, 7, 256, 18, .txtNotes ListBox 9, 47, 292, 97, AList, .lblNotesContents PushButton 103, 151, 50, 14, "Preview", .btnPreview Text 8, 142, 292, 9, DashLine, .lblDashLineBottom Text 9, 30, 292, 12, DashLine, .lblDashLineTop Text 9, 151, 89, 14, "-", .lblNotesSize End Dialog Dim dlgLongText as LongTextDialog response = Dialog(dlgLongText) Select Case response% Case 0 ' Cancel button pressed MsgBox "You selected cancel" Case -1 ' Okay button clicked MsgBox "The new Note value is :" & dlgLongText.txtNotes Case Else ' if you add other controls to the dialog box ' that provide other ways to exit the DialogBox ' add more Case # statements to handle appropriate ' reaction MsgBox "You clicked control button #" & str(response%) end Select End Sub sub main ShowNotesDialog end sub