[SOLVED] [C#]Add Text Around Text

x BlueRobot

Administrator
Staff member
Joined
May 7, 2013
Posts
10,400
The button simply adds to the text box.

Code:
private void Bold_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText("");
        }

I was thinking of adding this:

Code:
private void Bold_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectedText != "")
            {

                richTextBox1.AppendText(""); <-- My question is for this bit
            }

            else
            {
                richTextBox1.AppendText("");
            }

        }

If the user has selected some text, then I would like the program to wrap the selected text with the two B's.

Thanks for any help :)
 
Do you think it may be possible to overload that AppendText() method with more than one argument? For example, would it work if I stored the SelectedText in a string and then added two strings around the variable?

Code:
AppendText("{B}" + string_variable +"{/B}")
 
Haha, what you're trying to do here, I've done in a few applications already, so I have a good idea of what you can do. To mention, since you are using a RichTextBox control, you can actually bold the text too. There is one application I wrote for managing a VB.net school, that allows me to manage templates in a 'rich-text' mode, and 'source-mode' with BBcode visible, and no formatting.

As for wrapping the text in BBcode tags. This is a simple task. :cool3:

Do you think it may be possible to overload that AppendText() method with more than one argument?

Overload? What do you mean? And with more than one argument... What kind of other arguments? You don't need to be doing that... The SelectedText is a Get or Set property. That string is already stored somewhere (in the backing value for that property). Try selecting some text in that control, and having some code that changes that SelectedText to a new string:

Code:
[PLAIN]richTextBox1.SelectedText = "$$$"[/PLAIN]

What you want is something like this:
Code:
[PLAIN]richTextBox1.SelectedText = string.Format("[b]{0}[/b]", richTextBox1.SelectedText)[/PLAIN]

Note: I wrote that in C#, but to get it to VB.net, all I had to do here was remove the ";" at the end in this case lol.

Another way to check the selection is:
Code:
richTextBox1.SelectedText.Length > 0

And instead of "", remember you also have string.Empty :)
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top