Jump to content

Seach Within Textbox Problems


Recommended Posts

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

Link to comment
Share on other sites


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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Edited by dman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...