Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How do i parse a text from between two strings in a richTextBox control ?

$
0
0

I have some text in a richTextBox.

Then i have a richTextBox1 MouseUp event:

private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (isFirstClick == false)
            {
                textBox2.Text = richTextBox1.SelectedText;
                isFirstClick = true;
            }
            else
            {
                textBox3.Text = richTextBox1.SelectedText;
                isFirstClick = false;
                endPosition = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
                result = ExtractFromStrings(this.richTextBox1.Text, textBox2.Text, textBox3.Text);
            }
        }

First time i drag the mouse mark/highlight part of a text somewhere in the richTextBox window and it will add this text to textBox2.

The second time i drag the mouse and mark/highlight second part of the text it will add it to textBox3.

And then using the method ExtractFromStrings it will parse the text between the two strings in textBox2 and textBox3.

private static string ExtractFromStrings(string text, string startString, string endString)
        {
            string matched= "";
            int index = 0;
            int index1 = 0;

            while (true)
            {
                if (index == -1)
                {
                    break;
                }
                index = text.IndexOf(startString);
                index1 = text.IndexOf(endString, index);
                matched = text.Substring(index + startString.Length,
                                         index1 - index - startString.Length);
                index = text.IndexOf(startString, index1 + 1);
            }

            return matched;
        }

The reason i' m using while at leath i think i need while is since i want to find over all the text in the richTextBox for the text i will parse between textBox2 text and textBox3 text.

For example in richTextBox i have this line: New> Android > Android Application

And using the mouse i marked first time And(of the second Android word) and second time i marked lication(out of Application).

So in the variable result i should see: roid App

And if i marked first New> and second time marked roid(of the first Android word) so i should see in variable result:  And

And another case if i marked first time: New> but this time in second time i marked the roid of the second Android word so in result i should see:  Android > And

Today now as the code is when i mark the second time it stuck in the while and the whoe program freeze.

What it should do is to parse the text between the strings and then search to see if the parsed text exist in other places in the whole text in richTextBox1 and extract this text from all the other places.

And i wanted to use first indexof and substring to see if it's possible to do it with that.

And later to see other options maybe regex or others. But first i would like to use the indexof and substring.

The problem is that i messed up the whole ExtractFromStrings method.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>