Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

OnMouseDown -  Select the word clicked to clipboard

Posted on 2014-01-30
4
Medium Priority
?
836 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

609 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