Click here to Skip to main content
15,894,343 members

Comments by 10071962 (Top 40 by date)

10071962 3 days ago View    
You're right, sorry, I'll keep looking... Thanks!
10071962 3 days ago View    
First of all, I thank you for the time you spent and the effort you made, I attach the new derived class, completely empty, that I created to test your code, avoiding possible interference, and I inserted it in a form that is also empty , the problem is that it doesn't work for me, the hand appears randomly in the control area even though the mouse coordinates are correct and the image inserted correctly in the "imageLocations", I don't want to have done something wrong. I also tried with the Windows API (IRichEditOle Interface) but it doesn't work, yet for a relatively simple thing there is no direct call to the RichtextBox which is unnerving. Thanks again and happy Sunday. NOTE: I attach the class as an answer, I can't do it here, maybe the text is too long.
10071962 3 days ago View    
Deleted
First of all, I thank you for the time you spent and the effort you made, I attach the new derived class, completely empty, that I created to test your code, avoiding possible interference, and I inserted it in a form that is also empty , the problem is that it doesn't work for me, the hand appears randomly in the control area even though the mouse coordinates are correct and the image inserted correctly in the "imageLocations", I don't want to have done something wrong. I also tried with the Windows API (IRichEditOle Interface) but it doesn't work, yet for a relatively simple thing there is no direct call to the RichtextBox which is unnerving. Thanks again and happy Sunday.

Option Explicit On
Option Strict Off

Imports System.Windows

Public Class ExtendedRichTextBox : Inherits RichTextBox

Private imageLocations As List(Of Tuple(Of Integer, Integer))

Public Sub New()
MyBase.New()
imageLocations = New List(Of Tuple(Of Integer, Integer))
End Sub

'*****************************************************************************************
'*****************************************************************************************

Public Sub Me_InsertImage(ImagePath As String)

If Not System.IO.File.Exists(ImagePath) Then
Dim parentForm As Form = Me.FindForm()
Forms.MessageBox.Show(parentForm, "Il percorso dell'immagine specificato non esiste.", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If

Dim img As Image = Image.FromFile(ImagePath)

Forms.Clipboard.SetImage(img)
Me.Paste()

End Sub

'*****************************************************************************************
'*****************************************************************************************

Protected Overrides Sub OnTextChanged(e As EventArgs)
MyBase.OnTextChanged(e)
If Me.IsHandleCreated Then
FindImageLocations()
End If
End Sub

Private Function FindImageLocations() As Boolean
imageLocations.Clear()

Dim startTag As String = "{\pict"
Dim openBrace As Char = "{"c
Dim closeBrace As Char = "}"c
Dim braceCount As Integer
Dim currentPosition As Integer
Dim nextOpenBrace As Integer
Dim nextCloseBrace As Integer

Dim currentImageStart As Integer = Me.Rtf.IndexOf(startTag, 0)
If currentImageStart = -1 Then Return False

While currentImageStart <> -1
braceCount = 1
currentPosition = currentImageStart + startTag.Length
While braceCount <> 0
nextOpenBrace = Me.Rtf.IndexOf(openBrace, currentPosition)
nextCloseBrace = Me.Rtf.IndexOf(closeBrace, currentPosition)
If nextOpenBrace <> -1 Then
currentPosition = Math.Min(nextOpenBrace, nextCloseBrace)
Else
currentPosition = nextCloseBrace
End If
If Me.Rtf.Chars(currentPosition) = openBrace Then
braceCount += 1
ElseIf Me.Rtf.Chars(currentPosition) = closeBrace Then
braceCount -= 1
Else
Throw New Exception("Unexpected error")
End If
currentPosition += 1
End While
imageLocations.Add(Tuple.Create(currentImageStart, currentPosition))
My.Forms.Form_Di_Test_09_ExtendedRichTextBox.ToolStripStatusLabel_Start.Text = "Start, " & currentImageStart.ToString()
My.Forms.Form_Di_Test_09_ExtendedRichTextBox.ToolStripStatusLabel_End.Text = "End, " & currentPosition.ToString()
currentImageStart = Me.Rtf.IndexOf(startTag, currentPosition)
End While
Return True
End Function

Prot
10071962 3 days ago View    
Deleted
First of all, I thank you for the time you spent and the effort you made, I attach the new derived class, completely empty, that I created to test your code, avoiding possible interference, and I inserted it in a form that is also empty , the problem is that it doesn't work for me, the hand appears randomly in the control area even though the mouse coordinates are correct and the image inserted correctly in the "imageLocations", I don't want to have done something wrong. I also tried with the Windows API (IRichEditOle Interface) but it doesn't work, yet for a relatively simple thing there is no direct call to the RichtextBox which is unnerving. Thanks again and happy Sunday.

Option Explicit On
Option Strict Off

Imports System.Windows

Public Class ExtendedRichTextBox : Inherits RichTextBox

Private imageLocations As List(Of Tuple(Of Integer, Integer))

Public Sub New()
MyBase.New()
imageLocations = New List(Of Tuple(Of Integer, Integer))
End Sub

'*****************************************************************************************
'*****************************************************************************************

Public Sub Me_InsertImage(ImagePath As String)

If Not System.IO.File.Exists(ImagePath) Then
Dim parentForm As Form = Me.FindForm()
Forms.MessageBox.Show(parentForm, "Il percorso dell'immagine specificato non esiste.", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If

Dim img As Image = Image.FromFile(ImagePath)

Forms.Clipboard.SetImage(img)
Me.Paste()

End Sub

'*****************************************************************************************
'*****************************************************************************************

Protected Overrides Sub OnTextChanged(e As EventArgs)
MyBase.OnTextChanged(e)
If Me.IsHandleCreated Then
FindImageLocations()
End If
End Sub

Private Function FindImageLocations() As Boolean
imageLocations.Clear()

Dim startTag As String = "{\pict"
Dim openBrace As Char = "{"c
Dim closeBrace As Char = "}"c
Dim braceCount As Integer
Dim currentPosition As Integer
Dim nextOpenBrace As Integer
Dim nextCloseBrace As Integer

Dim currentImageStart As Integer = Me.Rtf.IndexOf(startTag, 0)
If currentImageStart = -1 Then Return False

While currentImageStart <> -1
braceCount = 1
currentPosition = currentImageStart + startTag.Length
While braceCount <> 0
nextOpenBrace = Me.Rtf.IndexOf(openBrace, currentPosition)
nextCloseBrace = Me.Rtf.IndexOf(closeBrace, currentPosition)
If nextOpenBrace <> -1 Then
currentPosition = Math.Min(nextOpenBrace, nextCloseBrace)
Else
currentPosition = nextCloseBrace
End If
If Me.Rtf.Chars(currentPosition) = openBrace Then
braceCount += 1
ElseIf Me.Rtf.Chars(currentPosition) = closeBrace Then
braceCount -= 1
Else
Throw New Exception("Unexpected error")
End If
currentPosition += 1
End While
imageLocations.Add(Tuple.Create(currentImageStart, currentPosition))
My.Forms.Form_Di_Test_09_ExtendedRichTextBox.ToolStripStatusLabel_Start.Text = "Start, " & currentImageStart.ToString()
My.Forms.Form_Di_Test_09_ExtendedRichTextBox.ToolStripStatusLabel_End.Text = "End, " & currentPosition.ToString()
currentImageStart = Me.Rtf.IndexOf(startTag, currentPosition)
End While
Return True
End Function

Prot
10071962 29-Apr-24 8:59am View    
Thank you, but I had tried, also replacing Dim imageIndex As Integer = text.IndexOf("\pict\wmetafile8"), the RTF code is "{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\ fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17\par
{\pict\wmetafile8\picw3386\pich3386\picwgoal1920\pichgoal1920
010009000003386000000000226000000000050000000b0200000000050000000c023a0d3a0d22" etc... The nice thing is that I created a new project, with an empty derived class but it doesn't want to know about it..