How to copy richtextbox content with link? - c#

I want to copy richtextbox content with keeping format same and hyperlinks. But it is been copied as a plain text without hyperlinks. I am using linklabel in richtextbox.
private void Bttn_copy_Click(object sender, EventArgs e)
{
richtxtbx_email.SelectAll();
Clipboard.Clear();
Clipboard.SetText(richtxtbx_email.SelectedRtf, TextDataFormat.Rtf);
}
and trying this:
DataObject dto = new DataObject();
dto.SetText(mesrtf, TextDataFormat.Rtf);
dto.SetText(mes, TextDataFormat.UnicodeText);
Clipboard.Clear();
Clipboard.SetDataObject(dto);
Can you help me solve this issue ?

Hyperlinks are just a way of a using hypertext links inside an editor that is capable of rendering them as such.
When copying the text from the textbox, you can only copy the plaintext itself.
Note that RichTextBox.SelectedRtf is property of type string.

RichTextBox doesn't hold a hyperlink like HTML does.
It only detects if a certain text looks like a link and automatically colores it blue, add underline and detect if the user clicks on it.
It does so if the RichTextBox.DetectUrls Property is set to true.
If you are copying data to a new RichTextBox and don't see the link that was detected in the other RichTextBox then you just need to set this property to true before you copy the text.
On the other hand, if you need real links so that the text is one thing and the link is another have a look here.

Related

Adding text (Or getting CaretPosition) of RichTextBox in .Net Core

I have a basic text editor app, and I aim to add a feature where the user can click a button and add premade text after where their cursor is.
I currently have this (using some code I found online)
richTextBox1.CaretPosition.InsertTextInRun(s);
I intend for the string s to be the string to be added.
However, the RichTextBox in System.Windows.Forms does NOT contain a CaretPosition. I found one post from 2010 suggesting you use System.Windows.Control, however, that is no longer accessible in .Net Core, it depends on presentation framework.
So, is there any way I could get my goal (inserting a string after the mouse cursor, in a rich text box), in .net core?
Updated2:
Use the following code to fully satisfy the richtextbox of the text text environment.:
richTextBox1.Select(richTextBox1.SelectionStart, richTextBox1.TextLength);//Select everything after the cursor
string tmp = richTextBox1.SelectedText;//Copy them
richTextBox1.SelectedText = "";//Set to null
richTextBox1.AppendText("Hello world"+tmp);//Add the target string and add the original text
Updated1:
// Determine if there is any text in the Clipboard to paste into the text box.
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
IDataObject dataObject = Clipboard.GetDataObject();
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
Clipboard.SetDataObject(dataObject);
}
else
{
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
}
This action preserves the contents of the original pasteboard.
Only works if the clipboard is data. I will continue to update after I think about it.
Original:
You only need to use the clipboard and paste method to insert the specified string after the specified cursor.
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
Clipboard.Clear();
Change it yourself according to your needs.

C# Winforms Help Text Change font

I have a little help pop-up that displays some text when the user presses a "?" label next to a drop-down to explain the different selections.
I did it using the Help.ShowPopup command since that seemed the easiest.
I was hoping there was a way to add different font properties to parts of the text or at least to the whole thing without having to go the direction of a CHM/HTML help-file.
Here is what I am trying to do:
private void helpLbl_Click(object sender, EventArgs e)
{
// for some reason, it ignores the 'parent' parameter
// and lays it out on the screen's coordinates
Point helpLocation = helpLbl.PointToScreen(Point.Empty);
helpLocation.Y += helpLbl.Height; // have it display underneath the control
Help.ShowPopup(this, // hosting form
#"<b>Fixed:</b>
Removes a fixed amount from the sale
<b>Percent Value:</b>
Removes a set percentage of the selected package from the sale
...", helpLocation);
I was hoping since there's the option to use an HTML document to display the help, I could use HTML tags to format what was being displayed, but it doesn't appear so. Any ideas?
Is there a way to do something like displaying a RichTextBox in the help pop-up?
Another possibility is generating a HTML document on-the-fly, but it asks for a "url" if I'm not supplying the text directly and I think that might be a little over-kill for the small amount I'm trying to do here.
You have two options. One is to use a WebBrowser Control. This natively accepts HTML and displays it. The problem with it is its kind of bloated just to use as a simple label.
Your second option is to simply create a RichTextLabel, simply like this:
public class RichTextLabel : RichTextBox
{
public RichTextLabel()
{
BorderStyle = BorderStyle.None;
}
}
Add this to your form and set the Rtf property to your RTF code. You will have to convert your HTML to RTF, which is easy if you got a program such as Microsoft Word, for example.

I want to select all and copy it to clipboard

I have a WebBrowser displaying text.
If i copy it to clipbaord it copy's all the html tags to and i don't want that.
I want to be able to select all then copy to clipboard.
I want to copy the text and its formatting to the clipboard.
When i highlight the text my self and click copy when i paste, its perfect just how i want it.
But when i use this code to copy just the Document text i get the Html tags to.
This is how i copy to clipboard:
void CopyCellText()
{
Clipboard.Clear();
if (webBrowser1 != null)
{
Clipboard.SetText(webBrowser1.DocumentText.ToString().Trim());
}
}
To Select all and copy to clipboard:
webBrowser1.Document.ExecCommand("SelectAll", true, null);
webBrowser1.Document.ExecCommand("Copy", true, null);
You wont see the html tags but have all there formatting.
You mean you want to convert your html code to text and copy to clipboard? You will need HTML Agility Pack. Check this page for an easy guide.
http://www.dreamincode.net/code/snippet1921.htm << check this code snippet. it would be better, if you strip the string while using regex!
I think the reason you are getting the HTML tags is webBrowser1.DocumentText will take the entire content of the HTML document itself, which will include all the generated HTML.
A quick search gave me the following:
Retrieving Selected Text from Webbrowser control in .net(C#)
Get all text from WebBrowser control

Text areas and hyperlinks?

I have two quick, easy questions on C# in Visual Studio. First, is there anything like the label, but for an area of text in the program? I would like to have multiple lines of text in my program, but can only seem to accomplish it with a DotNetBar label with wordwrap turned on.
Second, is there any way to have a hyperlink in the middle of the text without using a link label? If I wanted to generate text like "An update is available, please visit http://example.com to download it!", is it possible to make the link clickable without having to position a link label in the middle of the text?
You can use a LinkLabel and set its LinkArea property:
//LinkArea (start index, length)
myLinkLabel.LinkArea = new LinkArea(37, 18);
myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";
The above will make the http://example.com a link whilst the rest of the text in normal.
Edit to answer comment:
There are various ways of handling the link. One way is to give the link a description (the URL) and then launch the URL using Process.Start.
myLinkLabel.LinkArea = new System.Windows.Forms.LinkArea(37, 18);
myLinkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(myLinkLabel_LinkClicked);
myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";
myLinkLabel.Links[0].Description = "http://example.com";
And the event handler can read the description and launch the site:
void myLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(e.Link.Description);
}
You may try RichTextBox control.
string text = "This is the extract of text located at http://www.google.com and http://www.yahoo.com";
richTextBox1.Text = text;
richTextBox1.ReadOnly = true;
richTextBox1.LinkClicked += (sa, ea) =>
{
System.Diagnostics.Process.Start(ea.LinkText);
};
You can use the normal label and make the AutoSize property as false.
And then adjust your width and height it will wrap by it self
I assume you are using doing a windows application, and not a web application.
In C# you can create a normal textbox by dragging and dropping it onto your form, change its property to multi-line, and make it read only. Thats what I always do.
As for adding a link to the text without a linklabel. There is a way to add links to textboxes. You can check out a pretty good tutorial at http://www.codeproject.com/KB/miscctrl/LinkTextBox.aspx/

How to retrieve text from the clipboard in partial trust

We have a RichTextBox WPF control and since we control the layout, we simply cannot allow any rich content...
Therefor, we need to strip all data except text from the clipboard. For example, if someone is trying to copy/paste lets say text from a table directly from Microsoft Word, the RichTextBox also takes into account that this text was 1. originally from a table, 2. bold and 3. underlined, and create all sorts of inline content to accomodate all these properties of the text...
This is not appropiate behaviour in our case, because it can break our inline layouts.. we just want the clean text...
The most simple approach would be, in the preview paste command:
Clipboard.SetText(Clipboard.GetText());
and be done with it... But you guessed it.. Clipboard operations are not allowed in partial trust...
We also tried a dirty nasty hack, using a hidden Textbox suggested by this link:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5b5bcd71-2eea-4762-bf65-84176c919fce/
Like so:
public static class ClipboardManager
{
private static readonly TextBox textBox = new TextBox {AcceptsReturn = true, AcceptsTab = true};
public static void SetText(string text)
{
textBox.Text = text;
textBox.SelectAll();
textBox.Copy();
}
public static string GetText()
{
textBox.Clear();
textBox.Paste();
return textBox.Text;
}
}
And then call it like this:
ClipboardManager.SetText(ClipboardManager.GetText());
This works well in full trust, but for some reason, both Copy and Paste methods of TextBox do not work in partial trust...
Does anyone know how to retrieve the Clipboard's content in WPF/partial trust ?
Thanks
Edit: As Nir pointed out.. I know it's not very nice to mutate data from your clipboard.. But my question will be answered just the same if someone can just point me out how to retrieve only the text from the clipboard in partial trust :)..
It's simply not possible.
In the end we used a toggle button where you could toggle to a textbox, paste it in there, and toggle back to our control. Nasty, but it works.
http://msdn.microsoft.com/en-us/library/aa970910.aspx says only "Plaintext and Ink Clipboard Support" in Partial Trust. Full Trust is required for "Rich Text Format Clipboard"

Categories