How to place text and Button in a same line in WPF Richtextbox?
In the image below some texts and a button control are placed in a richtextbox.
I want some texts to be placed before and after the button in the same line.
Is it possible to define some width to the blockuicontainer or a wayout to achieve the same?
What I want is:
I have added a FlowDocument to the RichTextBox and a Paragraph to the FlowDocument.
Sample code is:
Paragraph para = new Paragraph();
FlowDocument fd = new FlowDocument();
fd.Blocks.Add(para);
txtExpression.Document = fd;
para.Inlines.Add(new Button() { Content = "Hello!" });
Related
Much like the editor you see on StackOverflow, I want users to be able to specify that parts of their text should be bold, italic, or underline, but I do not want them to be able to set the font size or family; these need to be inherited from somewhere else in the visual tree.
When placing the WPF RichTextBox control into an empty window and providing a few characters of text, the serialized representation of the rich text always includes the FontFamily. e.g., in LINQPad:
void Main()
{
var window = new Window();
var editor = new RichTextBox();
window.Content = editor;
window.ShowDialog();
var rtf = RtfUtility.ConvertToRtf(editor.Document);
rtf.Dump();
}
I entered "HELLO, WORLD!" into the RichTextBox.
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch HELLO, WORLD!}\li0\ri0\sa0\sb0\fi0\ql\par}}}
The RTF serialization code is nothing special:
var range = new TextRange(document.ContentStart, document.ContentEnd);
using (var stream = new MemoryStream())
{
range.Save(stream, DataFormats.Rtf);
stream.Position = 0;
using (var reader = new StreamReader(stream, Encoding.UTF8))
return reader.ReadToEnd();
}
The serialized representation references both Times New Roman and Segoe UI, but this is undesirable.
Is it possible to present rich text and inherit the font family and size from elsewhere, as well as serialize it without these properties?
I suppose an alternative is to set FontFamily and FontSize to whatever I want each time the text is deserialized -- but that just seems hacky. I'd also be open to an entirely different solution that does not involve RichTextBox, if that's feasible.
FlowDocument assumes Sergoe UI as default font. This font is in the style for FlowDocument objects.
To change this:
editor.Document.FontFamily = new FontFamily("Times New Roman");
editor.Document.FontSize = 12;
You can also create a style for all FlowDocuments using xaml declarations in App.xaml or programmatically, for each FlowDocument:
var style = new Style(typeof(FlowDocument));
style.Setters.Add(new Setter(FlowDocument.FontFamilyProperty, new FontFamily("Times New Roman")));
style.Setters.Add(new Setter(FlowDocument.FontSizeProperty, 12));
editor.Resources.Add(typeof(FlowDocument), style)
When creating style for a paragraph (new Style(typeof(Paragraph))) you can also change each individual paragraph's settings.
Unfortunately serialization saves FlowDocument's resources and all settings which are not inherited.
You can use a textrange:
TextRange tr2 = new TextRange(mergedDocument.ContentStart, mergedDocument.ContentEnd);
tr2.ApplyPropertyValue(Span.FontFamilyProperty, font);
tr2.ApplyPropertyValue(Span.FontSizeProperty, "24");
tr2.ApplyPropertyValue(List.FontFamilyProperty, font);
tr2.ApplyPropertyValue(List.FontSizeProperty, "24");
I'm using the RichTextBox to display some readonly text that I have to markup on the fly.
How can I pass markup codes in text to have it rendered by the RichTextBox control?
For example, I'd like to pass this is \cf6 sample \cf1 text to the richtextbox for it to render.
Right now, I build a FlowDocument and add the text value to a run object, but the text gets rendered literally.
RichTextBox fieldLabel = new RichTextBox();
FlowDocument flowDoc = new FlowDocument();
Paragraph myPara = new Paragraph();
Run myRun = new Run(content);
myPara.Inlines.Add(myRun);
flowDoc.Blocks.Add(myPara);
fieldLabel.Document = flowDoc;
I want to see the value in red, but I see the markup instead.
Thanks in advance for any input.
You cant assign RTF text just like that. You need to get that text into a stream and then pass that stream to the RichTextBox.Selection.Load() method. e.g.
MemoryStream stream = new MemoryStream(UTF8Encoding.Default.GetBytes(yourRTFText));
fieldLabel.Selection.Load(stream, DataFormats.Rtf);
You have to apply to the paragraph
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Add paragraphs to the FlowDocument.
myFlowDoc.Background = Brushes.LightBlue;
myFlowDoc.Foreground = Brushes.DarkRed;
myFlowDoc.Typography.Capitals = FontCapitals.SmallCaps;
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
Paragraph p = new Paragraph(new Run("Paragraph 2"));
p.Foreground = Brushes.Black;
myFlowDoc.Blocks.Add(p);
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
RichTextBox myRichTextBox = new RichTextBox();
// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;
I saw how to set a WPF rich text box in RichTextBox Class.
Yet I like to save its text to the database like I used to, in Windows Forms.
string myData = richTextBox.Text;
dbSave(myData);
How can I do it?
At the bottom of the MSDN RichTextBox reference there's a link to How to Extract the Text Content from a RichTextBox
It's going to look like this:
public string RichTextBoxExample()
{
RichTextBox myRichTextBox = new RichTextBox();
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;
// Let's pretend the RichTextBox gets content magically ...
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
myRichTextBox.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
myRichTextBox.Document.ContentEnd
);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}
I have developed a small chat client using WPF. In each chat window, it contains a richtextbox to display previous chat conversations and a textbox with send button to type a chat message.
I want to format the display text in the richtextbox as shown below.
user1: chat message goes here
For the time being, I use AppendText function to append chat conversation to the richtextbox. my code looks like this,
this.ShowChatConversationsBox.AppendText(from+": "+text);
But in this way, i couldn't find a method to format the text as show above. Is there any way to do this? or any alternative methods?
thanks
Instead of interacting with the RichTextBox, you can interact with the FlowDocument directly to add rich text. Set the Document on the RichTextBox to a FlowDocument containing a Paragraph, and add Inline objects such as Run or Bold to the Paragraph. You can format the text by setting properties on the Paragraph or on the Inlines. For example:
public MainWindow()
{
InitializeComponent();
this.paragraph = new Paragraph();
this.ShowChatConversationsBox.Document = new FlowDocument(paragraph);
}
private Paragraph paragraph;
private void Button_Click(object sender, RoutedEventArgs e)
{
var from = "user1";
var text = "chat message goes here";
paragraph.Inlines.Add(new Bold(new Run(from + ": "))
{
Foreground = Brushes.Red
});
paragraph.Inlines.Add(text);
paragraph.Inlines.Add(new LineBreak());
}
How do I implement a tab control with vertical tabs in C#?
Create an instance of System.Windows.Forms.TabControl (one of the standard container controls for Windows Forms) and set the Alignment property to Left.
First set in properties the Alignment property to Left.
Second set SizeMode property to Fixe.
Third set ItemSize property to prefered size example width :30 height :120.
After that you need to set the DrawMode property to OwnerDrawFixed.
Next step is define a handler for the DrawItem event of TabControl that renders the text from left to right.
Example
In form Designers.cs file
TabControl.DrawItem += new DrawItemEventHandler(tabControl_DrawItem);
Definition for tabControl_DrawItem method:
private void tabControl_DrawItem(Object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Brush _textBrush;
// Get the item from the collection.
TabPage _tabPage = TabControl.TabPages[e.Index];
// Get the real bounds for the tab rectangle.
Rectangle _tabBounds = TabControl.GetTabRect(e.Index);
_textBrush = new System.Drawing.SolidBrush(Color.Black);
// Use our own font.
Font _tabFont = new Font("Arial", (float)12.0, FontStyle.Bold, GraphicsUnit.Pixel);
// Draw string. Center the text.
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;
_stringFlags.LineAlignment = StringAlignment.Center;
g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));
}
Effect:Ready horizontal tabcontrol
I was based on https://msdn.microsoft.com/en-us/library/ms404305(v=vs.110).aspx