[RESOLVED] Determining the End of line....
I have a code here that selects a particular string inside a richtextbox
Dim a As String
Dim b As String
Dim c As String
a = TextBox1.Text
b = InStr(TextBox2.Text, a)
If b Then
TextBox2.SelectionStart = b - 1
TextBox2.SelectionLength = Len(a)
c = textbox2.SelectedText
Else
MsgBox("No text Found!")
End If
My question is how can I tweak it into selecting the whole line of the rich text box?
For instance I have 3 lines
This is my first line
Second one is much longer
The Third one should be the longest
then I searched the word "second", how can I select the whole second line? Because if you use the code above it only gets the word "second" but not the whole line
Thanks in Advance and more powers!
EDIT
By the way, I tried doing TextBox2.SelectionLength = Len(a) + number however of course that works only if you know the exact number of characters, and that wouldnt work since the text generated can be random
Re: Determining the End of line....
try this:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.Text.IndexOf(TextBox1.Text))
RichTextBox1.SelectionStart = RichTextBox1.GetFirstCharIndexFromLine(line)
RichTextBox1.SelectionLength = RichTextBox1.Lines(line).Length
RichTextBox1.Focus()
End Sub
End Class
Re: Determining the End of line....
Thanks for the reply. Tried it however using the
This is my first line
Second one is much longer
The Third one should be the longest
If I search "second" it highlights the "The Third one should be the longest"
EDIT: Weird (refreshed it) by creating a brand new project now it works perfectly
It works but here's what I tried using different words
For instance:
Chat ID: 00000000000000000
First Name: Zaborg
Last Name: Mobius
MSISDN: 897-789-1111
When I try to search "MSISDN" it doesn't select the line at all. ? Any reason why? Thanks though your code works perfectly
Another weird Outcome:
Now whenever I try to search MSISDN, I receive an error: Index was outside the bounds of the array.
1 Attachment(s)
Re: Determining the End of line....
it works for me. here's my test app:
Attachment 99723
Re: Determining the End of line....
Thanks! It's working yes, I dont know if it make sense however when I try to use different sets of sentences the outcome is different. It's seems like the result differs on the set of sentences you place on the rich text box.
Is there any code you know that I can get the line number of the selected text instead. Getting it would be much simple for this situation I think..
For instance if I search "second" msgbox will appear telling that it's line number 2.
Re: Determining the End of line....
instead of:
Code:
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.Text.IndexOf(TextBox1.Text))
use:
Code:
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Re: Determining the End of line....
Mmkay..
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
RichTextBox1.SelectionStart = RichTextBox1.GetFirstCharIndexFromLine(line)
RichTextBox1.SelectionLength = RichTextBox1.Lines(line).Length
RichTextBox1.Focus()
End Sub
Tried 3 examples:
1.Chat ID: 00000000000000000
First Name: Zaborg
Last Name: Mobius
MSISDN: 897-789-1111
Worked pefectly
2. This is my first line
Second one is much longer
The Third one should be the longest
Without editing your program(not changing the default text of the rich textbox) it highlights the first line
Cutting and pasting the same sentence the result differs (third sentence is being highlighted).
3. Very Long List
Error: Index was outside the bounds of the array.
Re: Determining the End of line....
I can't recreate your problem.
i'd need to see this happening to fix it
Re: Determining the End of line....
I used your program and replaced the code to
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
RichTextBox1.SelectionStart = RichTextBox1.GetFirstCharIndexFromLine(line)
RichTextBox1.SelectionLength = RichTextBox1.Lines(line).Length
RichTextBox1.Focus()
End Sub
then I entered
Chat ID: 00000000000000000
First Name: Zaborg
Last Name: Mobius
MSISDN: 897-789-1111
in the richtextbox1 then entered "Zaborg" in the textbox after running the program and then pressed button1, you will see that the highlighted text is the MSISDN
Re: Determining the End of line....
ooops!! It looks like disabling the richtextbox word wrap's property took care the issue!!
I understand now why is the issue kept on happening! Anyway, thanks paul for the great code you have given today! Ur the best. :)