Gle*_*rse 0 delphi with-statement
首先,我在onselect期间使用了combobox1 fill combobox2.我开始了很长的路,见下文.
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;
Run Code Online (Sandbox Code Playgroud)
我注意到你不能在内部使用with另一个with..或者我做错了.第二,我开始认为必须有一个更好的方法:D所以答案都没问题.无论是如何修复或使用更好的方法.
完全可以使用嵌套with语句.这通常不是一个好主意,因为with语句的不良性很复杂,但编译器在解释代码时没有问题.在解析标识符时,编译器只是从内部语句到外部语句,直到它找到一个具有它正在寻找的方法或属性的对象.
编译器发现的内容可能与您希望找到的内容不同.
通过使用一些变量和循环来避免重复代码,以及消除对with语句的需要,您可以使代码更加简洁.
procedure TFGetZoneDept.ComboBox1Select(Sender: TObject);
var
text1, text2: string;
i: Integer;
begin
Combobox2.Clear;
text1 := Combobox1.Text;
if text1 = '3' then
text2 := 'Zone 3 depots'
else if text1 = '4' then
text2 := 'Zone 4 depts'
else if text ='1' then
text2 := 'Zone 1 depts'
else if text ='2' then
text2 := 'Zone 2 depts'
else if text ='BoneYard' then
text2 := 'BoneYard depts'
else if text = 'Misc' then
text2 := 'Misc Depts';
for i := 1 to 6 do
Combobox2.Items.add(text2);
Combobox2.Enabled := true;
end;
Run Code Online (Sandbox Code Playgroud)
当您避免重复代码时,您也可以避免错误.除非你的意思是所有的情况都有6个选项,除了第4区,只有5个.
| 归档时间: |
|
| 查看次数: |
172 次 |
| 最近记录: |