c# - How can i get the selected text in a richTextBox using the mouse and display the text in real time? -


today when click them mouse on rich-textbox text window , drag mouse right it's highlighting text in blue.

what want simulate situation time:

  1. to make when moving mouse , highlighting text color in yellow not default blue.

  2. to make while mouse left button clicked , pressed down nonstop , move mouse on text show me text in label in real time.

what did far:

private void richtextbox1_mousedown(object sender, mouseeventargs e)         {             if (e.clicks == 1 && e.button == mousebuttons.left)             {                 int positionstart = richtextbox1.getcharindexfromposition                                        (new point(e.x, e.y));                 this.richtextbox1.selectionstart = positionstart;              }         } 

not sure if mouse-down correct event , maybe need use mouse-up event. or maybe mouse move too.

now have selectionstart index.

but should next ?

edit:

for displaying in real time selectedtext it's working coloring in real time not working. tried:

font drawfont = new font("arial", 16);         solidbrush drawbrush = new solidbrush(color.yellow);         pointf drawpoint = new pointf(0, 0);          protected override void onpaint(painteventargs e)         {             base.onpaint(e);             e.graphics.drawstring(richtextbox1.selectedtext, drawfont, drawbrush,drawpoint);         } 

but it's not coloring @ all. checked breakpoint it's getting there not coloring text. want in in realtime when in mousemove event color selectedtext.

in order highlight text when mouse on try define attached property:

public class textboxproperties : dependencyobject {     public static readonly dependencyproperty highlighttextonfocusproperty = dependencyproperty.registerattached("highlighttextonfocus", typeof(bool), typeof(textboxproperties), new propertymetadata(false, highlighttextonfocuspropertychanged));       [attachedpropertybrowsableforchildrenattribute(includedescendants = false)]     [attachedpropertybrowsablefortype(typeof(textbox))]     public static bool gethighlighttextonfocus(dependencyobject obj)     {         return (bool)obj.getvalue(highlighttextonfocusproperty);     }      public static void sethighlighttextonfocus(dependencyobject obj, bool value)     {         obj.setvalue(highlighttextonfocusproperty, value);     }      private static void highlighttextonfocuspropertychanged(             dependencyobject obj, dependencypropertychangedeventargs e)     {         var sender = obj uielement;         if (sender != null)         {             if ((bool)e.newvalue)             {                 sender.gotkeyboardfocus += onkeyboardfocusselecttext;                 sender.previewmousemove += onmousemovesetfocus;             }             else             {                 sender.gotkeyboardfocus -= onkeyboardfocusselecttext;                 sender.previewmouseleftbuttondown -= onmousemovesetfocus;             }         }     }      private static void onkeyboardfocusselecttext(         object sender, system.windows.input.keyboardfocuschangedeventargs e)     {         var textbox = e.originalsource textbox;         if (textbox != null)         {             textbox.selectall();         }     }      private static void onmousemovesetfocus(         object sender, system.windows.input.mouseeventargs e)     {         textbox tb = findancestor<textbox>((dependencyobject)e.originalsource);          if (tb == null)             return;          if (!tb.iskeyboardfocuswithin)         {             tb.focus();             e.handled = true;         }     }      static t findancestor<t>(dependencyobject current)         t : dependencyobject     {         current = visualtreehelper.getparent(current);          while (current != null)         {             if (current t)             {                 return (t)current;             }             current = visualtreehelper.getparent(current);         };         return null;     } } 

add in xaml :

                 <textbox grid.column="1"                            text="{binding testproperty}"                            verticalalignment="center"                            isreadonly="true"                           horizontalalignment="left"                           converters:textboxproperties.highlighttextonfocus="true"                          /> 

and of course, don't forget namespace :

          xmlns:converters="clr-namespace:yourproject.converters" 

further more, in order change highlight color on textbox try use selectionbrush :

 <textbox selectionbrush="red" selectionopacity="0.5"/>   

Comments