Solved

OnMouseDown -  Select the word clicked to clipboard

Posted on 2014-01-30
4
777 Views
Last Modified: 2014-11-12
I would like to "click" on a word inside a RichTextBox and put it on another textbox. I see answers for Select All text, but I wanted only the word where cursor has focus, I'm sure is possible because in my Windows Phone when I click a word it gets selected and I can paste it where I want! Thks
0
Comment
Question by:novreisb
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39822561
	public static void CopyFromRichTextBox(RichTextBox rtb, bool availableAfterEnd = false)
	{
		DataObject data = new DataObject();

		// get the selected RTF text if there is a selection,
		// or the entire text is no text is selected
		string rtfText = null;
		string plainText = null;
		if (rtb.SelectionLength > 0) {
			rtfText = rtb.SelectedRtf;
			plainText = rtb.SelectedText;
		}
		// do the copy only if there is something to be copied
		if (rtfText.Length > 0)
			data.SetData(DataFormats.Rtf, rtfText);
		if (plainText.Length > 0)
			data.SetData(DataFormats.Text, plainText);

		// finally copy into the clipboard
		Clipboard.SetDataObject(data, availableAfterEnd);
	}

Open in new window

0
 

Author Comment

by:novreisb
ID: 39822742
Hi MacroShadow, thks by your answer but it has two problems because perhaps  I was not clear!

I'm using VS2013, c# , WPF and 4.5 fw.

1) your code gives me errors (ex.rtb.SelectionLength)

2) my wish is more complicated than this. As far I understand I need to select the word and I did not want that, because it will be easy after to Ctrl C and  Ctrl V.

In my Lumia 820 I touch a word and the word get selected automatically. I think that it works with word boundaries or something else!
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39822809
This code should do what you want.
var mousePosition = Mouse.GetPosition(YourRichTextbox);
var textPointer = YourRichTextbox.GetPositionFromPoint(mousePosition, false);

private string GetWordAtPointer(TextPointer textPointer)
{
    return string.Join(string.Empty, GetWordCharactersBefore(textPointer), GetWordCharactersAfter(textPointer));
}
{
    // Copy the returned text to the Clipboard
    Clipboard.SetText(GetWordAtPointer, YourDestinationTextbox.Text);
}

Open in new window

0
 

Author Closing Comment

by:novreisb
ID: 39822888
Hi MacroShadow, your code was not complete, but with it I got to the final solution with some changes , which was excellent.

public MainWindow()
        {
            InitializeComponent();
            textBox6.GotFocus += textBox6_GotFocus;
         }


private void textBox6_GotFocus(object sender, RoutedEventArgs e)
    {
    var mousePosition = Mouse.GetPosition(textBox6);
    var textPointer = textBox6.GetPositionFromPoint(mousePosition, false);
    string selecc = GetWordAtPointer(textPointer);
   // Copy the returned text to the Clipboard
    LC.Text = selecc;
}
   
    private string GetWordAtPointer(TextPointer textPointer)
{
    return string.Join(string.Empty, GetWordCharactersBefore(textPointer), GetWordCharactersAfter(textPointer));
}



private string GetWordCharactersBefore(TextPointer textPointer)
{
    var backwards = textPointer.GetTextInRun(LogicalDirection.Backward);
    var wordCharactersBeforePointer = new string(backwards.Reverse().TakeWhile(c => !char.IsSeparator(c) && !char.IsPunctuation(c)).Reverse().ToArray());

    return wordCharactersBeforePointer;
}

private string GetWordCharactersAfter(TextPointer textPointer)
{
    var fowards = textPointer.GetTextInRun(LogicalDirection.Forward);
    var wordCharactersAfterPointer = new string(fowards.TakeWhile(c => !char.IsSeparator(c) && !char.IsPunctuation(c)).ToArray());

    return wordCharactersAfterPointer;
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now