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 :

Problèmes avec le composant TComboBox


Sujet :

Delphi

  1. #1
    Membre expert
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Octobre 2013
    Messages
    1 563
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2013
    Messages : 1 563
    Points : 3 404
    Points
    3 404
    Par défaut Problèmes avec le composant TComboBox
    Bonjour bonjour !

    J'ai un petit soucis avec une ComboBox...

    Pour commencer, voici le code me posant problème. Je le commente par la suite afin d'expliquer mon soucis.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
        CB_NomEtiquette.Text := wNomETQ;
    CB_NomEtiquette est une ComboBox remplis juste avant. wNomETQ est un String correspondant à un item de la CB. Le problème : la ligne est sans effet. CB_NomEtiquette.Text reste vide... A savoir que le style de la CB est csOwnerDrawFixed : non éditable par l'utilisateur, mais éditable par programme il me semble.

    Si vous avez des idées, je suis tout ouïe

    Merci par avance,

    ZenZiTone.

  2. #2
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 662
    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 662
    Points : 25 450
    Points
    25 450
    Par défaut
    Utilise ItemIndex := Items.IndexOf(...) c'est je crois obligatoire pour une ComboBox en mode sélection stricte !

    Code simple et fonctionnel
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    CB_NomEtiquette.ItemIndex := CB_NomEtiquette.Items.IndexOf(wNomETQ);
    Si tu es curieux la suite évoque l'utilisation de Objects[], chaque Items[] à son propre Objects[]

    Nom : Combo.jpg
Affichages : 154
Taille : 28,4 Ko

    J'évite normalement la recherche sur le texte
    j'utilise massivement Objects[] pour stocker les identifiants, c'est ce que je fais à défaut d'avoir trouver mieux !


    Un bouton avec juste une petite gomme permet d'effacer le contenu du Poste ou rétablir une valeur par défaut (c'est une Combo de Poste)
    J'ai prévu un item spécifique pour cela (plus lisible pour l'utilisateur qu'un ItemIndex à -1

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    //------------------------------------------------------------------------------
    procedure TModuleCalculAlterationForm.btnAlterationPosteIsNullClick(Sender: TObject);
    begin
      cbxAlterationPoste.ItemIndex := TPosteComboObject.IndexOf(cbxAlterationPoste, 0);
      cbxAlterationPosteChange(nil);  // OnChange d'un ComboBox ne se produit que lors de la modification utilisateur
    end;
     
    //------------------------------------------------------------------------------
    procedure TModulePrimesCalculAlterationForm.btnAlterationPosteUndoClick(Sender: TObject);
    begin
      cbxAlterationPoste.ItemIndex := TPosteComboObject.IndexOf(cbxAlterationPoste, -1);
      cbxAlterationPosteChange(nil); // OnChange d'un ComboBox ne se produit que lors de la modification utilisateur
    end;
    Le code dans le Constructeur chargeant données et combo :

    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
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
     
    //------------------------------------------------------------------------------
    procedure TModuleCalculAlterationForm.ShowAlterationPoste();
     
      procedure InitCombo();
      var
        PosteDico: TEntityPoste;
        PosteObj: TPosteComboObject;
      begin
        // Récupération de la liste des Postes
        PosteDico := TEntityPoste.CreateAsSet();
        try
          PosteDico.Connection := TModule.Instance.DBConnection;
          if PosteDico.Load() then
          begin
            PosteDico.First();
            while not PosteDico.EOF do
            begin
              with TProfilBaseTypeParPoste.Create() do
              try
                Connection := TModulePrimes.Instance.DBConnection;
     
                if Load(TBusinessProfilBaseTypeParPoste.MakeCriteriaByProfilAndPoste(FCalculView.Calculator.ProfilID, PosteDico.PosteID.AsInteger)) then
                begin
                  PosteObj := TPosteComboObject.Create(PosteID.AsInteger, PosteDico.PosteCode);
                  cbxAlterationPoste.Items.AddObject(PosteObj.PosteLibelle, PosteObj);
                end;
              finally
                Free();
              end;
     
              PosteDico.Next();
            end;
          end;
        finally
          PosteDico.Free();
        end;
     
        PosteObj := TPosteComboObject.Create(0, 'Poste ignoré');
        cbxAlterationPoste.Items.AddObject(PosteObj.PosteLibelle, PosteObj);
     
        PosteObj := TPosteComboObject.Create(-1, 'Rétablir poste');
        cbxAlterationPoste.Items.AddObject(PosteObj.PosteLibelle, PosteObj);
      end;
     
    var
      PosteCalculated, Poste: Integer;
    begin
      // Remplissage de la combo !
      InitCombo();
     
      PosteCalculated := FCalculView.Calcul.PosteIDTheorique.TryAsVariant();
      Poste := FCalculView.Calcul.PosteIDFinal.TryAsVariant();
     
      if PosteCalculated > 0 then
        lblPosteCalculated.Caption := 'Poste : ' + FCalculView.Calcul.CodePoste
      else
        lblPosteCalculated.Caption := 'Poste indéterminé';
     
      if not FAlterationLoaded then
        FAlterationLoaded := FCalculAlteration.Load(TBusinessCalculAlteration.MakeCriteriaByCalcul(FCalculView.Calcul));
     
      if Poste <> PosteCalculated then
      begin
        // Si le Poste utilisé en finalité est différent du Poste calculé,
        // c'est qu'il y a nécessairement une altération !
        if FAlterationLoaded then
        begin
          if Poste > 0 then
          begin
            cbxAlterationPoste.ItemIndex := TPosteComboObject.IndexOf(cbxAlterationPoste, Poste);
            lblPosteAlteration.Caption := 'Poste forcé : ' + TPosteComboObject.GetSelected(cbxAlterationPoste).PosteLibelle;
          end
          else
          begin
            lblPosteAlteration.Caption := 'Poste ignoré';
            cbxAlterationPoste.ItemIndex := TPosteComboObject.IndexOf(cbxAlterationPoste, 0);
          end;
     
          if FCalculAlteration.PosteAltered then
            lblPosteAlteration.Font.Style := [fsBold]
          else
            lblPosteAlteration.Font.Style := [fsStrikeOut];
        end
        else
          raise EModuleCalculatorError.Create(ERR_INCOHERENT_CALCUL_ALTERATION);
      end
      else
      begin
        lblPosteAlteration.Caption := '';
        cbxAlterationPoste.ItemIndex := -1;
      end;
    end;
     
    //------------------------------------------------------------------------------
    procedure TModulePrimesCalculAlterationForm.cbxAlterationPosteChange(Sender: TObject);
    var
      PosteObj: TPosteComboObject;
      PosteCalculated, PosteOld, PosteNew: Integer;
    begin
      // OnChange d'un ComboBox ne se produit que lors de la modification utilisateur
      PosteObj := TPosteComboObject.GetSelected(cbxAlterationPoste);
      if Assigned(PosteObj) then
      begin
        // une option pour rétablir l'ancienne valeur
        if PosteObj.PosteID = -1 then
        begin
          FCalculAlteration.Edit();
          FCalculAlteration.PosteAltered := False;
          PreviewAlterationPoste();
        end
        // une option pour forcer une valeur vide
        else if PosteObj.PosteID = 0 then
        begin
          FCalculAlteration.Edit();
          FCalculAlteration.PosteAltered := not FCalculView.Calcul.PosteIDTheorique.IsNull;
          FCalculAlteration.Poste.IsNull := True;
          PreviewAlterationPoste();
        end
        else
        begin
          PosteCalculated := FCalculView.Calcul.PosteIDTheorique.TryAsVariant();
          if FAlterationLoaded then
            PosteOld := FCalculAlteration.Poste.TryAsVariant()
          else
            PosteOld := FCalculView.Calcul.PosteIDPourPrime.TryAsVariant();
     
          PosteNew := PosteObj.PosteID;
          if PosteNew <> PosteOld then
          begin
            FCalculAlteration.Edit();
            if PosteNew <> PosteCalculated then
            begin
              FAlterationLoaded := True;
              FCalculAlteration.Poste.AsInteger := PosteNew;
              FCalculAlteration.PosteAltered := True;
            end
            else
              FCalculAlteration.PosteAltered := False;
     
            PreviewAlterationPoste();
          end;
        end;
      end;
    end;

    Je me fais en partie implémentation un objet d'aide GUI dans les données issus d'une Entité DB et la Combo

    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
      TPosteComboObject = class
      strict private
        FPosteID: Integer;
        FPosteLibelle: string;
      public
        constructor Create(const APosteID: Integer; const APosteLibelle: string);
        property PosteID: Integer read FPosteID;
        property PosteLibelle: string read FPosteLibelle;
      public
        class function IndexOf(ACombo: TComboBox; APosteID: Integer): Integer; inline;
        class function GetSelected(ACombo: TComboBox): TPosteComboObject; inline;
      end;
     
    { TPosteComboObject }
     
    //------------------------------------------------------------------------------
    constructor TPosteComboObject.Create(const APosteID: Integer; const APosteLibelle: string);
    begin
      FPosteID := APosteID;
      FPosteLibelle := APosteLibelle;
    end;
     
    //------------------------------------------------------------------------------
    class function TPosteComboObject.GetSelected(ACombo: TComboBox): TPosteComboObject;
    begin
      if ACombo.ItemIndex >= 0 then
        Result := TPosteComboObject(ACombo.Items.Objects[ACombo.ItemIndex])
      else
        Result := nil;
    end;
     
    //------------------------------------------------------------------------------
    class function TPosteComboObject.IndexOf(ACombo: TComboBox; APosteID: Integer): Integer;
    begin
      for Result := 0 to ACombo.Items.Count - 1 do
        if TPosteComboObject(ACombo.Items.Objects[Result]).PosteID = APosteID then
            Exit;
     
      Result := -1;
    end;

Discussions similaires

  1. Problème avec le composant JScrollPane
    Par vidocq dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 04/04/2006, 09h02
  2. Problème avec un composant fait maison
    Par gandf dans le forum C++Builder
    Réponses: 6
    Dernier message: 17/07/2005, 13h30
  3. problème avec le composant TSQLQuery
    Par vbcasimir dans le forum Bases de données
    Réponses: 6
    Dernier message: 31/05/2005, 16h45
  4. Problème avec le composant TXMLDocument
    Par kurul1 dans le forum C++Builder
    Réponses: 9
    Dernier message: 14/04/2005, 15h36
  5. Problème avec des composants non déclarés
    Par vbcasimir dans le forum Bases de données
    Réponses: 1
    Dernier message: 20/01/2005, 11h17

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