MSFN Forum: Seach Within Textbox Problems - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Seach Within Textbox Problems Visual Basic 2005 Beta 2 Rate Topic: -----

#1 User is offline   Cyber Axe 

  • Geek
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 13-December 03

Posted 07 August 2005 - 11:24 PM

My code is shown below the problems i am having are...

i search when the caret is at the startof the text box in teh first line at the start of hte line then [lengthPosition = InStr(varPosition, frmNotepad.txtNotepad.Text, Value, CaseSensative) 'Find Value After Caret varPosition] comes back in error it just highlights the line in yellow and doesnt say what the error is

the other problem i am having is when searching upwards the loops back around and starts form the bottom upwards and i want to stop this form happening

anyone got any suggestions

   Public Sub FindNext(ByVal Value, ByVal Direction, ByVal CaseSensative)
        If Value IsNot Nothing Then ' Only run if Search String is not Null
            ' Set The CaseSensative to the appropriate Search Method
            If CaseSensative = False Then
                CaseSensative = CompareMethod.Text
            Else
                CaseSensative = CompareMethod.Binary
            End If

            ' Set Search Start Location Based on Selection Length
            Dim varPosition As Integer = frmNotepad.txtNotepad.SelectionStart + frmNotepad.txtNotepad.SelectionLength

            ' Search for the Text In the specified Direction from the text cursor
            Dim lengthPosition As Long ' Declare Search Result Variable

            If Direction = "Down" Then
                lengthPosition = InStr(varPosition, frmNotepad.txtNotepad.Text, Value, CaseSensative) 'Find Value After Caret varPosition
            Else
                Dim varText As String = ReverseString(frmNotepad.txtNotepad.Text) 'Set varText to txtNotepad Contents
                Dim varTextLength As Integer = Len(varText) ' Get the Length of varText

                If varPosition > 0 Then ' Attempt to Stop the Search Looping when at Start of Document
                    lengthPosition = varTextLength - InStr(varTextLength - frmNotepad.txtNotepad.SelectionStart, varText, ReverseString(Value), CaseSensative) - 2 'Reverse Everything to Find Value Before Caret varPosition
                End If
            End If

            ' Select Searched Text
            If lengthPosition > 0 Then
                frmNotepad.txtNotepad.SelectionStart = lengthPosition - 1 ' Move Caret to Start of Text
                frmNotepad.txtNotepad.SelectionLength = Len(Value) ' Set Length of Selection to the Search String
            Else
                MsgBox("Cannot find " + Chr(34) + varFind + Chr(34), MsgBoxStyle.Information) ' Display Message if no more Instances of Search String in Specified Direction or None at all
            End If
            CaretRefresh(frmNotepad.txtNotepad) ' Scroll txtNotepad to Caret Position
            SetCharacterPosition() ' Update Line / Character in Status Bar
        End If
    End Sub



#2 User is offline   dman 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 717
  • Joined: 01-February 05

Posted 08 August 2005 - 03:29 PM

Hi Cyber,
Sent you a PM a week or so ago asking how this was going. Hopefully you just overlooked it and didn't blow me off.
If you would like me to try to debug, zip your code and mail to dettest@comcast.net. Can't really tell what is going on from the snippet you posted because referenced functions are not included.
dman

#3 User is offline   dman 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 717
  • Joined: 01-February 05

Posted 08 August 2005 - 09:17 PM

Your editor looks just as you described you would make it... clean, simple and functional. I like it.
Problem #1. Cursor position starts at 0 but instr() function would consider that place in string to be 1. Just add 1 to varPosition in this function and should work OK
If Direction = "Down" Then
    lengthPosition = InStr(varPosition + 1, frmNotepad.txtNotepad.Text, Value, CaseSensative) 'Find Value After Caret varPosition
Else
   .....


I try to take a look at looparound problem tomorrow.
Cya

#4 User is offline   dman 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 717
  • Joined: 01-February 05

Posted 10 August 2005 - 10:15 PM

Cyber,
Your "reversestring" function is clever, but I think not necessary. Try the InStrRev function, it was made to search backwards. try this.....
Public Sub FindNext(ByVal Value, ByVal Direction, ByVal CaseSensative)
        If Value IsNot Nothing Then ' Only run if Search String is not Null
            ' Set The CaseSensative to the appropriate Search Method
            If CaseSensative = False Then
                CaseSensative = CompareMethod.Text
            Else
                CaseSensative = CompareMethod.Binary
            End If

            ' Set Search Start Location Based on Selection Length
            Dim varPosition As Integer = frmNotepad.txtNotepad.SelectionStart + frmNotepad.txtNotepad.SelectionLength

            ' Search for the Text In the specified Direction from the text cursor
            Dim lengthPosition As Long ' Declare Search Result Variable

            If Direction = "Down" Then
                lengthPosition = InStr(varPosition + 1, frmNotepad.txtNotepad.Text, Value, CaseSensative) 'Find Value After Caret varPosition
            Else
                If frmNotepad.txtNotepad.SelectionStart > 0 Then
                    lengthPosition = InStrRev(frmNotepad.txtNotepad.Text, Value, frmNotepad.txtNotepad.SelectionStart, CaseSensative) 'Find Value After Caret varPosition
                End If
            End If

            ' Select Searched Text
            If lengthPosition > 0 Then
                frmNotepad.txtNotepad.SelectionStart = lengthPosition - 1 ' Move Caret to Start of Text
                frmNotepad.txtNotepad.SelectionLength = Len(Value) ' Set Length of Selection to the Search String
            Else
                MsgBox("Cannot find " + Chr(34) + varFind + Chr(34), MsgBoxStyle.Information) ' Display Message if no more Instances of Search String in Specified Direction or None at all
            End If
            CaretRefresh(frmNotepad.txtNotepad) ' Scroll txtNotepad to Caret Position
            SetCharacterPosition() ' Update Line / Character in Status Bar
        End If
    End Sub


#5 User is offline   Cyber Axe 

  • Geek
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 13-December 03

Posted 11 August 2005 - 09:35 AM

thanks dman both work perfectly and thats yet another thing checked off my task list shouldnt be too long before i can release it (which will be when the tasks are finished)

i'll be sure to add you to the credits for all the help you've given me

though i am having another problem whenever i use the split and join method you gave me for the Sort Alphabetically script for other things (the remove white space and insert) it seems to double the amount of CRLF's and so doubles the amount of lines you cant see it by going to the bottom line and using that to get the current line but its viewable from the insert box the amount of lines increase afterwords

the only way that seems to get around this is to do a loop and trim the end of each line in the string array however this (if i remember correctly) remove all trailing spaces and such too which i dont want to remove in some cases

do you know of any alternative way of getting around this?

thanks

#6 User is offline   dman 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 717
  • Joined: 01-February 05

Posted 11 August 2005 - 11:04 AM

Cool. I would be proud to be associated with your project. :thumbup
I see what you mean about the added crlf's. Tricky since like you said sometimes you don't want to remove whitespace.
Maybe instead of trimming the lines in the array you could use left() function to return all but last two chars (the cr & lf)?

#7 User is offline   Cyber Axe 

  • Geek
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 13-December 03

Posted 14 August 2005 - 11:58 AM

tried using the left() funcion doesnt work i also tried manually joining the array into a string by putting it through a loop and adding each line to the array but same result as string.join argh

#8 User is offline   dman 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 717
  • Joined: 01-February 05

Posted 14 August 2005 - 12:58 PM

I will try to take a look. If you are doing the join manually, the chars must be getting added during either split or sort. Lets first try just split and rejoin with no sort and see if crlf is added. If still bad must be in split. Either we are not using it split function correctly or else need to read textbox lines another way.

#9 User is offline   Cyber Axe 

  • Geek
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 13-December 03

Posted 14 August 2005 - 03:59 PM

the function i am testing doesnt sort it so it must be the split

#10 User is offline   dman 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 717
  • Joined: 01-February 05

Posted 14 August 2005 - 05:08 PM

Something is happening because we are splitting on newline, and some lines only contain newline. havent tracked it down exactly.
I tried replacing the newline with a extended ASCII char and did the split on that... seems to work but seems like there should be better way.
 Public Sub Alphabetize(ByVal Value As TextBox)
        frmNotepad.txtNotepad.HideSelection = True
        Dim str1 As String = Value.Text.ToString()
        Dim str2 As System.Text.StringBuilder = New System.Text.StringBuilder
        str2.Append(str1)
        str2.Replace(vbCrLf, Chr(234))
        Dim sArray As String() = str2.ToString.Split(Chr(234))
        Array.Sort(sArray)
        str1 = Join(sArray, vbCrLf)
        frmNotepad.txtNotepad.Text = str1 'Convert Array Back to String and Refill Textbox
        frmNotepad.txtNotepad.HideSelection = False
        CaretRefresh(frmNotepad.txtNotepad) ' Scroll txtNotepad to Caret Position
        SetCharacterPosition() ' Update Line / Character in Status Bar
        SetNoSelection() ' Remove Selection
    End Sub


#11 User is offline   Cyber Axe 

  • Geek
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 13-December 03

Posted 14 August 2005 - 06:25 PM

thanks, that worked though i changed chr(234) to vbCr

though i no longer need to use that method after playing aorund with that one i had the idea to google for multiple delimiters and came across the MSDN example page for the split function

i changed it to
Dim sArray As String() = Split(TextBox.Text, vbCrLf, -1, CompareMethod.Text)


and it seems to work perfectly

#12 User is offline   dman 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 717
  • Joined: 01-February 05

Posted 14 August 2005 - 06:34 PM

nice!
That's what I meant when I said seems like should be something better.

Now clear was splitting on chr(13) "CR" only, and chr(10) "LF" was being left over and appearing at start of new line. Learn something every day.

This post has been edited by dman: 14 August 2005 - 06:55 PM


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy