• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

replacing text in a textbox with pictures

id like to make a program that converts a string to hiragana, i have all the pictures but i just need a piece of code that can replace tow or three characters at a time in a rich text box.

hitori ja nai yo

() means that its picture so it will become
(HI)(TO)(RI) (JA) (NA)(I) (YO)

kono mama, atenado nakute ii
(KO)(NO) (MA)(MA), (A)(TE)(NA)(DO) (NA)(KU)(TE) (I)(I)

as you can see it replaces the text with pictures, is this possible
  • 2
1 Solution
Richie_SimonettiIT OperationsCommented:
Yes.. could you post a list of all letter combinations that will be replaced by a picture.

There is an easy way to do this.
Rename all the pictures to the strings they represent eg. (KO) should be KO.bmp
and put them in the same directory

This code has no error handling, will work only if images exist in the given path
and if valid input strings are given in Text1

Option Explicit
Const IMAGES_DIRECTORY = "C:\images\"
Const iImageW = 32
Const iImageH = 32

Private Sub Command1_Click()
    Dim x As Integer
    Dim y As Integer
    Dim i As Integer
    Dim tempBfr As String
    x = 1
    y = iImageH  'picture height
    tempBfr = ""
    For i = 1 To Len(Text1.Text)
        tempBfr = tempBfr & Mid(Text1.Text, i, 1)
        If FileExists(IMAGES_DIRECTORY & tempBfr) Then
            Picture1.PaintPicture LoadPicture("C:\a.bmp"), x, y
            tempBfr = ""
            x = x + iImageW
            'y remains the same if you place picures side by side
        End If
End Sub

Private Function FileExists(sFile As String) As Boolean
     If UCase(Dir(sFile)) = UCase(sFile) Then
      FileExists = True
      FileExists = False
    End If
End Function
Ups! I made a mistake

the Line

 Picture1.PaintPicture LoadPicture("C:\a.bmp"), x, y

should be

 Picture1.PaintPicture LoadPicture(IMAGES_DIRECTORY & tempBfr), x, y

Took a little while to come up with a working example. In the below example, you'll need to change the following constants to test it for yourself: ImageDir, ImageExt, Image1, Image2 and Image3. You may also want to add more constants of Image* (*=#) or use an array. The below example selects the word it found an image to, and replaces it by pasting the image for it.


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_PASTE = &H302

Private Const ImageDir As String = "C:\Program Files\Microsoft Visual Studio\Common\Graphics\Bitmaps\Assorted\"
Private Const ImageExt As String = ".bmp"
Private Const Image1 As String = "club"
Private Const Image2 As String = "cup"
Private Const Image3 As String = "hand"
Private Sub Form_Load()
    RichTextBox1.Text = ""
End Sub
Private Sub RichTextBox1_Change()
    Call ConvertWords(RichTextBox1)
End Sub
Private Sub ConvertWords(ByVal RichTextBox As RichTextBox)
    Dim strArray() As String, iLoop As Integer, iStart As Integer, iEnd As Integer
    strArray = Split(RichTextBox.Text, " ")
    For iLoop = LBound(strArray) To UBound(strArray)
        iEnd = iEnd + Len(strArray(iLoop)) + IIf(iLoop < UBound(strArray), 1, 0)
        iStart = iEnd - Len(strArray(iLoop))
        Select Case LCase(strArray(iLoop))
            Case Image1
                RichTextBox.SelStart = iStart
                RichTextBox.SelLength = iEnd
                Call PasteImage(ImageDir & Image1 & ImageExt, RichTextBox.hwnd)
            Case Image2
                RichTextBox.SelStart = iStart
                RichTextBox.SelLength = iEnd
                Call PasteImage(ImageDir & Image2 & ImageExt, RichTextBox.hwnd)
            Case Image3
                RichTextBox.SelStart = iStart
                RichTextBox.SelLength = iEnd
                Call PasteImage(ImageDir & Image3 & ImageExt, RichTextBox.hwnd)
        End Select
    Next iLoop
End Sub
Private Sub PasteImage(ByVal strPath As String, ByVal lHandle As Long)
    Call Clipboard.SetData(LoadPicture(strPath))
    Call SendMessage(lHandle, WM_PASTE, 0, 0)
End Sub


If you have the folder (VS6) I used for testing, then you can test it by typing one of the Image* strings such as "club", "cup" or "hand". When you space it, the word will be replaced by the image.  This may be what you're looking for, if not, sorry. :)

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now