Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I am trying to make a html editor. One function that i want is that when i press a button it puts in the html tags where the text is marked so. This is the code that i got, but i can't make something out of it.

XML
private void button8_Click(object sender, EventArgs e)
{
    string strInsert = "<a>";
    textBox4.Text = textBox4.Text.Insert(textBox4.SelectionStart, strInsert);

    MessageBox.Show(textBox4.SelectedText);
}


This is what i want:
OH... this thext is marked!


that will result in this after pressing the button:

<a>OH... this thext is marked!</a>



Thank you in advance.
Posted
Updated 14-Nov-10 0:34am
v2

1 solution

Does this work for you?
private void button8_Click(object sender, EventArgs e)
{        
  string strInsertBefore = "<a>";
  string strInsertAfter = "</a>";

  string text = textBox4.Text.Substring(textBox4.SelectionStart, textBox4.SelectionLength);
  text = strInsertBefore + text + strInsertAfter;
  MessageBox.Show(text);
}
 
Share this answer
 
v2
Comments
euhiemf 14-Nov-10 6:59am    
nop... it doesn't work... its the 'textBox4.SelectionLenght' that is bugging...
JF2015 14-Nov-10 7:02am    
Sorry, edited it: There was a spelling mistake!
euhiemf 14-Nov-10 7:04am    
Awesome!!!!!
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900