First, I was haveing combobox1 fill combobox2 during the onselect. I started the long way see below.
procedure TFGetZoneDept.ComboBox1Select(Sender: TObject);
begin
Combobox2.Clear;
with Combobox1 do
begin
if text = '3' then
begin
with combobox2 do
begin
Add('Zone 3 depts');
Add('Zone 3 depts');
Add('Zone 3 depts');
Add('Zone 3 depts');
Add('Zone 3 depts');
Add('Zone 3 depts');
end; {with combobox2}
end; {If }
if text = '4' then
begin
with ComboBox2 do
begin
add('Zone 4 depts');
add('Zone 4 depts');
add('Zone 4 depts');
add('Zone 4 depts');
add('Zone 4 depts)';
end;{combobox2 with}
end;{IF}
if text ='1' then
begin
with ComboBox2 do
begin
add('Zone 1 depts');
add('Zone 1 depts');
add('Zone 1 depts');
add('Zone 1 depts');
add('Zone 1 depts');
add('Zone 1 depts');
end; {combobox2 with}
end; {IF}
if text ='2' then
begin
with ComboBox2 do
begin
add('Zone 2 depts');
add('Zone 2 depts');
add('Zone 2 depts');
add('Zone 2 depts');
add('Zone 2 depts');
add('Zone 2 depts');
end; {Combobox2 with}
end; {IF}
if text ='BoneYard' then
begin
with ComboBox2 do
begin
add('BoneYard depts');
add('BoneYard depts');
add('BoneYard depts');
add('BoneYard depts');
add('BoneYard depts');
add('BoneYard depts');
end; {combobox2 with}
end; {IF}
if text = 'Misc' then
begin
with ComboBox2 do
begin
add('Misc Depts');
add('Misc Depts');
add('Misc Depts');
add('Misc Depts');
add('Misc Depts');
add('Misc Depts');
end; {combobox2 with}
end; {IF}
end;{combobox1 with}
Combobox2.Enabled := true;
end;
I noticed you cant use one with with another with inside.. or am i doing it wrong. Second I started to think there has to be a better way 😀 So either answer is ok. Either how to fix the with or do this a better way.
It’s entirely possible to have nested
withstatements. It’s generally not a good idea, since the badness ofwithstatements compounds, but the compiler has no trouble interpreting the code. When resolving identifiers, the compiler simply works its way from the inner statement to the outer one until it finds an object that has the method or property it’s looking for.What the compiler finds may differ from what you expect it to find.
You can make your code considerably more concise by using some variables and loops to avoid repeating code, as well as to remove the need for a
withstatement.When you avoid repeating code, you also avoid mistakes. Unless you meant for all the cases to have six options except zone 4, which only has five.