Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I'm making a html-editor and I want to have a function that inputs HTML-tags before and after a marked word. This is the code:
string strInsertBefore = "<a>";
string strInsertAfter = "</a>";

string text = textBox4.Text.Substring(textBox4.SelectionStart, textBox4.SelectionLength);

text =  strInsertBefore + text + strInsertAfter;

string strInsert = text;

textBox4.Text = textBox4.Text.Insert(textBox4.SelectionStart, strInsert);


This results in <a>hello</a>hello .

but i want it to result in <a>hello</a> only.
But i can't get it to work!

Here is a code that I've tried which removes the marked word.

textBox4.Text.Remove(textBox4.SelectionStart, textBox4.SelectionLength);


Thank you!
Posted

Have you considered combining the two functions?
First, remove the word from your textbox.
Then, insert the word, surrounded by the HTML tags...
 
Share this answer
 
why don't you simply insert strInsertBefore and strInsertAfter at the right places

textBox4.Text = textBox4.Text.Insert(textBox4.SelectionStart, strInsertBefore );

textBox4.Text = textBox4.Text.Insert(textBox4.SelectionStart + textBox4.SelectionLength + strInsertBefore.Length, strInsertAfter);


thing is untested code, you will need to adjust the index for the second one as needed.
 
Share this answer
 
Comments
OriginalGriff 19-Nov-10 16:53pm    
Better solution: insert the second one first...
Yusuf 19-Nov-10 18:21pm    
Why didn't I think about that.
euhiemf 20-Nov-10 5:03am    
Thank you, but i can't get it to work! I'm in a hurry now, Cya!

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