473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VB Help on Find Previous

Hi Everyone
I am new to this forum
I need a help in VB
i need a code for Find previous option
I am having one RichTextBox and two Command buttons [Find Previous and Find Next] in the Form
I have done the code for Find Next but not able to do for Find Previous Option
Please help me on this issue
I need to finish this work very soon
Thanks in Advance
Jul 17 '07 #1
17 2543
Hi Everyone
help me on this
Jul 18 '07 #2
hariharanmca
1,977 1GB
Hi Everyone
help me on this
Can you post, what you done in your code and explain little bit more (What you find next and previous). So that we can get what you need.
Jul 18 '07 #3
danp129
323 Expert 256MB
Similar to InStr exists:
Function InStrRev(StringCheck As String, StringMatch As String, [Start As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare]) As Long

It will search in reverse direction as the name suggests. Set the "Start" parameter to the textbox's current .SelStart.

Example (untested):

Expand|Select|Wrap|Line Numbers
  1. ret=instrrev(text1.text, "mysearchtext", text1.selstart)
  2. if ret <> 0 then 
  3.      text1.selstart=ret
  4.      text1.sellen=len("mysearchtext")
  5. else
  6.      msgbox "Not found."
  7. end if
Jul 18 '07 #4
Hi All,
This is my coding for Find Next option

Find Next:

Dim goptions, srch, str, result, start As Variant
goptions = rtfWholeWord + rtfMatchCase
srch = Trim(RichTextBox2.Text)
RichTextBox1.SelBold = False
RichTextBox1.SelColor = vbBlack
result = RichTextBox1.Find(srch, start, , goptions)
If result >= 0 Then
RichTextBox1.SelBold = True
RichTextBox1.SelColor = vbBlue
Else
str = MsgBox("Specific Region is Searched, Do you want to continue from the top", vbYesNo, "Information'")
If str = 7 Then
Exit Sub
Else
Call clear_formatting
start = 0
Exit Sub
End If
End If
start = result + Len(srch)


Is that possible to have similar kind of coding for Find Previous

Thanks in Advance
Jul 18 '07 #5
hariharanmca
1,977 1GB
Hi All,
This is my coding for Find Next option

Find Next:

Dim goptions, srch, str, result, start As Variant
goptions = rtfWholeWord + rtfMatchCase
srch = Trim(RichTextBox2.Text)
RichTextBox1.SelBold = False
RichTextBox1.SelColor = vbBlack
result = RichTextBox1.Find(srch, start, , goptions)
If result >= 0 Then
RichTextBox1.SelBold = True
RichTextBox1.SelColor = vbBlue
Else
str = MsgBox("Specific Region is Searched, Do you want to continue from the top", vbYesNo, "Information'")
If str = 7 Then
Exit Sub
Else
Call clear_formatting
start = 0
Exit Sub
End If
End If
start = result + Len(srch)


Is that possible to have similar kind of coding for Find Previous

Thanks in Advance

i think, the code posted by danap129 will help you and its short.
Jul 18 '07 #6
Hi

Will it work with RichTextBox also
Jul 18 '07 #7
hariharanmca
1,977 1GB
Hi

Will it work with RichTextBox also

let you to try somthing(Yes, it will).
Jul 18 '07 #8
Dim result, srch As Variant
srch = RichTextBox2.Text
RichTextBox1.SelBold = False
RichTextBox1.SelColor = vbBlack
result = InStrRev(RichTextBox1.Text, srch, RichTextBox1.SelStart)
If result <> 0 Then
RichTextBox1.SelStart = result
RichTextBox1.SelBold = True
RichTextBox1.SelColor = vbGreen
Else
MsgBox "Not found."
End If

I have used dnap coding but i couldn't get the result itself please let me know if i missed out anything
Thanks in Advance
Jul 18 '07 #9
hariharanmca
1,977 1GB
Dim result, srch As Variant
srch = RichTextBox2.Text
RichTextBox1.SelBold = False
RichTextBox1.SelColor = vbBlack
result = InStrRev(RichTextBox1.Text, srch, RichTextBox1.SelStart)
If result <> 0 Then
RichTextBox1.SelStart = result
RichTextBox1.SelBold = True
RichTextBox1.SelColor = vbGreen
Else
MsgBox "Not found."
End If

I have used dnap coding but i couldn't get the result itself please let me know if i missed out anything
Thanks in Advance


okay, don't confuse.

i think the below method will work

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub SearchMe()
  3. goptions = rtfWholeWord + rtfMatchCase
  4. srch = Trim(RichTextBox2.Text)
  5. RichTextBox1.SelBold = False
  6. RichTextBox1.SelColor = vbBlack
  7. result = RichTextBox1.Find(srch, start - (Len(Trim(RichTextBox2.Text)
  8. ) + 1), , goptions) 
  9. If result >= 0 Then
  10. RichTextBox1.SelBold = True
  11. RichTextBox1.SelColor = vbBlue
  12. Else
  13. str = MsgBox("Specific Region is Searched, Do you want to continue from the top", vbYesNo, "Information'")
  14. If str = 7 Then
  15. Exit Sub
  16. Else
  17. 'Call clear_formatting
  18. start = 0
  19. Exit Sub
  20. End If
  21. End If
  22. start = result - Len(srch)
  23.  
  24. End Sub
i think this will help you
Jul 18 '07 #10
danp129
323 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Sub Command1_Click()
  4.     Dim result As Long, srch As Variant
  5.     Dim lngStart As Long
  6.     srch = RichTextBox2.Text
  7.  
  8.     'RichTextBox1.SelBold = False
  9.     'RichTextBox1.SelColor = vbBlack
  10.     If RichTextBox1.SelStart = 0 Then lngStart = 1 Else lngStart = RichTextBox1.SelStart
  11.     result = InStrRev(RichTextBox1.Text, srch, lngStart, vbTextCompare)
  12.  
  13.     If result <> 0 Then
  14.         RichTextBox1.SelStart = result - 1
  15.         RichTextBox1.SelLength = Len(srch)
  16.         'RichTextBox1.SelBold = True
  17.         'RichTextBox1.SelColor = vbGreen
  18.     Else
  19.         MsgBox "Not found."
  20.     End If
  21.  
  22. End Sub
  23.  
  24. Private Sub Form_Load()
  25.     RichTextBox1.HideSelection = False
  26. End Sub
Jul 18 '07 #11
Hi dnap,
I have used ur previous coding and it is working really very fine
thanks for the help
with your previous code is there any way to find whole word because i am able to case-sensitive search but i cant do whole word
please help me on this

Find Previous Code:

Expand|Select|Wrap|Line Numbers
  1. Dim result1 As Variant
  2. RichTextBox1.SelBold = False
  3. RichTextBox1.SelColor = vbBlack
  4. If RichTextBox1.SelStart <> 0 Then
  5. result1 = InStrRev(RichTextBox1.Text, RichTextBox2.Text, RichTextBox1.SelStart, vbTextCompare) 
  6. End If
  7. If result1 <> 0 Then
  8.      result1 = result1 - 1
  9.      RichTextBox1.SelStart = result1
  10.      RichTextBox1.SelLength = Len(RichTextBox2.Text)
  11.      RichTextBox1.SelBold = True
  12.      RichTextBox1.SelColor = vbBlue
  13.      result1 = result1 + 1
  14. Else
  15.   str = MsgBox("Specific Region is Searched, Do you want to continue from the Bottom", vbYesNo, "Information'")
  16.     If str = 7 Then
  17.         Exit Sub
  18.     Else
  19.         Call clear_formatting
  20.         RichTextBox1.SelStart = Len(RichTextBox1.Text)
  21.         result1 = 1
  22.         Exit Sub
  23.     End If
  24. End If
  25. start = result1 // this is for find next option
thanks in advance
Jul 19 '07 #12
Killer42
8,435 Expert 8TB
Can you describe in detail what you mean by "finding whole word"? There might be different ways of interpreting this. Or to put it another way, can you describe (preferably showing examples) what situations you would consider to be a "whole word" match, and what you would not?
Jul 19 '07 #13
Hi
For Example i am having text like below shown in my textbox
hi
hii
If I want to find "hi" with wholeword option then
only "hi" has to select and not "hii"
Jul 19 '07 #14
hariharanmca
1,977 1GB
Hi
For Example i am having text like below shown in my textbox
hi
hii
If I want to find "hi" with wholeword option then
only "hi" has to select and not "hii"

just add space front and back of the text which you are getting it from the user.
Jul 19 '07 #15
Hi hariharanmca
ur logic is not working
is there any other way to do
Jul 19 '07 #16
hariharanmca
1,977 1GB
Hi hariharanmca
ur logic is not working
is there any other way to do

why not?

you have to search in three condition

Expand|Select|Wrap|Line Numbers
  1. No 1
  2.  srch1 = trim(RichTextBox2.Text) & " "
  3.  
  4. No 2
  5.   srch2 = " " & trim(RichTextBox2.Text) & " "
  6.  
  7. No 3
  8.    srch3 = " " & trim(RichTextBox2.Text) & "."
Jul 19 '07 #17
Killer42
8,435 Expert 8TB
I suppose you just do your normal search. Each time you find a match, you check either side of it. If both sides are either start/end of the text or a delimiter (space, comma, dot, whatever) then accept it. Otherwise, keep looking.
Jul 19 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: janice_2k | last post by:
Dear Sir/Mdm, I am writing this on behalf of my company. We bought the Visual Studio .NET Enterprise version but currently we need to use Visual Basic 6 for a small development. We are not able to...
1
by: Dishan Fernando | last post by:
Plz help How to find my previous topic?? Cheers Dishan
14
by: Stegano | last post by:
I am learning C Programming after working with Java for 5 years. I want to know where can I find the source files for C language itself. For example strcat is a function, which concatenates two...
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
7
by: Steffen Loringer | last post by:
Hi all, I'm using a linked list (double). The program is growing(windows xp task manager) if the showAllListNodes function is activated. But I can't figure out why. Any ideas??? Thanks...
5
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
0
by: gurusshetty | last post by:
Hi Is there any way to find previous value of data column or text in c#. Regards, Guru
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.