Option Explicit Private Type POINTAPI x As Long y As Long End Type Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private mode As Byte Private chaine As String, ou As Integer Private ancx As Long, sens As Integer, pa As POINTAPI Private Sub Form_Load() chaine = "34567" RichTextBox1.Text = "aaaaa34567fffccc" mode = 0 End Sub Private Sub RichTextBox1_Change() Static pre As String If RichTextBox1.Text Like "*" & chaine & "*" Then pre RichTextBox1.Text Else RichTextBox1.Text pre End Sub 'LA SOURIS Private Sub RichTextBox1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) ancx = x End Sub Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If Button 1 Then ancx x End Sub Private Sub RichTextBox1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = 1 Then Dim pos As Integer mode = 1 pos = InStr(RichTextBox1.Text, chaine) If x < ancx Then sens -1 Else sens 1 If RichTextBox1.SelLength <= 1 And RichTextBox1.SelStart >= pos - 1 And RichTextBox1.SelStart <= pos + Len(chaine) - 1 Then If sens = 1 Then RichTextBox1.SelStart = pos - 1 DoEvents GetCursorPos pa RichTextBox1.SelLength = Len(chaine) SetCursorPos calculelongueur(Left(RichTextBox1.Text, pos + Len(chaine) + 1)) + _ ScaleX(RichTextBox1.Left, Me.ScaleMode, vbPixels) + ScaleX(Me.Left, vbTwips, vbPixels), pa.y Exit Sub End If If sens = -1 And RichTextBox1.SelLength < 1 Then RichTextBox1.SelStart = pos + Len(chaine) - 1 SendKeys "{RIGHT}" SetCursorPos calculelongueur(Left(RichTextBox1.Text, pos)) + _ ScaleX(RichTextBox1.Left, Me.ScaleMode, vbPixels) + ScaleX(Me.Left, vbTwips, vbPixels), pa.y Exit Sub End If End If mode = 1 If x < ancx Then sens -1 Else sens 1 avancons RichTextBox1, chaine, 1 End If End Sub 'LE CLAVIER Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer) If Shift And Not KeyCode = 16 Then If KeyCode 37 And RichTextBox1.SelLength 0 Then sens = -1 If KeyCode 39 And RichTextBox1.SelLength 0 Then sens = 1 If RichTextBox1.SelLength 0 And sens -1 Then ou = RichTextBox1.SelStart End If End Sub Private Sub RichTextBox1_KeyUp(KeyCode As Integer, Shift As Integer) If Shift <> 1 Then Exit Sub If mode = 0 Then Dim pos As Integer, i As Integer pos = InStr(RichTextBox1.Text, chaine) If RichTextBox1.SelStart >= pos - 1 And RichTextBox1.SelStart < pos + Len(chaine) - 2 Then If sens = 1 And RichTextBox1.SelStart >= pos - 0 Then RichTextBox1.SelStart = pos - 1 SendKeys "{RIGHT}" Else If sens = -1 And RichTextBox1.SelLength < 2 Then RichTextBox1.SelStart = pos + Len(chaine) - 1 SendKeys "{LEFT}" Exit Sub End If End If End If End If mode = 0 avancons RichTextBox1, chaine, 1 End Sub Private Sub avancons(RTB As RichTextBox, chaine As String, maj As Integer) Dim toto As String, fofo As Integer, i As Integer Static pos As Integer pos = InStr(RTB.Text, chaine) Select Case sens Case 1 If mode = 0 Then If RTB.SelStart + RTB.SelLength = pos Then simule_cle "RIGHT", Len(chaine) - 1 End If If mode = 1 Then If RTB.SelStart + RTB.SelLength = pos Then GetCursorPos pa fofo = calculelongueur(Left(RTB.Text, pos + Len(chaine))) + ScaleX(RTB.Left, Me.ScaleMode, vbPixels) + _ ScaleX(Me.Left, vbTwips, vbPixels) SetCursorPos fofo, pa.y End If End If Case -1 If mode = 0 Then If RTB.SelStart <> ou Then If RTB.SelStart = pos + Len(chaine) - 2 Then simule_cle "LEFT", Len(chaine) - 1 Else If RTB.SelStart - RTB.SelLength = pos + Len(chaine) - 2 Then mode = 99 simule_cle "LEFT", Len(chaine) - 1 mode = 0 End If End If End If If mode = 1 Then On Error Resume Next toto = Mid(RTB.Text, RTB.SelStart - Len(chaine) + 2, Len(chaine)) On Error GoTo 0 If toto = chaine Then GetCursorPos pa fofo = calculelongueur(Left(RTB.Text, pos - 1)) + ScaleX(Me.Left, vbTwips, vbPixels) + _ ScaleX(RTB.Left, Me.ScaleMode, vbPixels) SetCursorPos fofo, pa.y End If End If End Select End Sub Private Function calculelongueur(ByVal ch As String) As Long If ch = "" Then Exit Function With Me.Font .Name = RichTextBox1.Font.Name .Size = RichTextBox1.Font.Size .Bold = RichTextBox1.Font.Bold End With calculelongueur = TextWidth(ch) calculelongueur = ScaleX(calculelongueur, Me.ScaleMode, vbPixels) End Function Private Sub simule_cle(cle As String, combien As Integer) Dim i As Integer For i = 1 To combien SendKeys "{" & cle & "}" Next End Sub
Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged Dim PositionDebutSelection As Integer Dim PositionFinSelection As Integer 'Permet de détécter si le text a changer depuis l'appel précédant (évite le problème d'enchainement d'événement, SelectionChanged appeler av TextChanged) PositionDebutSelection = RichTextBox1.SelectionStart PositionFinSelection = RichTextBox1.SelectionStart + RichTextBox1.SelectionLength If RichTextBox1.Text.Contains("34567") Then Dim PositionDebut34567 As Integer Dim PositionFin34567 As Integer PositionDebut34567 = RichTextBox1.Text.IndexOf("34567") PositionFin34567 = PositionDebut34567 + "34567".Length If PositionDebutSelection > PositionDebut34567 And PositionDebutSelection < PositionFin34567 Then PositionDebutSelection = PositionDebut34567 End If If PositionFinSelection > PositionDebut34567 And PositionFinSelection < PositionFin34567 Then PositionFinSelection = PositionFin34567 End If End If RichTextBox1.SelectionStart = PositionDebutSelection RichTextBox1.SelectionLength = PositionFinSelection - PositionDebutSelection End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionou = RichTextBox1.SelectionStart
Private ou As Integer Private Sub Form_Load() RichTextBox1.Text = "aaaaa34567fff" End Sub Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) RichTextBox1.SelLength = 0 End Sub Private Sub RichTextBox1_Mouseup(Button As Integer, Shift As Integer, x As Single, y As Single) ou = RichTextBox1.SelStart End Sub Private Sub RichTextBox1_SelChange() Dim chaine As String, pos As Integer Static deja As Boolean If deja Then deja = False Exit Sub End If chaine = "34567" pos = InStr(RichTextBox1.Text, chaine) Select Case ou Case Is <= pos deja = True RichTextBox1.SelLength = Len(Mid(RichTextBox1.Text, ou, pos + Len(chaine) - ou - 1)) Case Else deja = True RichTextBox1.SelStart = pos - 1 deja = True RichTextBox1.SelLength = Abs(RichTextBox1.SelLength - pos) End Select End Sub
RichTextBox1.SelLength = Len(Mid(RichTextBox1.Text, ou, pos + Len(chaine) - ou - 1))
RichTextBox1.SelLength = Len(Mid(RichTextBox1.Text, ou, pos + Len(chaine) - ou - 1))
TextBox1.Text = "1234567890123" Dim St As String = "56789" Dim i As Integer = InStr(TextBox1.Text, St) - 1 TextBox1.Select(i, St.Length)
Private Sub RichTextBox1_SelChange() Dim chaine As String, pos As Integer Static deja As Boolean If deja Then deja = False Exit Sub End If chaine = "34567" pos = InStr(RichTextBox1.Text, chaine) deja = True Select Case ou Case Is = 0 RichTextBox1.SelLength = Len(Mid(RichTextBox1.Text, 1, pos + Len(chaine))) deja = True Case Is <= pos RichTextBox1.SelLength = Len(Mid(RichTextBox1.Text, ou, pos + Len(chaine) - ou - 1)) Case Else Dim inv As Integer inv = ou RichTextBox1.SelStart = pos - 1 deja = True RichTextBox1.SelLength = Abs(ou - pos) + Len(chaine) End Select End Sub
Alors moi... soit j'ai rien compris, soit je trouve la solution trop facile...
à commencer par l'utilisation d'une variable générale
[...]MouseDown[...]
[...]MouseUp[...]
Dans une richtexbox composée d'une chaine A, d'une chaine X et d'une chaine B (dans cet ordre), je souhaite que toute sélection qui inclurait un ou plusieurs
caractères de la chaîne X soit étendue pour englober la totalité de la chaîne X.
Et je souhaite que ce résultat soit obtenu quel que soit le sens (de gauche à droite ou de droite à gauche) de la sélection faite.
Dans une richtexbox composée d'une chaine A, d'une chaine X et d'une chaine B (dans cet ordre), je souhaite que toute sélection qui inclurait un ou plusieurs
caractères de la chaîne X soit étendue pour englober la totalité de la chaîne X.
Et je souhaite que ce résultat soit obtenu quel que soit le sens (de gauche à droite ou de droite à gauche) de la sélection faite.