RichTextBox.SelectionLength : RichTextBox « System.Windows.Forms « C# / C Sharp by API






RichTextBox.SelectionLength

  

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

public class RichTextBoxes : Form
{
  RichTextBox rtxt;

  public RichTextBoxes()
  {
    Size = new Size(400, 500);
               
    rtxt = new RichTextBox();
    rtxt.Parent = this;
    rtxt.Text = "Enter text here.";
    rtxt.Multiline = true;
    rtxt.BorderStyle = BorderStyle.Fixed3D;
    rtxt.ScrollBars = RichTextBoxScrollBars.ForcedBoth;  
    rtxt.Dock = DockStyle.Fill;
    rtxt.DetectUrls = true;       
    rtxt.AutoWordSelection = true;  
    rtxt.BulletIndent = 10;
    rtxt.ShowSelectionMargin = true;

    MenuItem mnuImport = new MenuItem("&Import",new EventHandler(mnuImport_Click));
    MenuItem mnuFile = new MenuItem("&File",new MenuItem[] {mnuImport});

    MenuItem mnuDash1 = new MenuItem("-");
    MenuItem mnuDash2 = new MenuItem("-");
    MenuItem mnuUndo = new MenuItem("&Undo",new EventHandler(mnuUndo_Click),Shortcut.CtrlZ);
    MenuItem mnuCut = new MenuItem("Cu&t",  new EventHandler(mnuCut_Click),  Shortcut.CtrlX);
    MenuItem mnuCopy = new MenuItem("&Copy",new EventHandler(mnuCopy_Click),Shortcut.CtrlC);
    MenuItem mnuCopyRtf = new MenuItem("Copy &Rtf",  new EventHandler(mnuCopyRtf_Click));
    MenuItem mnuPaste = new MenuItem("&Paste",new EventHandler(mnuPaste_Click),Shortcut.CtrlV);
    MenuItem mnuDelete = new MenuItem("&Delete",new EventHandler(mnuDelete_Click));
    MenuItem mnuSelectAll = new MenuItem("Select &All",  new EventHandler(mnuSelectAll_Click),Shortcut.CtrlA);
    MenuItem mnuSelect5 = new MenuItem("Select First &5",new EventHandler(mnuSelect5_Click),Shortcut.Ctrl5);
    MenuItem mnuClear = new MenuItem("Clea&r",new EventHandler(mnuClear_Click));
    MenuItem mnuEdit = new MenuItem("&Edit",new MenuItem[] {mnuUndo, mnuDash1, 
                    mnuCut, mnuCopy, mnuCopyRtf, mnuPaste, 
                    mnuDelete, mnuDash2, mnuSelectAll, 
                    mnuSelect5, mnuClear});

    MenuItem mnuScrollToCaret = new MenuItem("&Scroll to Caret",new EventHandler(mnuScrollToCaret_Click));
    MenuItem mnuView = new MenuItem("&View",new MenuItem[] {mnuScrollToCaret});
    
    MenuItem mnuAlignLeft = new MenuItem("Align&Left",new EventHandler(mnuAlignLeft_Click));
    MenuItem mnuAlignRight = new MenuItem("Align&Right",new EventHandler(mnuAlignRight_Click));
    MenuItem mnuAlignCenter = new MenuItem("Align&Center",new EventHandler(mnuAlignCenter_Click));

    MenuItem mnuBullet = new MenuItem("&Bullet",new EventHandler(mnuBullet_Click));
    MenuItem mnuAlign = new MenuItem("&Align",new MenuItem[] {mnuAlignLeft, mnuAlignRight, mnuAlignCenter});
    MenuItem mnuRed = new MenuItem("&Red",new EventHandler(mnuRed_Click));
    MenuItem mnuBold = new MenuItem("Bo&ld",new EventHandler(mnuBold_Click));
    MenuItem mnuHang = new MenuItem("&Hanging Indent",new EventHandler(mnuHang_Click));
    MenuItem mnuIndent = new MenuItem("&Indent",new EventHandler(mnuIndent_Click));
    MenuItem mnuRightIndent = new MenuItem("&Right Indent",  new EventHandler(mnuRightIndent_Click));
    MenuItem mnuFormat = new MenuItem("For&mat",new MenuItem[] {mnuBullet, mnuAlign, 
                    mnuRed, mnuBold, mnuHang, mnuIndent, 
                    mnuRightIndent});

    Menu = new MainMenu(new MenuItem[] {mnuFile, mnuEdit, 
                    mnuView, mnuFormat});

  }
  static void Main() 
  {
    Application.Run(new RichTextBoxes());
  }

  private void mnuImport_Click(object sender, EventArgs e)
  {
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.InitialDirectory = @"c:\";
    ofd.Filter = "RTF files (*.rtf)|*.rtf|" +
                 "All files (*.*)|*.*";
    ofd.FilterIndex = 1;              //  1 based index
    
    if (ofd.ShowDialog() == DialogResult.OK)
    {
      try
      {
        StreamReader reader = new StreamReader(ofd.FileName);
        rtxt.Rtf = reader.ReadToEnd();
        reader.Close();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
        return;
      }
    }
  }

  private void mnuUndo_Click(object sender, EventArgs e)
  {
    if (rtxt.CanUndo == true)
    {
      rtxt.Undo();
      rtxt.ClearUndo();
    }
  }

  private void mnuCut_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectedText != "")
      rtxt.Cut();
  }

  private void mnuCopy_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionLength > 0)
      rtxt.Copy();
  }

  private void mnuCopyRtf_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionLength > 0)
    {
      Clipboard.SetDataObject(rtxt.SelectedRtf);
    }
  }

  private void mnuPaste_Click(object sender, EventArgs e)
  {
    if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
    {
      if (rtxt.CanUndo == true)
      {
        if (rtxt.SelectionLength > 0)
        {
          if (MessageBox.Show(
            "Do you want to overwrite the currently selected text?", 
            "Cut & Paste", MessageBoxButtons.YesNo) == DialogResult.No)
            rtxt.SelectionStart = rtxt.SelectionStart + 
                                  rtxt.SelectionLength;
        }
        rtxt.Paste();
      }
    }
  }

  private void mnuDelete_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionLength > 0)
      rtxt.SelectedText = "";
  }

  private void mnuClear_Click(object sender, EventArgs e)
  {
    rtxt.Clear();
  }


  private void mnuSelect5_Click(object sender, EventArgs e)
  {
    if (rtxt.Text.Length >= 5)
    {
      rtxt.Select(0,5);
    }
    else
    {
      rtxt.Select(0,rtxt.Text.Length);
    }
  }

  private void mnuSelectAll_Click(object sender, EventArgs e)
  {
    rtxt.SelectAll();
  }

  private void mnuScrollToCaret_Click(object sender, EventArgs e)
  {
    rtxt.ScrollToCaret();
  }

  private void mnuBullet_Click(object sender, EventArgs e)
  {
    rtxt.SelectionBullet = !rtxt.SelectionBullet;
  }

  private void mnuAlignLeft_Click(object sender, EventArgs e)
  {
    rtxt.SelectionAlignment = HorizontalAlignment.Left;
  }

  private void mnuAlignRight_Click(object sender, EventArgs e)
  {
    rtxt.SelectionAlignment = HorizontalAlignment.Right;
  }

  private void mnuAlignCenter_Click(object sender, EventArgs e)
  {
    rtxt.SelectionAlignment = HorizontalAlignment.Center;
  }

  private void mnuRed_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionColor == Color.Red)
      rtxt.SelectionColor = Color.Black;
    else
      rtxt.SelectionColor = Color.Red;
  }

  private void mnuBold_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionFont.Bold )
      rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Regular);
    else
      rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Bold);
  }

  private void mnuHang_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionHangingIndent == 10 )
      rtxt.SelectionHangingIndent = 0;
    else
      rtxt.SelectionHangingIndent = 10;
  }

  private void mnuIndent_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionIndent == 10 )
      rtxt.SelectionIndent = 0;
    else
      rtxt.SelectionIndent = 10;
  }

  private void mnuRightIndent_Click(object sender, EventArgs e)
  {
    if (rtxt.SelectionRightIndent == 50 )
      rtxt.SelectionRightIndent = 0;
    else
      rtxt.SelectionRightIndent = 50;
  }
}

   
    
  








Related examples in the same category

1.RichTextBox.Focus()
2.RichTextBox.Multiline
3.RichTextBox.SelectionChanged
4.RichTextBox.SelectionFont