IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Delphi Discussion :

Delphi 7 - Tcombobox


Sujet :

Delphi

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Novembre 2015
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2015
    Messages : 63
    Points : 39
    Points
    39
    Par défaut Delphi 7 - Tcombobox
    Bonjour,

    Voici mon problème

    J'ai un tcombox qui est peuplé par une requête comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    while (not SectionsQry.Eof)
        do begin
          SectionsCmbBx.AddItem(SectionsQryDENOM.AsString+ifthen(SectionsQryOPTION.IsNull,
          '', ' '+SectionsQryOPTION.AsString) ,
            TObjString.Create(SectionsQryID_SECTION.AsString));
          SectionsQry.Next;
        end;
    Nous récupérons la valeur de l'objet comme ceci pour pousser différentes valeurs dans une table :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    IdSection := (SectionsCmbBx.Items.Objects[SectionsCmbBx.ItemIndex]
        as TObjString).Val;
    Ensuite, le but est de récupérer les données encodées et de les afficher. La question est comment puis-je faire pour me positionner sur la combobox avec la valeur encodée.
    Dans mes recherches, j'ai trouvé quelque chose comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SectionsCmbBx.ItemIndex:= SectionsCmbBx.Items.IndexOfObject(TObject(DerniersPtsUFQryID_SECTION.AsInteger));
    Voici un exemple qui sera plus visuelle

    Combobox créé par la requête :

    Indexof du combobox String du Tcombobox Object du Tcombobox
    0 section 1 100
    1 section 2 101
    2 section 3 102
    3 section 4 103


    J'ai donc réalisé l'encodage en choissisant la section 3 avec un id 102.
    Dans ma requête pour afficher les données, je récupère l'id 102. Comment faire pour positionner la combobox sur l'id 102 alors que je ne connais pas son indexof ou que celui-ci peut changer ?

    J'espère avoir été clair ^^' n'hésitez pas à poser vos questions si ce n'est pas le cas

    Merci d'avance pour votre aide.

    Valéry

  2. #2
    Expert confirmé
    Avatar de Ph. B.
    Homme Profil pro
    Freelance
    Inscrit en
    Avril 2002
    Messages
    1 784
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 784
    Points : 5 915
    Points
    5 915
    Par défaut
    Bonjour,

    Le problème vient tout simplement de la manière dont vous remplissez la "partie" Objets de votre liste déroulante, c.a.d. en y plaçant des chaines de caractères plutôt que des entiers et que vous y recherchiez ensuite un entier.
    Pour que cela fonctionne, modiffiez le code d'alimentation de votre liste déroulante comme suit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    while (not SectionsQry.Eof)
        do begin
          SectionsCmbBx.AddItem(SectionsQryDENOM.AsString + 
            IfThen(SectionsQryOPTION.IsNull, '', ' '+SectionsQryOPTION.AsString),
            TObject(SectionsQryID_SECTION.AsInteger));  // <===  ICI !
          SectionsQry.Next;
        end;
    Philippe.

  3. #3
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 459
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 459
    Points : 24 873
    Points
    24 873
    Par défaut
    Utiliser un la référence du TObject pour stocker un entier 32bits
    En Delphi 7, cela fonctionne, je l'ai fait, je ne le fait plus car c'est pas très propre, cela oblige a mettre des commentaires pour indiquer la bidouille
    En Delphi 64Bits, c'est encore plus vilain le mélange 32/64 bits


    Je me suis fait des objets standards pour cela proche de ton TObjString mais qui inclue IndexOfID

    le remplissage
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    var 
      Obj: TGenericCodeComboObject;
    begin
        while not SectionsQry.Eof do
        begin
          Obj := TGenericIDComboObject.Create(SectionsQryID_SECTION.AsInteger,
            SectionsQryDENOM.AsString + IfThen(SectionsQryOPTION.IsNull, '', ' '+ SectionsQryOPTION.AsString);
     
          SectionsCmbBx.Items.AddObjec(Obj.GenericLibelle, Obj);      
          SectionsQry.Next();
        end; 
    end;
    la recherche

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SectionsCmbBx.ItemIndex := TGenericIDComboObject.IndexOfID(SectionsCmbBx.Items, DerniersPtsUFQryID_SECTION.AsInteger);
    la récupération
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    var
      Obj: TGenericCodeComboObject;
    begin
      Obj := TGenericIDComboObject.GetSelected(SectionsCmbBx);
      IdSection := Obj.GenericID;
    end;
    et les classes


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    unit Slite.GUI.Generic;
     
    interface
     
    uses System.Classes, Vcl.StdCtrls;
     
    type
      { Forward class declarations }
      TGenericCodeComboObject = class;
      TGenericIDComboObject = class;
     
      TGenericCodeComboObject = class(TObject)
      strict private
        FGenericCode: string;
        FGenericLibelle: string;
      public
        constructor Create(const AGenericCode: string; const AGenericLibelle: string);
        property GenericCode: string read FGenericCode;
        property GenericLibelle: string read FGenericLibelle;
      public
        class function GetSelected(ACombo: TComboBox): TGenericCodeComboObject; static;
        class function IndexOfCode(AGenerics: TStrings; const AGenericCode: string): Integer; inline;
      end;
     
      TGenericIDComboObject = class(TObject)
      strict private
        FGenericID: Integer;
        FGenericLibelle: string;
      public
        constructor Create(AGenericID: Integer; const AGenericLibelle: string);
        property GenericID: Integer read FGenericID;
        property GenericLibelle: string read FGenericLibelle;
      public
        class function GetSelected(ACombo: TComboBox): TGenericIDComboObject; static;
        class function IndexOfID(AGenerics: TStrings; AGenericID: Integer): Integer; inline;
      end;
     
    implementation
     
    { TGenericCodeComboObject }
     
    //------------------------------------------------------------------------------
    constructor TGenericCodeComboObject.Create(const AGenericCode: string; const AGenericLibelle: string);
    begin
      FGenericCode := AGenericCode;
      FGenericLibelle := AGenericLibelle;
    end;
     
    //------------------------------------------------------------------------------
    class function TGenericCodeComboObject.GetSelected(ACombo: TComboBox): TGenericCodeComboObject;
    begin
      if ACombo.ItemIndex >= 0 then
        Result := TGenericCodeComboObject(ACombo.Items.Objects[ACombo.ItemIndex])
      else
        Result := nil;
    end;
     
    //------------------------------------------------------------------------------
    class function TGenericCodeComboObject.IndexOfCode(AGenerics: TStrings; const AGenericCode: string): Integer;
    begin
      for Result := 0 to AGenerics.Count - 1 do
        if Assigned(AGenerics.Objects[Result]) and (TGenericCodeComboObject(AGenerics.Objects[Result]).GenericCode = AGenericCode) then
          Exit;
     
      Result := -1;
    end;
     
    { TGenericIDComboObject }
     
    //------------------------------------------------------------------------------
    constructor TGenericIDComboObject.Create(AGenericID: Integer; const AGenericLibelle: string);
    begin
      FGenericID := AGenericID;
      FGenericLibelle := AGenericLibelle;
    end;
     
    //------------------------------------------------------------------------------
    class function TGenericIDComboObject.GetSelected(ACombo: TComboBox): TGenericIDComboObject;
    begin
      if ACombo.ItemIndex >= 0 then
        Result := TGenericIDComboObject(ACombo.Items.Objects[ACombo.ItemIndex])
      else
        Result := nil;
    end;
     
    //------------------------------------------------------------------------------
    class function TGenericIDComboObject.IndexOfID(AGenerics: TStrings; AGenericID: Integer): Integer;
    begin
      for Result := 0 to AGenerics.Count - 1 do
        if Assigned(AGenerics.Objects[Result]) and (TGenericIDComboObject(AGenerics.Objects[Result]).GenericID = AGenericID) then
          Exit;
     
      Result := -1;
    end;
     
    end.
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Novembre 2015
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2015
    Messages : 63
    Points : 39
    Points
    39
    Par défaut
    Bonjour,

    Merci à Ph. B. et ShaiLeTroll.

    Je teste vos propositions dès que possible et je reviens vers vous pour un feedback.

    Bonne journée,

    Valéry

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Novembre 2015
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2015
    Messages : 63
    Points : 39
    Points
    39
    Par défaut
    Bonjour,

    J'ai essayé la solution de Ph. B. mais cela a fait planté mon application avec une erreur mémoire.
    N'ayant pas beaucoup de temps, je suis parti sur une autre solution. Je fais une boucle for et je compare les valeurs.
    Voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
            for I := 1 to SectionsCmbBx.Items.Count do
              begin
                //On se positionne sur l'index
                SectionsCmbBx.ItemIndex:=i-1;
                //On récupère sa valeur
                v_id_section:= (SectionsCmbBx.Items.Objects[SectionsCmbBx.ItemIndex] as TObjstring).Val;
                //Comparaison avec la valeur récupérée de la DB
                if (DerniersPtsUFQryID_SECTION.asString)=v_id_section then
                begin
                  v_index:=SectionsCmbBx.ItemIndex;
                  ShowMessage(IntToStr(SectionsCmbBx.ItemIndex)+' '+v_id_section);
                end;
              end;
            //On se positionne sur l'index équivalent à la valeur de la DB
            SectionsCmbBx.ItemIndex:=v_index;
    Merci encore pour votre aide.

    A bientôt,

    Valéry

  6. #6
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 459
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 459
    Points : 24 873
    Points
    24 873
    Par défaut
    Citation Envoyé par lave_01 Voir le message
    J'ai essayé la solution de Ph. B. mais cela a fait planté mon application avec une erreur mémoire.
    Normal, la solution de Ph. B. qui lui l'utilisation de la référence du TObject comme stockage 32 Bits, ce qui est très vilain !

    Citation Envoyé par lave_01 Voir le message
    N'ayant pas beaucoup de temps, je suis parti sur une autre solution.
    Je fais une boucle for et je compare les valeurs.
    Sans t'en rendre compte, tu as fait un IndexOfID comme je l'ai proposé
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Novembre 2015
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2015
    Messages : 63
    Points : 39
    Points
    39
    Par défaut
    Merci pour ton retour ShaiLeTroll.

    quoiqu'il arrive je garde ton code sous le coude. Il me paraît bien utile

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Différences entre Delphi et Visual Basic ?
    Par Anonymous dans le forum Débats sur le développement - Le Best Of
    Réponses: 75
    Dernier message: 30/03/2009, 20h09
  2. Réponses: 1
    Dernier message: 13/05/2002, 09h19
  3. [Kylix] Migration delphi -> kylix
    Par Christian dans le forum EDI
    Réponses: 1
    Dernier message: 03/04/2002, 22h50
  4. Réponses: 4
    Dernier message: 27/03/2002, 11h03
  5. Réponses: 2
    Dernier message: 20/03/2002, 23h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo