Select All in multiple RichTextBox areas simultaneously

395 Views Asked by At

How can I select all and keep the visual of the selection across multiple RichTextBox controls ?


First, simple instructions to reproduce the problem:

  • Drop two RichTextBox controls on a new blank form in a WinForms project in C#
  • Put some random text in both RichTextBox's .
  • Place the mouse cursor in one (doesn't matter which), and press CTRL+A to select all.
  • Now put the cursor in the other box, and press CTRL+A to select all.

You will notice that when you lose focus on the first RichTextBox, it will look like this nothing was selected as shown below >

enter image description here

What I would like to do, is when you select all in one box, I would like to select all in other boxes as well, and to keep the visual of selected text unless the user clicks somewhere else to cancel the select all (eg, clicking on the form should cancel any selection made), depicted as follows :

enter image description here

The default behavior is selection shows for whatever control has focus. enter image description here

Source to demonstrate the issue as follows:

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

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

        private void Form1_Load( object sender, EventArgs e ) {
            richTextBox1.Text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eu dui in tellus cursus luctus. Integer rutrum lorem nec quam faucibus ullamcorper. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ante tellus, rutrum eu consectetur non, mattis non orci. Proin dignissim odio in faucibus vulputate. Maecenas ac efficitur dui. Cras eu quam mattis, tempor neque vitae, sollicitudin urna. Nullam et ipsum a orci finibus efficitur et sed felis. Aenean nulla justo, tempor nec ligula vitae, interdum mollis dui. In purus ipsum, cursus in arcu sed, tincidunt molestie arcu. Cras suscipit felis ac auctor vehicula. Etiam dapibus convallis sapien. Integer lorem metus, consequat at urna sed, venenatis imperdiet est. Nam eleifend ac dui sit amet feugiat. Nam ut nibh nec dui molestie hendrerit.";
            richTextBox2.Text=richTextBox1.Text;
        }
    }
}
1

There are 1 best solutions below

0
On

After poking around a bit more, discovered the HideSelection property, and adjusted the above code by adding the following lines which solved the "select all" problem partially:

        richTextBox1.HideSelection = false;
        richTextBox2.HideSelection = false;
        richTextBox1.SelectAll();
        richTextBox2.SelectAll();

Could still use a hand for the inverse part of the question which is when I click inside one of the boxes, and the selection changes from all, the other box still remains selected as shown here >

enter image description here