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"
Related
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.
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.
How can I extend the .text and .caption property of ALL controls. I want to create a multilanguage App, which load the text from a custom file.
I want to do this:
button1.Text.LoadLocalizedText()
label1.Caption.LoadLocalizedText()
Now, I need the following text result, to get the correct text.
I need "class.button1.Text" as string and for the label "class.label1.Caption" as string.
After I load the custom text, I must set the text dynamically to the "source" property like text or caption.
I hope, someone could help me.
Thanks.
If you want to write a method you can call on all controls, write a extension method for control (see #Adriani6's answer)
public static void LoadLocalizedText(this Control ctrl) {...}
If you want to load a resource localized by your culture, you should use your resource files and create files for each culture.
Create 2 resource files like Resource1.resx and Resource1.de.resx and save your corresponding values to the resource files.
Then, you can load your resources like
[ProjectNamespace.]Resources.Resource1.ResourceManager.GetString("myResource");
or, alternatively with an overload to specify the culture yourself
Resources.Resource1.ResourceManager.GetString("myResource", System.Globalization.CultureInfo.GetCultureInfo("de"));
See Microsoft Docs
If this is WinForms, you can write an extension method for the Control class.
public static class LocalizationExtension
{
public static void LoadLocalizedText(this Control ctrl)
{
ctrl.Text = "My loaded string";
}
}
This way you can target all controls that inherit from Control such as Button and Label.
You just call it using Button.LoadLocalizedText() and Label.LocdLocalizedText() etc..
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 have setup a profanity filter with bad words in a XML file and have the following function to run on my page to replace the words:
BadWordFilter.Instance.GetCleanString(TextBox1.Text);
i'm about to go through my entire site now wrapping that function around every little text variable one by one and it's going to be a huge pain in the butt
i'm hoping there's a way that i could just set my masterpage to automatically run all text through this thing on any page_load, so that the effect would be site-wide instantly. is this possible?
much appreciated for any help
One quick tip I have is to use the tag mapping feature of asp.net for this:
Create a custom textbox class derived from the TextBox class
Override the get/set Text property & in the get part, return the cleaned string
Use tag mapping feature in the web.config file to replace all TextBox classes with your custom text box class & everything should work really well.
This link has a sample implementation which uses the HTMLEncode, but you get the idea: http://www.devwebpro.co.uk/devwebprouk-46-20071010ASPNETTagMapping.html
HTH.
I realize you said Page_Load(), but I suspect this will do what you need.
In the Page.PreRender event, walk through the controls:
/* inside Page_PreRender() handler...*/
if (user_options.filterBadWords == true)
{
FilterControls(this);
}
/* this does the real work*/
private void FilterControls(Control ctrl)
{
foreach (Control c in ctrl.Controls)
{
if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
TextBox t = (TextBox)c;
/* do your thing */
t.Text = BadWordsFilter(t.Text);
}
if (c.HasControls())
FilterControls(c);
}
}
This is a hack, which will get you through your current problem: overriding the TextBox control is ultimately a better solution.