views:

27

answers:

1

i have two richtextboxes one below the other in my application.when the user start selection in one richtextbox and continue to the other richtextbox selection should automatically move to the second richtextbox.is there any way to do this type of selection.

thanks in advance, dhyanesh

A: 

You'd think you could use MouseEnter and MouseLeave, but when the mouse is captured (as it is during text selection), these events don't fire as expected.

The way to achieve your goal is:

  1. Subscribe to MouseMove on the first RichTextBox.
  2. In the MouseMove event, check Mouse.Captured to see if it is the RichTextBox.
  3. If the mouse is captured, do a hit test on the mouse's current postion using VisualTreeHelper.HitTest. Go up the visual tree from the value of HitTestResult.VisualHit to see if the mouse is over a RichTextBox other than the current one.
  4. If the mouse is over a new RichTextBox, cancel the mouse capture with Mouse.Capture(null), then fire a MouseLeftButtonDown event on the new RichTextBox to cause it to capture the mouse and begin selection.
Ray Burns
It worked thank you very much
Dhyanesh
i want to retain the selection for multiple textbox.is there any way to do this.
Dhyanesh
I believe it is possible only if you put each RichTextBox in a different focus scope using FocusManager.IsFocusScope, but I'd be concerned that allowing two controls to be focused at once may be confusing to the end-user. Another approach would be to actually update colors or other attributes of the document to indicate selection.
Ray Burns