[RichTextBox] [Helper] - Emoticonizer Helper Class
In the attachment is a class that can generate emoticons in a RichTextBox control, called "Emoticonizer".
Four key components are used:
Code:
Public Shared Sub Init(Optional ByVal smileyfolder As String = "smileys")
Initializes the smiley images and adds them into a HashTable. You can specify a custom folder path where the smiley images are, but that is optional.
To customize the smileys, change the Init subroutine to contain your new smiley(s):
Code:
emoticontable.Add(";)", New Bitmap(smileyfolder & "\smiley1.gif"))
Code:
Public Shared Sub Dispose()
Disposes all smiley images and clears the Emoticon Table.
This should be called when your program closes or when you no longer need emoticon support.
If you want to re-initialize, you have to call Dispose first.
Code:
Public Shared Sub Emoticonize(ByVal RTB As RichTextBox, Optional ByVal startindex As Integer = 0)
The main emoticonization code. You specify a RichTextBox control and optionally a character index to start at.
This subroutine will then store the current (selection)states and locks the control.
The text fragments that equal a smiley identifier (';)') are selected and replaced by the corresponding smiley image.
Finally it restores and unlocks the control and makes the control redraw itself.
Code:
Public Shared Sub AddMessage(ByVal RTB As RichTextBox, ByVal message As String)
Appends the message as a new line to the RichTextBox and calls Emoticonize with as starting index the start of the new message.
You can optionally add a "Buffer RichTextBox control" in the class to obtain the RTF of plain text. Example:
Code:
Public Shared Function GetRTF(ByVal text As String) As String
Dim tmpRTB As New RichTextBox
tmpRTB.Text = text
Emoticonize(tmpRTB)
GetRTF = tmpRTB.Rtf
tmpRTB.Dispose()
tmpRTB = Nothing
End Function
Finally here a site with smiley images you can test against: http://chaaarge.rattleserver.de/smilies.html
Not sure if it is copyrighted. If it is, message me and I'll remove the link from this post.
Last edited by bergerkiller; Apr 25th, 2011 at 07:01 AM.
Re: [RichTextBox] [Helper] - Emoticonizer Helper Class
Hi there. How do i call this function? i added this emoticonizer to my project in visual studio 2010. Now i'd like to send an emoticon to my richtextbox.
I have a button to send messages, and on it i have this :
Private Sub bt_enviar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_enviar.Click
Re: [RichTextBox] [Helper] - Emoticonizer Helper Class
Hi,
Luckily I got an e-mail notification, otherwise I couldn't answer. Anyhow, in your FORM_LOAD event (or some other place where your program starts) you call Emoticonizer.initialize(). Then, when you want to write a new line to the rich text box, call Emoticonizer.AddMessage(richtextbox1, yourtext) where yourtext is the raw text to append.
This should be slightly altered, because I think using text = will wipe all rich text format from the control. To solve this, you will have to 'insert' text into the control. In this case, inserting it at the end:
Notice how i used & instead of + and vbNewLine instead of VBCrlf. This has something to do with the text formatting of RTB, and & is a safer operator. (you may end up using numbers and mess things up)
I split up your 'said' message and the actual message, to prevent possible emoticon failures with the time format. (you have ): in that line, for example)