.Net Programming Information and User Forum
September 04, 2010, 01:31:30 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome all new members and guests.

"I really enjoy helping all the people learning .net and c#. I hope I can show them something new today", Ken Nipper
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Insert text at the carat and then move carat to desired position  (Read 49 times)
Ken
Administrator
Established Member
*****
Posts: 1022



View Profile WWW
« on: May 24, 2010, 11:02:53 am »

Someone asked me how to insert text at a certain position in a richtextbox and then put the carat in the middle of the text that is inserted.
Like when you click the bold button you get [bold][/bold] or something like that where you can use the tags to enhance a message.

OK here is one way to accomplish

Code:


using System;
using System.Windows.Forms;

namespace TextBoxInsert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonBold_Click(object sender, EventArgs e)
        {
            //when the user clicks the bold button, [Bold][/Bold] is inserted at the carat
            //you may change to more preferred [b][/b] if needed just chnage the index += 6 to index += 3;
            //and the carat is moved six spaces forward to the middle of the inserted text

            //get the carat position
            int index = richTextBox1.SelectionStart;
            //insert the [Bold][/Bold] text at the carat position
            richTextBox1.Text = richTextBox1.Text.Insert(index, "[Bold][/Bold]");
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            //move the carat 6 spaces to the right
            index += 6;
            richTextBox1.SelectionStart = index;
            //refocus to make carat blink
            richTextBox1.Focus();
        }

        private void buttonItalics_Click(object sender, EventArgs e)
        {
            //see documentation above
            int index = richTextBox1.SelectionStart;
            richTextBox1.Text = richTextBox1.Text.Insert(index, "[Italic][/Italic]");
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            index += 8;
            richTextBox1.SelectionStart = index;
            richTextBox1.Focus();
        }

        private void buttonUnderline_Click(object sender, EventArgs e)
        {
            //see documentation above
            int index = richTextBox1.SelectionStart;
            richTextBox1.Text = richTextBox1.Text.Insert(index, "[Underline][/Underline]");
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            index += 11;
            richTextBox1.SelectionStart = index;
            richTextBox1.Focus();
        }
    }
}




Ken Cool
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!