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.
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.
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?
I am really confused about this. Please consider below scenario.
Scenario:
I have a C# Winform app with few Textbox controls. Now when I input data in these text boxes, for example, "THIS IS MY SAMPLE TEXTBOX", which overlaps the Textbox's visible area and displaying as "AMPLE TEXTBOX". But I want the text to be displayed from the starting position like "THIS IS MY S" and then if needed, overlaps. How can I do this? I have tried below but no luck. Please help. Thanks.
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
Edit
I am also using AutoCompleteMode.Suggest so that when I press any key, corresponding list will be displayed similar to dropdownlist. But the first item of this list is selected by default, which I do not want. Can you please suggest in this also. Thanks.
Final Solution
I am using this to solve the issue
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
(sender as TextBox).Select(0, 0);
Thanks to #Har Har.
I found the solution, To position the cursor at the beginning of the contents of a TextBox control, call the Select method and specify the selection start position of 0, and a selection length of 0.
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Hello this is a sample application";
textBox1.Select(0, 0);
}
It will show the cursor position at 0 index, It's working.
In my application, i want user to select a font, from a list of fonts available in user's system and set that font to richtextbox.
I tried ::
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (FontFamily family in fonts.Families)
{
comboBox1.Items.Add(family.Name);
}
and
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
richTextBox1.Font = new Font(comboBox1.SelectedText, 14);
}
but it changes the default behaviour of the text. For example: text includes headings in bold and italics. Setting the font with above code transforms the text in the regular form.
My question is, to possibly keep the behaviour of the text as before and not to change the complete text in regular form by keeping the bold text in bold and italics in italics.
SelectedText is for the highlighted part of the ComboBox text property. Probably not what you want:
richTextBox1.Font = new Font(comboBox1.Text, 14);
Also, if you just want the highlighted part of the RichTextBox to have the new font:
richTextBox1.SelectionFont = new Font(comboBox1.Text, 14);
If any part of the highlighted text has a mix of bold and italics and different sizes, etc, the RichTextBox does not preserve those properties very well. See How do I maintain RichText formatting (bold/italic/etc) when changing any one element?
There's an option to initialize a font with a font prototype. Try defining a font prototype as a font with micrsoftsansserif, with desired size and style, and then pass the new font as the second parameter. msdn has all the info.
im trying to work with the menustrip and i have the helpToolStripMenuItem_Click may someone help me the code on how to put the documentation i.e if i click help a new window show appear with documentation like this picture i capture from a vlc help button
any ideas this is my empty code
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
}
Make form (for example with name frm_Help) with RichTextBox and Button
In Richtextbox add all text (read this Richtextbox)
In button event click add
{
Close();
}
Somewhere where you want to show this form add
{
frm_Help frm=new frm_Help;
frm.ShowDialog();
}
To get the same result as in the image you posted, you need to create a new form, put a WebBrowser control to it and load the HTML page with the documentation to this control in the form's initialization code:
public HelpForm()
{
InitializeComponent();
string text = System.IO.File.ReadAllText(#"Docs\readme.html");
webBrowser1.DocumentText = text;
}
Here, the code reads HTML from the readme.html file located in the Docs subfolder of the folder where the EXE file is located.
You should create new form, containing all the text you want in it and in helpToolStripMenuItem_Click you should create new instance of that form and show it (as a dialog maybe)
Create a new form or add AboutBox and add RichTextbox control.