|
-
Mar 22nd, 2011, 02:53 PM
#1
Thread Starter
Member
Find Text: Previous
I'm making a notepad program and i want the user to be able to search for text. I got the code working for the "Find Next" button, but I cant get the code for the "Find Previous" button.
Here's what I have for Find Next:
Code:
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim x As Integer
Dim opt As RichTextBoxFinds = 0
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
x = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, PlaceHolder, opt)
If x < 0 Then
If MessageBox.Show("There are no more occurances of " & txtFind.Text & vbCrLf & "Start at begining?", "Can't Find", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
PlaceHolder = 0
btnNext.PerformClick()
End If
End If
PlaceHolder = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).SelectionStart + 1
CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()
End Sub
Here's what I have for Find Previous:
I just can't seem to make it work. I've been messing with it and trying all sorts of different options, it just keeps messing up.
Code:
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
Dim x As Integer
Dim opt As RichTextBoxFinds = 0
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
x = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, PlaceHolder, opt)
If x < 0 Then
If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
PlaceHolder = 0
btnNext.PerformClick()
End If
End If
PlaceHolder = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).SelectionStart - 1
CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()
End Sub
Any help is greatly appreciated!
~Thanks in advance
-
Mar 22nd, 2011, 03:20 PM
#2
Re: Find Text: Previous
vb Code:
Dim startAt As Integer = 'the position in your text to start from
x = RichTextBox1.Find("a", startAt, RichTextBoxFinds.Reverse)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 03:51 PM
#3
Thread Starter
Member
Re: Find Text: Previous
I already tried that, it doesn't work at all. No matter what the start position is, it always starts at the bottom. And for some reason, it doesn't move up.
-
Mar 22nd, 2011, 04:27 PM
#4
Re: Find Text: Previous
Your options are stored in a variable of type RichTextBoxFinds named opt. Let's see how it's set up in both of your methods.
Find next:
Code:
Dim opt As RichTextBoxFinds = 0
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
So in this case it does whatever 0, or 0 OR MatchCase does. You have to turn to disassembly to know what that is for sure; in this case you get 0 or 4; None or MatchCase.
Find previous:
Code:
Dim opt As RichTextBoxFinds = 0
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
Oh dear. It's the same. Perhaps you should try .paul.'s advice, or at least produce the code you have that isn't working so we can determine if there's another problem.
-
Mar 22nd, 2011, 04:28 PM
#5
Re: Find Text: Previous
try this:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim endAt As Integer = RichTextBox1.Text.Length
endAt = RichTextBox1.Find("it", 0, endAt, RichTextBoxFinds.Reverse) - 1
Do While endAt > -1
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
endAt = RichTextBox1.Find("it", 0, endAt, RichTextBoxFinds.Reverse) - 1
Loop
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 04:32 PM
#6
Re: Find Text: Previous
the problem with the startAt parameter is if you set it to 100 it'll reverse search from the end of the text to startAt. startAt needs to be 0 + the endAt parameter needs to be 1 char before the last find, then that's where it'll reverse search from...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 05:09 PM
#7
Thread Starter
Member
Re: Find Text: Previous
I still can't get it to work. All it does is highlight the last occurrence every time I hit the button. Here's the new code:
Code:
Dim x As Integer
Dim opt As RichTextBoxFinds = 0
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
x = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, RichTextBoxFinds.Reverse) - 1
If x < 0 Then
If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
PlaceHolder = 0
btnNext.PerformClick()
End If
End If
I tried copying your code and I couldn't make it work. I want it to start where the 'find next' left off and just move up. I want it to act almost exactly like the find function in windows notepad, except instead of checking 'up' or 'down' - hit next or previous.
-
Mar 22nd, 2011, 05:20 PM
#8
Re: Find Text: Previous
it highlights only the last occurrence because you aren't looping
vb Code:
Dim x As Integer
Dim opt As RichTextBoxFinds = RichTextBoxFinds.Reverse
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
Do While endAt > -1
endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
If x < 0 Then
If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
PlaceHolder = 0
btnNext.PerformClick()
End If
End If
Loop
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 05:34 PM
#9
Thread Starter
Member
Re: Find Text: Previous
But because that code loops, it reads from the bottom until it reaches the top doesn't it?
I need it to stop and highlight the occurrence before the present one. So if I had:
abc
abc
abc
abc
I hit 'find next' 3 times and the third one down is highlighted. When I hit 'find previous' I want the second one down to be highlighted. So I changed the code to this:
vb Code:
Dim x As Integer
Dim opt As RichTextBoxFinds = RichTextBoxFinds.Reverse
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
Do While endAt > PlaceHolder - 1
endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
If x < 0 Then
If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
PlaceHolder = 0
btnNext.PerformClick()
End If
End If
Loop
CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()
But now it just highlights the same one as the 'find next' button does.
Thank you by the way for all the help. I'm much closer now than I was before!
-
Mar 22nd, 2011, 06:07 PM
#10
Thread Starter
Member
Re: Find Text: Previous
Ok this code works almost the way I want it. After you hit the button once, it re-highlights the text-- then after you hit it again it moves up.
vb Code:
On Error Resume Next
Dim x As Integer
Dim opt As RichTextBoxFinds = RichTextBoxFinds.Reverse
If chkCase.Checked Then opt = opt Or RichTextBoxFinds.MatchCase
Dim endAt As Integer = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Text.Length
endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
Do While endAt > PlaceHolder - 2
endAt = CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Find(txtFind.Text, 0, endAt, opt) - 1
If x < 0 Then
If MessageBox.Show("There are no more occurrences of " & txtFind.Text & vbCrLf & "Start at begining?", "Word or Phrase Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
PlaceHolder = 0
btnNext.PerformClick()
End If
End If
Loop
PlaceHolder -= 3
CType(frmMain.TabControl.SelectedTab.Controls.Item(0), RichTextBox).Focus()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|