< character in RichTextBox causing issues

EDIT - Sorry folks, i guess i wanted to "obscure" my work code too much... i don't know why it got so many downvotes but anyway. see below for update/edit with actual code.

I am trying to insert a piece of text into an existing section of a line (<data) which resides at the beginning of a line in my RichTextBox control. However, whenever i do that in the following manner:

private void AddSelectedIntellisense(object sender, EventArgs e)
    {
        ToolStripItem x = sender as ToolStripItem;
        int cursorpos = this.txt_Body.SelectionStart;
        string final = this.txt_Body.Text.Insert(cursorpos, x.Text);
            //final var at breakpoint is equal to "<data log=\"Original\""
            //then i assign it/that to the RTB.Text
        this.txt_Body.Text = final;
            //when checked with breakpoint, this.txt_Body.Text is equal to
            //"log=\"\"<data log=\"Original\"" 
        this.txt_Body.SelectionStart = cursorpos + x.Text.Length;
    }

I am thinking that it is the < character that is causing issues when i assign the string to the .Text property (because if i replace the < with a [ in my logic, no problems), but i don't know how to fix it... if you could help me i would really appreciate it.

I also checked all of the indexes manually and they all lign up perfectly... so i don't know why the RTB.Text value is different than the string but if someone knows please tell me.

Cheers!

Answers


You are first setting:

txt = this.RTB1.Text.Substring(starts, length);

Then on the next line you are replacing the value of txt:

txt = this.RTB1.Text.Insert(index,"log='test'></data>");

You are probably looking to concatenate the strings:

string txt = this.RTB1.Text.Substring(starts, length);
txt += this.RTB1.Text.Insert(index,"log='test'></data>");
this.RTB1.Text = txt;

Ok folks... i suppose i'll give it to Aaron, since it's like somewhat related and nobody else answered.

The answer was: I am using the RTB.On_TextChanged event to fire off the intellisense based on a condition. However, because i am also setting text the RTB.Text value within the Intellisense, the condition became true twice and added the specific text twice. So i setup a flag when i add intellisense text and check it in the on_textchanged event.

Cheers and sorry for the confusion.


Need Your Help

Why might ASP.NET be putting JavaScript in HTML Comment blocks, not CDATA?

asp.net javascript validation webforms

We have an ASP.NET 2.0 WebForms app that uses MS Ajax 1.0. It's working fine on all our environments (dev, test, IE6 VMs etc.). However, at the customer site the client side validation is not happe...

Find the size in an ID using regex

php regex

I'm currently having a small problem with the following regex in PHP: