This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to add emoticons(smileys) into WPF richtext box
Now,I want to know , how to add emoticon in RichTextBox in WPF.
For example: when I type hi :) , it will return as image(smile image) in RichTexBox.
I hope , you will help me. if anyone know the solution or example code
Thanks
There isn't a prebuilt solution for this, but, as you're using WPF, you can either react to change of the text inside the RichTextBox via the TextChanged event or via your PropertyChanged -if it's MVVM-.
Once you know text changed you give it a pass by using a simple search (there are way better ways to do this parsing, but for the sake of a simple example just use a simple string search). If you find the text you want, I mean, the token corresponding your smiley hi :) then you can insert the image on that position by playing with the FlowDocument inside the RichTextBox
Before parsing
<RichTextBox>
<FlowDocument>
<Paragraph>
<Run>Hello :) world!</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
After parsing
<RichTextBox>
<FlowDocument>
<Paragraph>
<Span>
<Run Text="Hello"/>
<Image Width="16" Source="Your emoticon source"/>
<Run Text=" world"/>
</Span>
</Paragraph>
</FlowDocument>
</RichTextBox>
That should give you an idea of how to begin.
Related
I am trying to set an image into a richtextbox.
Can this be done and if so how?
Will I have to use an online textbox and load it into my form using the webbrowser, such as with monaco?
Using the properties menu, I do not see anything about images in the richtextbox, only backcolor, so I tried changing the backcolor to "transparent" because I thought of putting an imagebox under it and setting the link of the image there(say this one: https://media.tenor.com/images/cdcf2410001a5c02bcd371d13743aa57/tenor.gif)
Can anyone help me with this?
In WPF, you can add a BlockUIContainer to the document of your richtextbox. This allows you to input an IElement and display it as part of the document. This is great because that means you can input a picture here aswell.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.documents.blockuicontainer?view=netframework-4.8
Sample:
<RichTextBox HorizontalAlignment="Center" Height="100" VerticalAlignment="Center" Width="100" BorderBrush="Gray">
<FlowDocument>
<BlockUIContainer>
<!-- Here you can input any wpf controls, like the Image control. -->
<Image Height="100" Width="100" Source="C:\Some\Image.png" Stretch="Fill"/>
</BlockUIContainer>
</FlowDocument>
</RichTextBox>
Here's an stackoverflow answer that shows how to add an image from the web:
Showing image in WPF using the URL link from database
I copied roughly a 200,000 length text to the Clipboard.
And I pasted it to RichTextBox by using shortcut key Ctrl V.
My pc couldn't load it..
But, when I pasted it to a TextBox, it worked well.
Can I ask you what problem is?
Here is my code snippet.
<RichTextBox Grid.Row="1" x:Name="에디터"
AcceptsTab="True" AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
PreviewMouseLeftButtonUp="에디터_PreviewMouseLeftButtonUp"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Margin="5"
>
<FlowDocument Name="editFlow" >
<Paragraph>
여기에 텍스트를 붙여 넣으세요
</Paragraph>
</FlowDocument>
</RichTextBox>
You cannot do it and you should not do it. WPF controls aren't designed to work with that kind of data(hence it freezes).Lets say even if you had succeded in adding that data in RTB the scrollbars would be useless as there would be very little data to display.Imagine the pains you will have when you have to locate a line in the text using scrollbar (it could take minutes...).
So the solution is you need to rethink what you are doing and maybe change your logic.
I am writing a c# wpf application that get information from rss feed. I got a string with "<'b>Bold Text<'/b> and <'i>Italic Text<'/i> and <'u>Underline text<'/u>" and need to change the font styles to the correct style when I want to use the string for a label or textbox. The output now would be like Label: <'b>Text<'/b> instead of Text being BOLD. Need help to find a way to fix it.
Let's say you got the following text:
<b>Bold</b><i>Italic</i>
You need to break the texts up and put them into WPF tags. That text in WPF must look like this:
<TextBlock>
<Run Text="Bold" FontWeight="Bold" />
<Run Text="Italic" FontStyle="Italic" />
</TextBlock>
If you google HTML to XAML convertion, you should find some API's or examples you can use:
XAML to HTML Conversion Demo
HTML-XAML Converter
I can't seem to find how to add a drop shadow to a hyperlink in a flow document. Since HyperLink is not a UIElement it has no Effect property (all my googling led me to DropShadowEffect). It does have a TextEffects collection but I can't seem to figure out how to create a drop shadow with that. Ultimately what I would like to do is add a dropshadow to the background brush not to the text itself.
My requirement is pretty vague, I just have to make the focus appearance look better (i.e. tab focus and keyboard focus, not mouse over), and I thought a light drop shadow would do the trick, but I'm open to other suggestions.
(I don't have enough rep to upload a screenshot grr so here's a simulation)
There is a HyperLink here
I would like to add a drop shadow around the gray part (light blue in my app) not the text (as mentioned before).
I am pretty sure you can't do this on a per-inline basis. The way WPF provides effects is it applies them wholesale to a single object in the visual tree. Content elements like a Hyperlink are all combined into a single visual element in their parent (the FlowDocumentReader or comparable).
FlowDocument controls do have the ability to host child visual elements using the BlockUIContainer block content element, or InlineUIContainer inline content element. You can use this to apply a DropShadow to a single Hyperlink like so:
<FlowDocument>
<Paragraph>
<Run Text="This is a" />
<InlineUIContainer>
<TextBlock Background="#FFCDCDCD">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="3" Color="#FFC9C9C9" />
</TextBlock.Effect>
<Hyperlink><Run Text="hyperlink" /></Hyperlink>
</TextBlock>
</InlineUIContainer>
</Paragraph>
</FlowDocument>
I'm programming a little Twitter Client just for fun. I have the tweet's text on a TextBlock and I want to make the URLs clickable.
I know that I have to parse the URLs with a regexp but... how I put text plus link on the TextBlock?
I can't have a string like: Hello check my blog at <Hyperlink>http://myblogurl.com</Hyperlink> because the TextBlock doesn't parse the tags.
Then, how I can have a TextBlock that maybe has a link or maybe not?
Thank you.
<RichTextBox IsDocumentEnabled="True">
<FlowDocument>
<Paragraph>
This is a richTextBox. And this is a <Hyperlink NavigateUri="http://www.microsoft.com">Hyperlink</Hyperlink>.
</Paragraph>
</FlowDocument>
</RichTextBox>
MSDN discussion
Rather than use a TextBlock, take a look at using the WPF version of the RichTextBox. It's a very flexible little critter.
Something like...
<TextBlock>
<Hyperlink Name="btnOpen" Click="btnOpen_Click">
<TextBlock Text="Click to Open" />
</Hyperlink>
</TextBlock>
You could parse the string in code behind and build up a collection of content controls, change your textblock to a wrap panel and set the children of the panel to your collection you have created.