Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# RichTextBox selection problem

Tags:

c#

controls

I have a RichTextBox control on an application and here's my problem: when the application runs, if I start selecting with the mouse some of the characters inside a word and continue selecting outside it, the selection automatically includes the whole word in which I begun selection and any other words from which I want to select just a part, ms word-ish, if I'm not mistaken.

e.g.:

  • the text is: "Just another foobar"
  • what I want to select is: "Just ano[ther foo]bar" (the thing between the [])
  • what is actually selected: "Just [another foobar]"

The problem is just with mouse selecting, if I select text with the keyboard it works just fine. Also, the auto word select property of the control is turned off. Any idea why is that?

like image 588
cantrem Avatar asked Sep 09 '10 16:09

cantrem


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

There's a silly bug in the AutoWordSelection property implementation. The workaround is equally silly. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing RTB.

using System;
using System.Windows.Forms;

public class FixedRichTextBox : RichTextBox {
    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        if (!base.AutoWordSelection) {
            base.AutoWordSelection = true;
            base.AutoWordSelection = false;
        }
    }
}

I left an annotation at the bottom of this MSDN Library page with the details of the bug.

like image 154
Hans Passant Avatar answered Oct 13 '22 12:10

Hans Passant


Maybe things have changed since this question was answered, but I have an even simpler solution:

Just add richTextBox1.AutoWordSelection = false; to the code.

Sounds crazy, but setting this to false in the properties-box does not work. You have to do it in the code, even if the property is already false. Then it works!

like image 45
Mads Aggerholm Avatar answered Oct 13 '22 12:10

Mads Aggerholm