0

まず、onselect 中にコンボボックス 1 でコンボボックス 2 を埋めていました。私は長い道のりを始めました。以下を参照してください。

    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;

with内部で別のものを使用できないことに気付きましたwith..または私はそれを間違っています. 次に、もっと良い方法が必要だと思い始めました:Dだから、どちらの答えでも構いません。で修正する方法、またはこれをより良い方法で行う方法。

4

2 に答える 2

8

ネストされた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;

コードの繰り返しを避けると、間違いも避けられます。5 つしかないゾーン 4を除いて、すべてのケースに 6 つのオプションがあることを意図していない限り.

于 2013-02-08T04:44:35.117 に答える
3

ネストされた With ステートメントを使用している場合は、時間を節約するために顔を殴ってください。

人々は、GOTO と WITH ステートメントについて強い議論をします。WITH ステートメントは入れ子にすることができるため、使用するたびにその悪さを悪化させます。

With の最初のルールは、誰もそれについて話さない (または使用しない) ことです。

于 2013-02-08T04:58:45.753 に答える