Solved

OnMouseDown -  Select the word clicked to clipboard

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
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.

930 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

10 Experts available now in Live!

Get 1:1 Help Now