home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / wordwrap / wordwrap.frm (.txt) next >
Encoding:
Visual Basic Form  |  1995-07-17  |  3.4 KB  |  106 lines

  1. VERSION 2.00
  2. Begin Form frmWrap 
  3.    Caption         =   "Carl Franklin's WordWrap Function"
  4.    ClientHeight    =   2790
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1485
  7.    ClientWidth     =   7365
  8.    Height          =   3195
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   2790
  12.    ScaleWidth      =   7365
  13.    Top             =   1140
  14.    Width           =   7485
  15.    Begin TextBox Text1 
  16.       Height          =   2460
  17.       Left            =   135
  18.       MultiLine       =   -1  'True
  19.       TabIndex        =   1
  20.       Top             =   150
  21.       Width           =   4785
  22.    End
  23.    Begin CommandButton Command1 
  24.       Caption         =   "Save"
  25.       Height          =   495
  26.       Left            =   5175
  27.       TabIndex        =   0
  28.       Top             =   135
  29.       Width           =   1215
  30.    End
  31.    Begin Label Label1 
  32.       Caption         =   "This function will save text in the textbox to a file called ""MYFILE.TXT"" in the directory that the program is being run from.  Probably VB in this case.  See the ""CMDIALOG"" sample code for use with printing."
  33.       FontBold        =   0   'False
  34.       FontItalic      =   0   'False
  35.       FontName        =   "MS Sans Serif"
  36.       FontSize        =   8.25
  37.       FontStrikethru  =   0   'False
  38.       FontUnderline   =   0   'False
  39.       Height          =   1770
  40.       Left            =   5130
  41.       TabIndex        =   2
  42.       Top             =   750
  43.       Width           =   2145
  44.       WordWrap        =   -1  'True
  45.    End
  46. Sub Command1_Click ()
  47. 'To use this function with a text box, make sure the
  48. 'text box's Multiline property to True, and use the
  49. 'following code:
  50.     '-- VB functions that take strings as parameters
  51.     'will not accept a Text property.
  52.     T$ = Text1.Text
  53.     '-- Get the formatted text (45 characters wide)
  54.     Wrapped$ = WordWrap$(T$, 45)
  55.     '-- Save the text to a file.
  56.     Open "MYFILE.TXT" For Output As 1
  57.     Print #1, Wrapped$
  58.     Close #1
  59. End Sub
  60. Function WordWrap$ (St$, Length)
  61. '-- This function converts raw text into CRLF delimited lines.
  62.     Length = Length + 1
  63.     St$ = Trim$(St$)
  64.     Cr$ = Chr$(13)
  65.     Crlf$ = Chr$(13) & Chr$(10)
  66.     Do
  67.         L = Len(NextLine$)
  68.         S = InStr(St$, " ")
  69.         C = InStr(St$, Cr$)
  70.         If C Then
  71.             If L + C <= Length Then
  72.                 Text$ = Text$ & NextLine$ & Left$(St$, C)
  73.                 NextLine$ = ""
  74.                 St$ = Mid$(St$, C + 1)
  75.                 GoTo LoopHere
  76.             End If
  77.         End If
  78.         If S Then
  79.             If L + S <= Length Then
  80.                 DoneOnce = True
  81.                 NextLine$ = NextLine$ & Left$(St$, S)
  82.                 St$ = Mid$(St$, S + 1)
  83.             ElseIf S > Length Then
  84.                 Text$ = Text$ & Crlf$ & Left$(St$, Length)
  85.                 St$ = Mid$(St$, Length + 1)
  86.             Else
  87.                 Text$ = Text$ & NextLine$ & Crlf$
  88.                 NextLine$ = ""
  89.             End If
  90.         Else
  91.             If L Then
  92.                 If L + Len(St$) > Length Then
  93.                     Text$ = Text$ & NextLine$ & Crlf$ & St$ & Crlf$
  94.                 Else
  95.                     Text$ = Text$ & NextLine$ & St$ & Crlf$
  96.                 End If
  97.             Else
  98.                 Text$ = Text$ & St$ & Crlf$
  99.             End If
  100.             Exit Do
  101.         End If
  102. LoopHere:
  103.     Loop
  104.     WordWrap$ = Text$
  105. End Function
  106.