I'm currently trying to create a simple program in WPF to make cards for a custom card game.
Some of the descriptions feature rich text (like bold and italics) to highlight important things. The effect and the flavor text are copied into the same RichTextBox for aesthetic and convenience, which is why I need to be able to copy rich text from an input into a line/paragraph that is added to the display.
Here is the code:
private void EffectInput_TextChanged(object sender, TextChangedEventArgs e)
{
TextRange temp = new TextRange(EffectInput.Document.ContentStart, EffectInput.Document.ContentEnd);
Console.WriteLine(temp.Text);
effectText.Inlines.Clear();
effectText.Inlines.Add(new Run(temp.Text));
updateDescription();
}
private void updateDescription()
{
Description.Document.Blocks.Clear();
Description.Document.Blocks.Add(effectText);
Description.Document.Blocks.Add(flavorText);
}
I managed to make it work with the Text inside the box, but I can't find a way to also copy the format of the text. Can anyone help me?
Related
I am having trouble removing the formatting (bold, font, size etc.) on paste of some formatted text into a RichTextBox.
I have successfully been using the following:
Add a pasting handler to the RichTextBox
DataObject.AddPastingHandler(RichTextBoxControl, TextBoxPasting);
Remove formatting and insert text
private void TextBoxPasting(object sender, DataObjectPastingEventArgs e)
{
var pastingText = e.DataObject.GetData(DataFormats.Text) as string;
RichTextBoxControl.Document.ContentEnd.InsertTextInRun(pastingText);
e.CancelCommand();
}
But unfortunately this always places the inserted text in the end of the RichTextBox. Also the caret is not moving.
Let us say you are at this positing:
Helloꕯ World and you are pasting Beautiful you would get Helloꕯ World Beutiful instead of Hello Beutifulꕯ World.
Instead of manually inserting the text and cancelling the event you could just alter the text that is to be inserted in the DataObjectPastingEventArgs, and let the chains of the event do all the work for you.
private static void TextBoxPasting(object sender, DataObjectPastingEventArgs e)
{
e.DataObject = new DataObject(DataFormats.Text, e.DataObject.GetData(DataFormats.Text) as string ?? string.Empty);
}
e.DataObject.GetData(DataFormats.Text) is getting you the plain text without any formatting. The caret will move properly since you are not canceling the events that were supposed to move it.
There's a font that has a Windows 8 style progressring in itself as characters. However, there's too many of them and programming them one by each would take forever. Is there a way I could get a label to change it's text to those characters using a timer, but without having to program each character one by one? (The font is used in both the Windows boot screen, and the Windows 8-10 Media Creation Tool to display the progressring, I would imagine there's a way to do this in C# WinForms as well.)
Here's the font file opened in charmap:
The font with the progressring in charmap
In the end, this worked: (this is for the Setup font file, if you are using the C:\Windows\Boot\Fonts\segoe_slboot.ttf font file, look in charmap for the character code points)
char code = "\ue052"[0]; // U+E052 is the first character of the progressring
public Application()
{
InitializeComponent();
progressringLabel.Text = code;
}
private void progressringTimer_Tick(object sender, EventArgs e)
{
code++;
progressring.Text = code.ToString();
if (code == "\ue0CB"[0])
{
code = "\ue052"[0]; // When the code ends up being the last progressring character, revert back to the first one so that it won't go into the other characters
}
}
i was wondering how can i paste the content of my clipboard like Microsoft word do, i mean for example if i want to copy something like:
Some text
*some image*
More text
and paste it just like it was when i copied with the text and the image between the text, how can i do that?
I tried with Rich Textbox pasting with HTML Format but still got nothing...
So far i can only paste the text without format or the HTML text with the tags also without format...
By the way, is there some way to override the Ctrl + V paste method of a textbox?
Thank you
EDIT: I'm working on WinForms
If you can see the image displayed in RichTextBox via copy paste, then you can do the same from code as follow :
myRichTextBox.Rtf = Clipboard.GetText(TextDataFormat.Rtf);
simple step, set RichTextBox's Rtf property to value you get from Clipboard in Rtf format.
As to the first question, I don't believe you can do that with the RichTextBox. You can paste an image by itself, or you can paste just the text if you copied text and images from a website. But it won't paste both, unfortunately.
Here's a previous SO post where some suggestions were thrown out.
As for the other question, if you want to override CTRL+V functionality, you'll have to subscribe to the KeyDown and KeyPress events:
private bool IsPressedCtrlV = false;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
// You have access to modifiers in here, so you can detect the Control key
IsPressedCtrlV = (e.Modifiers == Keys.Control && e.KeyCode == Keys.V);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (IsPressedCtrlV)
{
// When the key "press" is complete, handle ctrl-v however you want
e.Handled = true;
MessageBox.Show("No pasting allowed!");
}
}
in c# I'm trying to display text (from txt file) in RichTextBox and I want display whole text using the preseted font (Arial). When I type the text "test ěščřžýáíé" it displays ok but when I try to set the Text programmaticaly
RichTextBox.Text="test ěščřžýáíé";
the words/characters are displayed with different fonts (the word "test" is really displayed in Arial). What do I do wrong?
Thanks for any help
Pavel
How are you setting the font? I assume you are using the Win Forms RichTextBox. I have managed to produce a very simple application which does what you have described as what you need:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 12.0f);
richTextBox1.Text = "test ěščřžýáíé";
}
With a Button button1 and a RichTextBox richTextBox1. When the button is clicked the text appears.
Can I open a PDF file in RichTextBox?
Short answer: No.
Longer answer: No. A RichTextBox is for displaying rich text. PDFs can contain anything including text, but that's not the document model underlying the RichTextBox. Besides, WPF does not handle PDF natively. There are third-party controls, however.
This question also has some pointers which may be of use to you, albeit not using a RichTextBox.
You need to use the Acrobat Control for ActiveX or at least the Adobe Reader 9 equivalent and use as
using PdfLib;
namespace WindowsFormsApplication1{
public partial class ViewerForm : Form{
public ViewerForm()
{
InitializeComponent();
PdfLib.AxAcroPDF axAcroPDF1;
axAcroPDF1.LoadFile(#"C:\Documents and Settings\jcrowe\Desktop\Medical Gas\_0708170240_001.pdf");
axAcroPDF1.Show(); }
private void richTextBox1_TextChanged(object sender, EventArgs e)
{ } } }
You can write a simple app in a few seconds containing a WebBrowser control, and just call the navigate method and give it a URL pointing to the document you want.
XAML:
<Grid>
<WebBrowser x:Name="Browser"/>
</Grid>
C#:
private void Window1_Loaded(object sender, WindowLoadedArgs args)
{
Browser.Navigate(new URL("path to document.pdf");
}
Note: I am writing from memory so consider this pseudocode rather than something that will work as-is.