Results 1 to 3 of 3

Thread: [RichTextBox] [Helper] - Emoticonizer Helper Class

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009

    [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:
    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):
    emoticontable.Add(";)", New Bitmap(smileyfolder & "\smiley1.gif"))
    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.

    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.

    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:
        Public Shared Function GetRTF(ByVal text As String) As String
            Dim tmpRTB As New RichTextBox
            tmpRTB.Text = text
            GetRTF = tmpRTB.Rtf
            tmpRTB = Nothing
        End Function
    Finally here a site with smiley images you can test against:
    Not sure if it is copyrighted. If it is, message me and I'll remove the link from this post.
    Attached Files Attached Files

  2. #2
    New Member
    Join Date
    Oct 2012

    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

    If SerialPort1.IsOpen = True Then
    SerialPort1.Write("Msg:" + rtf_escrita.Text + vbCrLf)
    rtf_leitura.Text = rtf_leitura.Text + "Joao Says" + " " + "(" + Format(Now, "HH:mm:ss") + "):" + vbCrLf + rtf_escrita.Text + vbCrLf + vbCrLf

    rtf_escrita.Text = ""
    ElseIf SerialPort1.IsOpen = False Then
    MsgBox("No COM port detected.", , "Erro:")
    End If

    End Sub

    Where should i call emoticonizer and how só my richtextbox detects and automaticaly changes "" to emoticon?

    Thanks in advance.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009

    Re: [RichTextBox] [Helper] - Emoticonizer Helper Class


    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.

    You have this code:

    rtf_leitura.Text = rtf_leitura.Text + "Joao Says" + " " + "(" + Format(Now, "HH:mm:ss") + "):" + vbCrLf + rtf_escrita.Text + vbCrLf + vbCrLf

    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:

    rtf_leitura.Select(rtf_leitura.TextLength, 0)
    rtf_leitura.SelectedText = "Joao Says" & " " & "(" & Format(Now, "HH:mm:ss") & "):" & vbNewLine
    Emoticonizer.AddMessage(rtf_leitura, rtf_escrita.Text & vbNewLine & vbNewLine)

    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)

    Hope this answers your question

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width