Bonjour,
Et pour un TextBox, voici un petit code.
Private Sub TextBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
MyBase.OnMouseDown(e)
'Caratère cliqué
Dim charClicked As Char = Me.TextBox1.GetCharFromPosition(e.Location)
'Index du caractère cliqué dans la chaine
Dim charIndexClicked As Integer = Me.TextBox1.GetCharIndexFromPosition(e.Location)
'Position du caractère dans le control
Dim charLocation As Point = Me.TextBox1.GetPositionFromCharIndex(charIndexClicked)
charLocation.Offset(-1, -1)
'Efface l'ancien rectangle
Me.Refresh()
'Si un caractère est cliqué
If Not Char.IsWhiteSpace(charClicked) Then
'Création du graphic du textbox
Using g As Graphics = Me.TextBox1.CreateGraphics(), sf As New StringFormat(StringFormat.GenericTypographic)
'Taille du caractère cliqué
Dim s As SizeF = g.MeasureString(charClicked, Me.TextBox1.Font, New PointF(0, 0), sf)
s.Height += 1
s.Width += 1
'Dessine le rectangle
g.DrawRectangle(New Pen(Brushes.Red), New Rectangle(charLocation, Size.Round(s)))
End Using
End If
End Sub
Il faut bien entendu que la police soit grande pour que le résultat ressemble à quelque chose.
Et ce code reste une solution simpliste qui présente un souci. A chaque fois que le TextBox se redessine, le rectangle disparaît (Exemple: Après réduction agrandissement de la fenêtre).
Mais il peut être une piste à améliorer...