Les Snippets

Connexion

Récupérer la position d'une chaîne dans une combobox ou une listbox (et sélectionner cette ligne)

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 21/08/2007 08:53:30 et initié par PCPT [Liste]
Date de mise à jour : 22/08/2007 14:05:55
Vue : 3831
Catégorie(s) : Chaîne de caractères, Control
Langages dispo pour ce code :
- VB6, VBA
- VB6, VBA
- Delphi 5



Langage : VB6 , VBA
Date ajout : 21/08/2007
Posté par PCPT [Liste]
DateMAJ : 21/08/2007
Function GetListIndex(oObj As Object, sValue As String) As Long
    GetListIndex = -1
    Dim As Long
    If Not oObj Is Nothing Then
        If (TypeOf oObj Is ComboBox) Or (TypeOf  oObj Is ListBox) Then
            If oObj.ListCount > Then
                For i = To oObj.ListCount - 1
                    If oObj.List(i) = sValue Then GetListIndex = i: Exit For
                Next i
            End If
        End If
    End If
End Function

'   EXEMPLE  D'UTILISATION
'    =====================
Private Sub Form_Load()
    Combo1.Clear
    Combo1.AddItem "Masculin"
    Combo1.AddItem "Féminin"
    Combo1.ListIndex = GetListIndex(Combo1, "Féminin")
    
    List1.Clear
    List1.AddItem "Masculin"
    List1.AddItem "Féminin"
    List1.ListIndex = GetListIndex(List1, "Féminin")
End Sub

Remarque :
possible qu'il faille modifier le TypeOf pour VBA...
Langage : VB6 , VBA
Date ajout : 22/08/2007
Posté par Renfield [Liste]
DateMAJ : 22/08/2007
Private Const CB_FINDSTRING As Long = &H14C
Private Const CB_FINDSTRINGEXACT As Long = &H158
Private Const LB_FINDSTRING As Long = &H18F
Private Const LB_FINDSTRINGEXACT As Long = &H1A2
  
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
   
Function GetListIndex(ByRef voObj As Object, ByRef vsValue As String, Optional ByVal vbBeginsWith As Boolean = False, Optional vnStart As Long = -1) As Long
Dim eMsg As Long
   If Not voObj Is Nothing Then
       If (TypeOf voObj Is ComboBox) Then
           If vbBeginsWith Then
               eMsg = CB_FINDSTRING
           Else
               eMsg = CB_FINDSTRINGEXACT
           End If
       ElseIf (TypeOf voObj Is ListBox) Then
           If vbBeginsWith Then
               eMsg = LB_FINDSTRING
           Else
               eMsg = LB_FINDSTRINGEXACT
           End If
       Else
           Err.Raise 5
           Exit Function
       End If
   
       GetListIndex = SendMessage(voObj.hwnd, eMsg, vnStart, ByVal vsValue)
   End If
End Function 
Remarque :
Possibilité de rechercher les mots commençant par, et de spécifier l'index de départ utilisé pour la recherche.
Langage : Delphi 5
Date ajout : 26/08/2007
Posté par f0xi [Liste]


ListBoxX.ItemIndex := ListBoxX.Items.IndexOf('un truc');
 ComboBoxX.ItemIndex := ComboBoxX.Items.IndexOf('un truc');
 


Snippets en rapport avec : Chaine, Listbox, Trouver, Combobox, Sélectionner



Codes sources en rapport avec : Chaine, Listbox, Trouver, Combobox, Sélectionner

{Delphi} TRI DANS LISTBOX/COMBOBOX (TSTRINGS)
D' après une question sur le forum : http://www.delphifr.com/infomsg_TRI-NUMERIQUE-SUR-LISTBOX_1050...

{Flash} COMBOBOX OU LISTBOX
Suite à une compoxbox pas commentée et peu compréhensible sur ce site alors que le principe était ut...

{Visual Basic, VB6, VB.NET, VB 2005} DLL CONTENANT LES OBJETS LISTBOX ET COMBOBOX MULTICOLONNES
J'avais créé cette DLL en VB 2003 la voilà en VB 2005 suite à une demande récente. L'exemple traite...

{Delphi} BARRE DE SÉLECTION GRAPHIQUE
Exemples de barres de sélection graphique sur ComboBox/ListBox/StringGrid/Menu/PopupMenu...

{C / C++ / C++.NET} CRÉE TRÈS FACILEMENT, DES COMBOBOX, LISTBOX, PROGRESSBAR... AVEC OBJECT
Permet de manipuler des objects graphiques (win32), comme: -les combobox -les listbox -les progre...

{Visual Basic, VB6, VB.NET, VB 2005} UTILISATION DE L'API SENDMESSAGE AVEC UNE LISTBOX OU UN COMBOBOX
Voici une liste d'exemples de l'API SendMessage dans le cadre d'une utilisation avec une ListBox ou ...

{ASP / ASP.NET} TROUVER UNE CHAINE ET LA REMPLACER
Trouve str1 dans la chaine s et le remplace par str2 inputs : s, str1, str2 au format string ...

{Visual Basic, VB6, VB.NET, VB 2005} SAUVEGARDER UNE LISTBOX OU UNE COMBOBOX DANS UN FICHIER (SAUVER ET OUVRIR)
Faites un Form(Form1) avec 2 Bouton(Command1 et Command2), un ListBox ou ComboBox ...

{C / C++ / C++.NET} HMEDIAV2 LECTEUR (WIN32)
C'est un Lecteur Audio-Video qui utilise l'API "vfw" pour lire et controler le video et l'API FM...

{Visual Basic, VB6, VB.NET, VB 2005} EXTRAIRE LE TEXTE DES FENETRES D'APPLICATIONS TIERCES (LISTBOX, LISTVIEW, TEXTBOXES, ...)
une mini source mais assez utile, dans certains cas... cas concrêt: j'ai une appli au boulot qui...