Solved

OnMouseDown -  Select the word clicked to clipboard

Posted on 2014-01-30
4
804 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 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to run a powershell script in Dell KACE (K2000) systems deployment appliance? 2 292
dialer.exe 4 68
Question to Pivot table 1 67
VS2015 Redefinition errors 4 91
The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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…

726 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