Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FastColoredTextbox AutoWordSelection?

FastColoredTextbox is an user-control that can be downloaded in this url, it looks like this:

enter image description here

Its an amazing control but only can select one word when doubleclicking on the text, can't hold the mouse to select more words, so it only selects the entire current word over the mouse pointer even if you try to move the mouse cursor to left or right to select more text.

I have not found any information explaining the problem, and all of the official example projects has this problem.

Nobody means how to make an AutoWordSelection equivalent of a default TextBox for a FastcoloredTextbox control, but even the most important thing is:

How to select just more than one word with the mouse?

UPDATE:

@mostruash answer is very instructive but in all this time I could not carry out the modifications by myself.

I need a huge help from a C# programmer to carry out this task, my knowledge of C# is very low and the modifications that I made to the source did not work (don't compiled), I went back to the original user-control source to not end up spoiling more. I hate to say this but this time I need the job done, this source is too much for me.

If I'm requesting for too much then maybe with the necesary extended instructions of a C# developer, explaining how to accomplish this step by step, maybe I could carry it out by myself.

UPDATE

A video that demostrates the problem:

https://www.youtube.com/watch?v=Cs2Sh2tMvII

UPDATE

Another demo, I show what the FastColoredTextBox can't do but I would like to do like every other text-editor can do:

enter image description here

like image 491
ElektroStudios Avatar asked Jan 02 '14 19:01

ElektroStudios


3 Answers

I've checked the source code of the project. Dragging is cancelled if a double click occurs and SelectWord is called.

You could modify the source code to include the feature that you request. (https://github.com/PavelTorgashov/FastColoredTextBox). In that case:

  • You must trace selections that start with double clicks.
  • Instead of calling SelectWord function, use the Selection class and draggedRange attribute to mark the selected word in OnMouseMove.
  • You also must handle deselection of words in OnMouseMove.
  • You must also select spaces between words in OnMouseMove.

The double click is handled in the code piece below:

            if (!isLineSelect)
            {
                var p = PointToPlace(e.Location);

                if (e.Clicks == 2)
                {
                    mouseIsDrag = false; //Here, drag is cancelled. 
                    mouseIsDragDrop = false;
                    draggedRange = null; //Drag range is nullified

                    SelectWord(p); //SelectWord is called to mark the word
                    return;
                }

                if (Selection.IsEmpty || !Selection.Contains(p) || this[p.iLine].Count <= p.iChar || ReadOnly)
                    OnMouseClickText(e);
                else
                {
                    mouseIsDragDrop = true;
                    mouseIsDrag = false;
                }
            }

EDIT:

This may require a lot of work to accomplish. So maybe you should use another tool/library. I have not studied the whole source code so there will probably be additional steps to those provided above.

For example, to trace double clicks you can do the following:

  • Define a class variable/property in FastColoredTextbox.cs: bool isDoubleClick.
  • Set it to true in OnMouseDown under if(e.Clicks == 2) condition. Set it to false in all other conditions.
  • Set it to false in OnMouseClick or OnMouseUp or in other relevant mouse event handlers.

That way you will know if series of mouse events had started with a double click event or not. Then you would act accordingly in OnMouseMove because that is where you (un)mark characters or (un)mark words.

LAST WORDS OF CAUTION:

The author of that project did not include any inline comments or any other means of documentation so you will be studying the code line by line to understand what each function/part does.

like image 107
mostruash Avatar answered Nov 14 '22 00:11

mostruash


Add the following statement between Line 5276 and line 5277 in the class FastColoredTextBox.cs:

SelectWord(p);
mouseIsDrag = true; // here
return;

Note that implementing the ultimate behavior would require a good bunch of coding. Whereas the workaround mentioned above might satisfy your needs.

like image 2
yazanpro Avatar answered Nov 13 '22 22:11

yazanpro


As @mostruash points out in his answer, that is the place where author cancels the mouse drag. Not sure why he deliberately prevents this feature. Only he knows.

if (e.Clicks == 2)//Line 5270
{
    mouseIsDrag = false;
    mouseIsDragDrop = false;
    draggedRange = null;

    SelectWord(p);
    return;
}

I didn't read whole code, and I have no reason to do it. I just checked quickly and removed them. And it works as you expect.

if (e.Clicks == 2)//Line 5270
{
    //Comment or remove completely.

    //mouseIsDrag = false;
    //mouseIsDragDrop = false;
    //draggedRange = null;

    SelectWord(p);
    return;
}

Note: Am not sure this breaks something else, I've not tested. At least that works. Test it yourself.

like image 2
Sriram Sakthivel Avatar answered Nov 14 '22 00:11

Sriram Sakthivel