Einzelnen Beitrag anzeigen

Benutzerbild von MaBuSE
MaBuSE

Registriert seit: 23. Sep 2002
Ort: Frankfurt am Main (in der Nähe)
1.837 Beiträge
 
Delphi 10 Seattle Enterprise
 
#9

Re: Doppelte Einträge in 5 ComboBoxen herausfinden ?

  Alt 14. Dez 2007, 12:52
Zitat von HolgerCW:
Die ist identisch,
wie würdest Du den Vergleich machen, da man ja jede ComboBox gegen jede andere Vergleichen müsste ?
Eine einfache Lösung auf die Schnelle ist folgende:

(Natürlich kann man die Comboboxen auch mit FindComponent suchen, aber das hier ist ja nur ein einfaches Beispiel.
Das Füllen der Comboboxen ist nur zu Testzwecken drin. 5 Comboboxen und ein Button auf das Form und dann folgenden Quelltext einfügen.)

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    ComboBox5: TComboBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  a : Array [1..5] of TComboBox;
  sl: TStringList;
  i, j: integer;
  ok : Boolean;
begin
  // Dem Array die ComboBoxen zuweisen
  a[1] := ComboBox1;
  a[2] := ComboBox2;
  a[3] := ComboBox3;
  a[4] := ComboBox4;
  a[5] := ComboBox5;

  // nur wenn ComboBox Listen leer sind
  if a[1].Items.Count = 0 then
  begin
    // Eine Liste für alle Comboboxen erstellen
    sl := TStringList.Create;
    try
      sl.Add('Eins');
      sl.Add('Zwei');
      sl.Add('Drei');
      sl.Add('Vier');
      sl.Add('Fünf');

      // Liste allen Comboboxen zuweisen
      for i := 1 to 5 do
      begin
        a[i].Items.Assign(sl);
      end;
    finally
      sl.Free;
    end;
  end;

  // ok ist True wenn alle Inhalte unterschiedlich sind
  ok := True;

  // Comboboxen miteinander vergleichen
  for i := 1 to 5 do
  begin
    if a[i].ItemIndex > -1 then
    begin
      for j := i + 1 to 5 do
      begin
        ok := ok and (a[i].ItemIndex <> a[j].ItemIndex);
      end;
    end;
  end;

  // ok ausgeben
  if ok then Caption := 'okelse Caption := 'not ok';
end;

end.
(°¿°) MaBuSE - proud to be a DP member
(°¿°) MaBuSE - proud to be a "Rüsselmops" ;-)
  Mit Zitat antworten Zitat