Solved

OnMouseDown -  Select the word clicked to clipboard

Posted on 2014-01-30
4
815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

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 27

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I'd like to talk about something that is near and dear to my heart: build systems. Without them, building software is all about compiling locally, with software versions everywhere. It can be a mess. Today we are going to discuss building a small di…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

717 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