Your Ads Here

Monday 6 February 2012

Set tabs in a RichTextBox using C#.NET


This example shows how to set tabs in a RichTextBox in C#.NET.
Keywords: RichTextBox, tabs, C#.NET, CSharp.NET.

The RichTextBox control can set different tabs for different parts of its text so this example makes some text, selects it, and then sets its tabs.
In the following code, the program uses the control's SelectAll method to select all of the text. It sets the SelectionTabs property to an array of Integers giving the tab positions. It sets AcceptsTab to True to ensure that tab characters go into the control rather than tabbing off to the next control and then it moves the selection position to the beginning of the text. 

  richTextBox1.SelectAll();
  richTextBox1.SelectionTabs = new int[] { 20, 40, 80, 120 };
  richTextBox1.AcceptsTab = true;
  richTextBox1.Select(0, 0);

Here the complete code to set tabs in RichTextBox.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testc
{

    public partial class Form1 : Form
    {
      
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.SelectionTabs = new int[]
            { 20, 40, 80, 120 };
            richTextBox1.AcceptsTab = true;
            richTextBox1.Select(0, 0);
        }
      
    }
}

1 comment: