Testing multiple TComboBox values at once
I'm a noob at Delphi but please help.
I have 7 TComboBoxes on a form. Their values are assigned to them using a SQL query from the same table called Numbers.
procedure TForm3.Button4Click(Sender: TObject);
begin
Q2.Close;
Q2.SQL.Clear;
Q2.SQL.Add ('Select num');
Q2.SQL.Add ('FROM numbers.dbf');
Q2.RequestLive := true;
Q2.Open;
cb1.Items.Add(q2.FieldByName('num').value);
cb1.Text:= '? ? ?';
cb2.Items.Add(q2.FieldByName('num').value);
cb2.Text:= '? ? ?';
...
...
...
end;
Where cb1, cb2.... are TComboBoxes.
I'm trying to get them to test their values (all values are text) against one another when you 开发者_Python百科click on them. Specifically, if you select cb1 = 1 from the dropdown, then if you select cb2 = 1 ...etc and you assign the same number, it should give you an error message
MessageDlg('Check Values: CB 1 and CB 2: Same Values Entered.',mtError, mbOKCancel, 0);
Which method would you recommend I use, I've been battling for two days now.
Thanks in advance!
Create a new form with seven combo boxes (with Style := csDropDownList
). Then, create a
var
combos: array[1..7] of TComboBox;
and initiate it:
procedure TForm1.FormCreate(Sender: TObject);
begin
combos[1] := ComboBox1;
combos[2] := ComboBox2;
combos[3] := ComboBox3;
combos[4] := ComboBox4;
combos[5] := ComboBox5;
combos[6] := ComboBox6;
combos[7] := ComboBox7;
end;
Then you can just do
procedure TForm1.VerifyUniqueness(Sender: TObject);
begin
if LongBool(TComboBox(Sender).Perform(CB_GETDROPPEDSTATE, 0, 0)) then
Exit;
for i := low(combos) to high(combos) do
if (Sender <> combos[i]) and SameStr(TComboBox(Sender).Text, combos[i].Text) then
raise Exception.CreateFmt('The fields %s and %s have the same value.', [TComboBox(Sender).Name, combos[i].Name]);
end;
and assign VerifyUniqueness
to the OnChange
event of each combo box. In addition, you need
procedure TForm1.ComboBoxesKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then VerifyUniquness(Sender);
end;
Validating each time a Combo Box's value changes does lead to a small inconvenience. If you want to swap the values of two Combo Boxes, you have to do so in a roundabout way.
- Set the first to a temporary third value.
- Set the second to the original first value.
- Set the first to the original second value.
The following code provides a means to validate all Combo Boxes at any point in time. E.g. When the user clicks the Ok button. Of course, you could still call the method each time a value changes.
The code also uses an array of combo boxes as with Andreas Rejbrand's answer, so I'll not repeat that portion of the code.
procedure ValidateComboBoxes;
var
LCombValues: TStrings;
I: Integer;
LDuplicateIndex: Integer;
begin
LComboValues := TStringList.Create;
try
for I := Low(FCombos) to High(FCombos) do
begin
LDuplicateIndex := LComboValues.IndexOf(FCombos[I].Text);
if (LDuplicateIndex >= 0) then
begin
raise Exception.Create('The value: ['+FCombos[I].Text+
'] has been duplicated in the following Combo Boxes: ['+FCombos[I].Name+
'] and ['+TComboBox(LComboValues.Objects[LDuplicateIndex]).Name+']');
end;
LComboValues.AddObject(FCombos[I].Text, FCombos[I]);
end;
finally
LComboValues.Destroy;
end;
end;
精彩评论