How to format plain text in WPF RichTextBox - wpf

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());
}

Related

Display tooltip only when text in Infragistics' UltraCombo is too long

We are using Infragistics' UltraCombo in a WinForms app.
Sometimes the displayed text (in the combo box itself, i.e. when it's not expanded) is too long to be completely shown.
Is there any way to only provide tooltips when this text is cut off and not to show the tooltip when the displayed text completely fits into the UltraCombo combobox?
Many thanks...
The only way I am aware is to manually determine if the current text is too wide.
Add a tooltip to the form. Then handle the TextChanging event on your combo box.
private void ultraCombo1_TextChanged( object sender, EventArgs e )
{
var textWidth = TextRenderer.MeasureText( ultraCombo1.Text, ultraCombo1.Font ).Width;
var textBoxWidth = ultraCombo1.ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth;
if ( textWidth < textBoxWidth )
toolTip1.SetToolTip( ultraCombo1, "" );
else
toolTip1.SetToolTip( ultraCombo1, ultraCombo1.Text);
}

Webbrowser Control - overtyping does not delete selected text when editing HTML [duplicate]

In my C# app I get an xml from a server that contains some replies like in a forum thread (with elements like author, time, body, title, whatever).
When I get this xml, I create a new form in which i want to display these replies, and a little text box with an "add reply" button. I'd also like some edit buttons on perhaps my own replies in the reply list displayed in the form.
The simplest way that came to my mind to display the replies is to put a web browser control in the form, generate a full html page in a string from the xml, and throw it in that web browser control. And under it i can put the text box with the add reply button.
Everything is ok, except that i have no idea of how i could implement the edit function on my own replies (i mean i could add a link in there... but link to what)
I would like to know if there is a way to get that edit event from the web browser control (my guess is i can't) or another (maybe simple/easy) idea of displaying the replies in a winform using other controls
Yes, that's possible, you want to turn "design mode" on for the document. Add a reference to Microsoft.mshtml. Start a new Windows Forms project and drop a WB and a button on the form. Make the code look similar to this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
webBrowser1.DocumentText = "<html><body><textarea rows='15' cols='92' name='post-text' id='wmd-input'></textarea></body></html>";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
button1.Click += button1_Click;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
mshtml.IHTMLDocument2 doc = webBrowser1.ActiveXInstance as mshtml.IHTMLDocument2;
doc.designMode = "On";
}
private void button1_Click(object sender, EventArgs e) {
var html = webBrowser1.Document.Body.All["post-text"].InnerHtml;
// do something with that
//...
}
}

Which control to show readonly colorful text in WPF?

I want an element or a control to show readyonly, colorful, selectable, scrollable text which is a kind of log in my application. I don't know whether it is fixed document or flow document.
The RichText may be the seeming choice, but it originally supports editing. I believe even I set readonly=true, the build-in editing support takes some resources. I want to find a lighter-weight one.
Perhaps the FlowDocumentScrollViewer? It is readonly and do not show tool bar by default. Even I turn IsToolBarVisible on, the tool bar is just a small control.
The Block came into my mind. Although it may be the lightest control, I cannot select the text in it without other effort.
Maybe other choices exist? What's your opinions?
I made an experiment to help me choose my preferable control among FlowDocumentScrollViewer, RichTextBox, and TextBlock. I find FlowDocumentScrollViewer is the best.
In each window I have two controls of same type: FlowDocumentScrollViewer, RichTextBox, or TextBlock. And I made three such windows, as the MainWindow has three buttons.
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
document1 = HelperClass.GetDocument();
document2 = HelperClass.GetDocument();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Document = document1;
viewer2.Document = document2;
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms",Title);
}));
}
Where viewer1 and viewer2 can be FlowDocumentScrollViewer or RichTextBox.
For TextBlock, I use
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
inlines1 = HelperClass.GetInlines();
inlines2 = HelperClass.GetInlines();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Inlines.AddRange(inlines1);
viewer2.Inlines.AddRange(inlines2);
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms");
}));
}
The test indicates FlowDocumentScrollViewer has best performance among the three:
FlowDocumentScrollViewer RichTextBox TextBlock
Working set 65400 67252 82124
Loading Time 1045 1414 45119
I'm not sure what type of resources you think are being taken up by "editing" functionality. The ability to select text goes hand in hand with ability to edit text.
If you want one, you'll have to put up with the other. Luckilly, setting IsReadOnly to "True" will satisfy your functional requirements.
If your application machine is capable of running the .NET Framework with WPF, I wouldn't worry about tiny amounts of resources which may (or may not) be consumed by the ability to edit simple text.

richtextbox binding

I have 2 RichTextBoxes (rtb1, rtb2), I something wrote in rtb1 and click on enter key, on this event is added text from rtb1 to rtb2. I solved this in code behind, it is possible this same write in XAML?
C# code:
private void rtb2_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var textElement = new Run() { Text = rtb2.Text };
var paragraph = new Paragraph();
paragraph.Inlines.Add(textElement);
rtb1.Document.Blocks.Add(paragraph);
rtb2.Document.Blocks.Clear();
//On this place I would like set start position for input text in rtb2 richtextbox on the start position
}
}
Thank for your advances.
If you really need your combined RichTextBox to have generalized document editing capabilities, what you are doing is probably the easiest and no, there is no pure-XAML way to do it.
On the other hand, if you are writing a chat application or something like it there may be a much better way to do it: Replace your combined RichTextBox with an ItemsControl that displays all the chat items as individual items. Then when Enter is pressed, add the text to a collection in your model. As long as the collection implements INotifyCollectionChanged, the new text will appear in your ItemsControl. And if the template you use for your ItemsControl allows editing, your user will be able to edit the item.
Which way you go all depends on what you're trying to accomplish.

Handle Silverlight 4 RichTextBox Paste event

How can I handle the Paste event for a RichTextBox control in Silverlight 4? (I want to be able to copy-paste images - the Clipboard in SL4 supports only text, so I'm sending the ImageSource Uri, and on the Paste event I want to load the image in the RichTextBox instead of the Uri string).
public class MyRichTextBox : RichTextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
string text = Clipboard.GetText();
this.Selection.Text = text;
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
...
You can handle the Silverlight 4 clipboard events and then check if focus on the RichTextBox and then simply add the content as a paragraph or other such elements. Have a quick search for Silverlight 4+Clipboard on Google for some good examples.
You would need to handle checking the format of the clipboard text in your handler and then converting if necessary (e.g. plain text, text copied from another RichTextBox, HTML formnatted text etc).
Hope that helps,

Resources