Need a help on Finding a whole word in Textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • denakaraj
    New Member
    • Jul 2007
    • 14

    Need a help on Finding a whole word in Textbox

    Hi All,
    i am new to VB.
    below is the scenario
    I am having 2 Textboxes say Text1 and Text2
    i need to find the Text2.text in the Text1 with whole word constraint
    i would like to get coding for this.
    please help me on this.

    Example Scenario:
    Text1:
    Hi
    Hi
    Hii
    Hi

    Text2:
    Hi

    when i click Find button it has to highlight the first "Hi". for the second click it has to highlight the Seconf "Hi". for third click it has to highlight the last "Hi" and not "Hii"

    Regards,
    Den
  • nev
    Contributor
    • Oct 2007
    • 251

    #2
    Hi, do you mean TextBox1 will contain the text "Hi Hi Hii Hi" ?

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      can you kindly post how you have tried to solve this.

      Comment

      • Ali Rizwan
        Banned
        Contributor
        • Aug 2007
        • 931

        #4
        This will be show the letter in text3 but i don't know how to hilight some text in a textbox.

        Code:
        Dim r As Integer, b As Integer
        
        b = Len(Text2)
        
        r = InStr(Text1.Text, Text2.Text)
        
        If r <> 0 then
        Text3.Text = Mid(Text1, r, b)
        Else
        MsgBox "Not found"
        End If
        So i m sorry for not hilighting the text but this code will search well.
        GOODLUCK
        ALI

        Comment

        • chandru8
          New Member
          • Sep 2007
          • 145

          #5
          hi
          ya Rizwan is correct

          after that, for highlighting
          Text1.SelStart = 0
          Text1.SelLength = len(text1.text)

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by chandru8
            Text1.SelStart = 0
            Text1.SelLength = len(text1.text)
            More accurately, set Text1.SelStart = r.

            Not sure whether the Instr() function will handle the "whole word" requirement, though.

            Comment

            • denakaraj
              New Member
              • Jul 2007
              • 14

              #7
              Hi All
              pls find my below coding for finding text
              but i need to find the whole word,

              VB Code:

              Dim srch As Variant
              Dim lngStart As Long
              srch = RichTextBox2.Te xt
              If RichTextBox1.Se lStart = 0 Then lngStart = 1 Else lngStart = RichTextBox1.Se lStart
              result = InStr(lngStart + result, Trim(RichTextBo x1.Text), srch, vbBinaryCompare )
              If result > 0 Then
              RichTextBox1.Se lStart = result - 1
              RichTextBox1.Se lLength = Len(srch)
              Else
              MsgBox "Not found."
              End If

              but one draw back in the code is say in the Richtextbox1 if am having "hii"
              and if i search for "hi". it will highlight the text in the Richtextbox1. but according to the constraint it has to text not found.
              help me out in this issue

              Comment

              • denakaraj
                New Member
                • Jul 2007
                • 14

                #8
                Originally posted by nev
                Hi, do you mean TextBox1 will contain the text "Hi Hi Hii Hi" ?
                ya,
                Textbox1 will contain the text "Hi Hi Hii Hi"
                Textbox 2 will contain the text "Hi"
                if i click for search it has to highlight only the text "Hi" and not "Hii"

                Comment

                • QVeen72
                  Recognized Expert Top Contributor
                  • Oct 2006
                  • 1445

                  #9
                  Hi,

                  When you want to serch for a Word, Suffix the word with a Single Space ..
                  Say Instead of searching for "Hi", Search For "Hi "..
                  One problem here would be, it may not search if, it is last word, there you can Search for "Hi."
                  So, your search word should be "Hi " or "Hi."

                  Regards
                  Veena

                  Comment

                  • denakaraj
                    New Member
                    • Jul 2007
                    • 14

                    #10
                    Hi Veena,
                    when i tried ur logic it is not working
                    cud u explain bit more or just give me pseudo code on this

                    regards,
                    Den

                    Comment

                    • QVeen72
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1445

                      #11
                      Hi,

                      in your code change this line:

                      srch = RichTextBox2.Te xt & " "

                      with this , "Hii" will not be highlighted..

                      Regards
                      Veena

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by QVeen72
                        srch = RichTextBox2.Te xt & " "
                        with this , "Hii" will not be highlighted.
                        But what if the "Hi" is at the end of the textbox?

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          denakaraj, why not keep it simple? Just use Instr() function (or simply loop through the individual characters) to find each occurence of your search string, then check the characters either side of it. If they are spaces/punctuation, or the start/end of the text, then it's a match.

                          Comment

                          • QVeen72
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1445

                            #14
                            Hi,

                            That's what I told in my previous post, you have to check for "Hi ", "Hi." and "Hi, "

                            Regards
                            Veena
                            Last edited by Killer42; Oct 16 '07, 09:39 PM.

                            Comment

                            • chandru8
                              New Member
                              • Sep 2007
                              • 145

                              #15
                              hi
                              You can use split function to compare.if possible

                              Comment

                              Working...