OVH Cloud OVH Cloud

boite de recherche txt

2 réponses
Avatar
Rosalie Mignon
¨¦tant d¨¦butante, j'aimerais savoir si VB6
mets ¨¤ disposition une boite stantard de
recherche d'occurence avec les mentions
respect de la casse, vers le haut ou le bas
etc.
merci d'avance

2 réponses

Avatar
François Picalausa
Hello,

Windows offre une boite de recherche et de remplacement toute faite au
travers des fonctions FindText et ReplaceText exportées par Comdlg32.dll
Cette méthode n'est pas simple à mettre en oeuvre (enregistrement du message
FINDMSGSTRING, sous classement du owner).

Je te conseille de créer ta propre boite de recherche à partir d'une form.

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"Rosalie Mignon" a écrit dans le message de
news:40f162e9$0$30273$
étant débutante, j'aimerais savoir si VB6
mets à disposition une boite stantard de
recherche d'occurence avec les mentions
respect de la casse, vers le haut ou le bas
etc.
merci d'avance


Avatar
François Picalausa
Hello,

voici un exemple:
'Dans une form frmFind contenant
' Une Frame fraDir
' Deux OptionButtons : optDown et optUp
' Une Checkbox : chkCase
' Deux CommandButtons : cmdCancel et cmdNext
' Un TextBox : txtFind
' Un Label : lblFind
Option Explicit

Event Find(ByVal strTextToFind As String)
Event CloseFind()
Event FindTextChange()
Event DirectionChange()
Event MatchCaseChange()

'La casse doit correspondre?
Public Property Get CaseMatch() As Boolean
CaseMatch = (chkCase.Value = vbChecked)
End Property

Public Property Let CaseMatch(NewValue As Boolean)
If NewValue Then
chkCase.Value = vbChecked
Else
chkCase.Value = vbUnchecked
End If
End Property

'Direction de la rechercher = bas?
Public Property Get DirectionDown() As Boolean
DirectionDown = optDown.Value
End Property

Public Property Let DirectionDown(NewValue As Boolean)
optDown.Value = NewValue
optUp.Value = Not NewValue
End Property

'Texte à rechercher
Public Property Let FindText(NewValue As String)
txtFind.Text = NewValue
End Property

Public Property Get FindText() As String
Find = txtFind.Text
End Property

Public Sub Find(strTextToFind As String, Optional owner As Form)
txtFind.Text = strTextToFind

'Sélectionne tout le contenu de la textbox
txtFind.SelStart = 0
txtFind.SelLength = Len(txtFind.Text)
Me.Show , owner
End Sub

Private Sub chkCase_Click()
'Changement de casse
RaiseEvent MatchCaseChange
End Sub

Private Sub cmdCancel_Click()
'Envoie l'événement de fermeture
RaiseEvent CloseFind
Unload Me
End Sub

Private Sub cmdNext_Click()
'Envoie l'événement de recherche
RaiseEvent Find(txtFind.Text)
End Sub

Private Sub Form_Load()
'Initialise les propriétés
Me.BorderStyle = 1
optDown.Value = True
cmdCancel.Cancel = True
cmdNext.Default = True
txtFind.Text = ""
lblFind.Caption = "Rechercher :"
chkCase.Value = vbUnchecked
optUp.Caption = "Haut"
optDown.Caption = "Bas"

'Initialise les positions des éléments
fraDir.Move 2400, 720, 1560, 600

Set optDown.Container = fraDir
Set optUp.Container = fraDir
optDown.Move 795, 255, 570, 270
optUp.Move 60, 240, 690, 285
chkCase.Move 75, 1050, 1935, 255
cmdCancel.Move 4050, 525, 1095, 315
cmdNext.Move 4050, 90, 1095, 315
txtFind.Move 1110, 105, 2805, 285
lblFind.Move 120, 120, 1335, 255
frmFind.Width = 5310
frmFind.Height = 2010
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
RaiseEvent CloseFind
End If
End Sub

Private Sub optDown_Click()
'Changement de direction
RaiseEvent DirectionChange
End Sub

Private Sub optUp_Click()
'Changement de direction
RaiseEvent DirectionChange
End Sub

Private Sub txtFind_Change()
'Le texte à rechercher a été modifié
RaiseEvent FindTextChange
End Sub

'------------------------------------------------
'Dans une autre form contenant un textbox, text1 et un élément de menu,
mnuSearch:
Option Explicit

Private WithEvents Find As frmFind

Private Sub Find_Find(ByVal strTextToFind As String)
'On doit rechercher l'occurence du texte renvoyé
Dim Occurence As Long
Dim TextboxText As String

If Find.CaseMatch Then
TextboxText = Text1.Text
Else
'On utilise les textes convertis en minuscules
TextboxText = LCase$(Text1.Text)
strTextToFind = LCase$(strTextToFind)
End If

If Find.DirectionDown Then
Occurence = InStr(Text1.SelStart + 2, _
TextboxText, strTextToFind) - 1
Else
If Text1.SelStart > 0 Then 'pour éviter de générer une erreur
Occurence = InStrRev(TextboxText, _
strTextToFind, Text1.SelStart) - 1
Else
Occurence = -1
End If
End If

If Occurence = -1 Then
MsgBox "Recherche terminée!"
Else
'Définit la position de la sélecition et active le textbox
Text1.SelStart = Occurence
Text1.SelLength = Len(strTextToFind)
Text1.SetFocus
End If
End Sub

Private Sub Form_Load()
Set Find = frmFind

Text1.Text = "Un très long texte dans lequel on peut tester la
fonctionnalité de recherche totalement intégrée. Cette fonctionnalité est
vraiment géniale! Enfin non, mais il me faut quand même quelques mots pour
la tester..."
End Sub

Private Sub Form_Unload(Cancel As Integer)
Unload frmFind
End Sub

Private Sub mnuSearch_Click()
'Ouvre la recherche
Find.Find Text1.SelText, Me
End Sub


--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net
"François Picalausa" a écrit dans le message de
news:O93$
Hello,

Windows offre une boite de recherche et de remplacement toute faite au
travers des fonctions FindText et ReplaceText exportées par Comdlg32.dll
Cette méthode n'est pas simple à mettre en oeuvre (enregistrement du


message
FINDMSGSTRING, sous classement du owner).

Je te conseille de créer ta propre boite de recherche à partir d'une form.

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"Rosalie Mignon" a écrit dans le message de
news:40f162e9$0$30273$
> étant débutante, j'aimerais savoir si VB6
> mets à disposition une boite stantard de
> recherche d'occurence avec les mentions
> respect de la casse, vers le haut ou le bas
> etc.
> merci d'avance