Not able to replace string with Image in TextControl.Text in vb.net

30 Views Asked by At

I have a TextControl.Text if in that text there is any portion where it starts with *img[x-y-z] then I have to create a QR code with the value found between [] in this case x-y-z and replace the entire *img[x-y-z] with the QR code.

I managed to create the QR code found the position of *img[] and tried adding the QR image at that position. Everything works fine when I have to create 1 QR code instead of 1 *img[], however when I try to replicate the same for multiple QR codes with the help of a loop it only shows the last QR code while removing all the *img[].

What should I do to show all the QR codes in the TextControl

If I try the same without removing *img[] (Without the TextControl.Text.Remove line in loop) all the QR codes are coming along with the *img

Dim pattern As String = "\*img\[(.*?)\]"
Dim matches As MatchCollection = Regex.Matches(TextControl.Text.ToString(), pattern)

            Dim txImage As New TXTextControl.Image()
            Dim bmpimg As System.Drawing.Bitmap
            Dim firstIndex As Integer
            Dim reg As Regex = New Regex("\[([^\]]*)\]") 

            If matches.Count > 0 Then

                For Each match As Match In matches


                           Dim m As Match = reg.Match(match.Value)
                           Dim value As String = m.Groups(1).Value
           
                           firstIndex = TextControl.Text.IndexOf(match.Value)

                            bitmapimg = CreateQR(value, height, width)
                            txImage = New TXTextControl.Image(bitmapimg)
                        

                    TextControl.Text = TextControl.Text.Remove(firstIndex, match.Length)
                    TextControl.Images.Add(txImage, firstIndex)

                Next
            End If
0

There are 0 best solutions below