home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / instrrev / frmmain.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-09-30  |  6.6 KB  |  178 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "InStrRev"
  5.    ClientHeight    =   2085
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   6390
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    ScaleHeight     =   2085
  12.    ScaleWidth      =   6390
  13.    StartUpPosition =   3  'Windows Default
  14.    Begin VB.CommandButton cmdGetCharPos 
  15.       Caption         =   "Get Position of String2 in String1"
  16.       Height          =   495
  17.       Left            =   4920
  18.       TabIndex        =   9
  19.       Top             =   100
  20.       Width           =   1455
  21.    End
  22.    Begin VB.TextBox txtString2 
  23.       Height          =   285
  24.       Left            =   3480
  25.       TabIndex        =   5
  26.       Top             =   240
  27.       Width           =   1335
  28.    End
  29.    Begin VB.Frame frmeString2 
  30.       Caption         =   "String2"
  31.       Height          =   615
  32.       Left            =   3360
  33.       TabIndex        =   4
  34.       Top             =   0
  35.       Width           =   1575
  36.    End
  37.    Begin VB.TextBox txtString1 
  38.       Height          =   285
  39.       Left            =   840
  40.       TabIndex        =   3
  41.       Top             =   240
  42.       Width           =   2415
  43.    End
  44.    Begin VB.Frame frmString1 
  45.       Caption         =   "String1"
  46.       Height          =   615
  47.       Left            =   720
  48.       TabIndex        =   2
  49.       Top             =   0
  50.       Width           =   2655
  51.    End
  52.    Begin VB.TextBox txtStart 
  53.       Height          =   285
  54.       Left            =   120
  55.       TabIndex        =   1
  56.       Top             =   240
  57.       Width           =   495
  58.    End
  59.    Begin VB.Frame frmeStart 
  60.       Caption         =   "Start"
  61.       Height          =   615
  62.       Left            =   0
  63.       TabIndex        =   0
  64.       Top             =   0
  65.       Width           =   735
  66.    End
  67.    Begin VB.Label lblString2Info 
  68.       BackStyle       =   0  'Transparent
  69.       Caption         =   "String2 - The string you want to search for."
  70.       Height          =   255
  71.       Left            =   0
  72.       TabIndex        =   8
  73.       Top             =   1680
  74.       Width           =   6495
  75.    End
  76.    Begin VB.Label lblString1 
  77.       BackStyle       =   0  'Transparent
  78.       Caption         =   "String1 - The string that's being searched."
  79.       Height          =   255
  80.       Left            =   0
  81.       TabIndex        =   7
  82.       Top             =   1320
  83.       Width           =   6255
  84.    End
  85.    Begin VB.Label lblStartInfo 
  86.       BackStyle       =   0  'Transparent
  87.       Caption         =   $"frmMain.frx":0000
  88.       Height          =   495
  89.       Left            =   0
  90.       TabIndex        =   6
  91.       Top             =   720
  92.       Width           =   6375
  93.    End
  94. Attribute VB_Name = "frmMain"
  95. Attribute VB_GlobalNameSpace = False
  96. Attribute VB_Creatable = False
  97. Attribute VB_PredeclaredId = True
  98. Attribute VB_Exposed = False
  99. '**********************************************************
  100. '*              InStrRev by Joseph Huntley                *
  101. '*               joseph_huntley@email.com                 *
  102. '*                http://joseph.vr9.com                   *
  103. '*                                                        *
  104. '*  Made:  October 1, 1999                                *
  105. '*  Level: Beginner                                       *
  106. '**********************************************************
  107. '*   The form here are only used to demonstrate how to    *
  108. '* use the function 'InStrRev'. You may copy the          *
  109. '* functions into your project for use. If you need any   *
  110. '* help please e-mail me.                                 *
  111. '**********************************************************
  112. '* Notes: This function is built into VB6. I wrote this   *
  113. '*        function for VB4/VB5 users.                     *
  114. '**********************************************************
  115. Function InStrRev(Optional Start, Optional String1, Optional String2)
  116. '**********************************************************
  117. '*              InStrRev by Joseph Huntley                *
  118. '*               joseph_huntley@email.com                 *
  119. '*                http://joseph.vr9.com                   *
  120. '**********************************************************
  121. '*   You may use this code freely as long as credit is    *
  122. '* given to the author, and the header remains intact.    *
  123. '**********************************************************
  124. '--------------------- The Arguments -----------------------
  125. 'Start   - The character position to start at. If left blank
  126. '          the default would be the length of String1
  127. 'String1 - The string to search.
  128. 'String2 - The string to search for.
  129. '-----------------------------------------------------------
  130. 'Returns: The character position of the first character of
  131. '         the last instance of String2 before Start.
  132. 'Description: Searches a string, but in reverse.
  133. Dim lngLastPos As Long, lngPos As Long, lngStartChar As Long
  134. Dim strString As String
  135.   'check to see if String2 is missing. If yes, then
  136.   'the start argument wasn't given so automatically
  137.   'give it the value of the length of String1.
  138.   If IsMissing(String2) Then
  139.     lngStartChar& = Len(Start)
  140.     strString$ = CStr(Start)
  141.     strSearchString$ = CStr(String1)
  142.   Else
  143.     lngStartChar& = CLng(Start)
  144.     strString$ = CStr(String1)
  145.     strSearchString$ = CStr(String2)
  146.   End If
  147. 'if the string can't be found then exit
  148. If InStr(strString$, strSearchString$) = 0 Then Exit Function
  149. 'loop through the text until lngPos is bigger than Start or equal to 0.
  150. 'then return the character position prior to that.
  151.    DoEvents
  152.    lngPos& = InStr(lngLastPos& + 1, strString$, strSearchString$)
  153.    If lngPos& > lngStartChar& Or lngPos& = 0 Then Exit Do
  154.    lngLastPos& = lngPos&
  155.  Loop
  156. InStrRev = lngLastPos&
  157. End Function
  158. Private Sub cmdGetCharPos_Click()
  159.    Dim lngCharPos As Long
  160.    lngCharPos& = InStrRev(txtStart.Text, txtString1.Text, txtString2.Text)
  161.      If lngCharPos& > 0 Then 'if string2 is within string1
  162.         MsgBox "The last instance of String2 starts at character position #" & lngCharPos& & " in String1.", vbInformation
  163.      Else
  164.         MsgBox "String2 is not within String1.", vbInformation
  165.      End If
  166. End Sub
  167. Private Sub Form_Load()
  168.    'set texbox's default values
  169.    txtString1.Text = "InStrRev by Joseph Huntley"
  170.    txtString2.Text = "Joseph"
  171.    txtStart.Text = Len(txtString1.Text)
  172. End Sub
  173. Private Sub txtString1_Change()
  174.    'Set the start position to use the length of String1.
  175.    '(It's automatically default)
  176.    txtStart.Text = Len(txtString1.Text)
  177. End Sub
  178.