2014-10-07 7 views
0

로 다스 려 할 때 카운터를 사용하는 방법을 내가 할 노력하고있어 무엇을,특정 편집 상자 여기

문제가
for y:=1 to 8 do 
    begin 
    if edittexty.text = '1' then 
     input1[y]:=true 
    else 
     input1[y]:=false; 

는, 델파이는 전체 변수

모든 I로 edity.text 인식 원하는 것은 8 개의 editbox를 1과 비교하고 배열 값을 true/false로 설정하는 것입니다. 나는 edit1.text, edit2.text 등을 작성하는 것을 피하기 위해 전체 프로세스를 반복하려고합니다. 어떤 아이디어입니까?

도움 주셔서 감사합니다.

+0

많은 방법이 있습니다. 어떤 버전의 델파이? –

+1

편집 컨트롤을 배열에 넣기 –

답변

1
var 
    LComponent : TComponent; 
begin 
    for y:=1 to 8 do 
    begin 
    LComponent := Self.FindComponent('edit' + IntToStr(y)); 
    if (not Assigned(LComponent)) or (not (LComponent is TCustomEdit)) then 
     continue; 

    input1[y] := (LComponent as TCustomEdit).Text = '1'; 
    end; 
end; 

여기에서 Self은 모든 수정 사항을 보유하는 양식입니다.

+0

'if (LComponent <> nil) 및 (LComponent는 TCustomEdit입니다.) '및'계속 '을 사용하지 않고. 그것은 그렇게 더 짧습니다. –

+0

이 솔루션은 CPU 시간 (메모리와 비교할 때)에 메모리를 사용하지만 메모리를 사용하는 것을 선호하지만 이는 맛의 문제이며 8 개의 컨트롤과 눈에 띄지 않는 차이를 만들지는 못합니다. –

+0

'if' 문이 어떻게 행해지는지는 개인적인 취향입니다. 필자가 선호하는 것은 어떻게 처리 했는가이다. (여분의 줄을 추가하는 것처럼)'begin' /'end' 줄에 줄을 써 넣을 필요가 없다. –

4

TList<T> 사용 : (이 또한 새로운 델파이 버전에 TArray<T>과 같이 쓸 수있다) array of T를 사용

var 
    Edits : TList; 
    I  : Integer; 
begin 
    Edits := TList.Create; 
    try 
    Edits.Add(Edit1); 
    Edits.Add(Edit2); 
    // etc. 
    // You must be careful with array bounds here, Inputs and Edits must have the same length 
    Assert(Length(Inputs) = Length(Edits)); 
    for I := 0 to Edits.Count - 1 do 
     Inputs[I] := TEdit(Edits[I]).Text = '1'; 
    finally 
    Edits.Free; 
    end; 
end; 

: 제네릭없이 이전 델파이 버전

uses 
    System.Generic.Collections; 

var 
    Edits : TList<TEdit>; 
    I  : Integer; 
begin 
    Edits := TList<TEdit>.Create; 
    try 
    Edits.AddRange([ 
     Edit1, 
     Edit2, 
     // etc. 
     Edit8 
    ); 
    // You must be careful with array bounds here, Inputs and Edits must have the same length 
    Assert(Length(Inputs) = Length(Edits)); 
    for I := 0 to Edits.Count - 1 do 
     Inputs[I] := Edits[I].Text = '1'; 
    finally 
    Edits.Free; 
    end; 
end; 

을 (A TList 사용)을

var 
    Edits : array of TEdit; 
    I  : Integer; 
begin 
    SetLength(Edits, 8); 
    Edits[0] := Edit1; 
    Edits[1] := Edit2; 
    // etc. 
    // You must be careful with array bounds here, Inputs and Edits must have the same length 
    Assert(Length(Inputs) = Length(Edits)); 
    for I := 0 to Edits.Count - 1 do 
    Inputs[I] := Edits[I].Text = '1'; 
end; 

TArray<T>.CreateTArray<T> 사용 :

var 
    Edits : TArray<TEdit>; 
    I  : Integer; 
begin 
    Edits := TArray<TEdit>.Create(
    Edit1, 
    Edit2, 
    // etc. 
    Edit8 
); 
    // You must be careful with array bounds here, Inputs and Edits must have the same length 
    Assert(Length(Inputs) = Length(Edits)); 
    for I := 0 to Edits.Count - 1 do 
    Inputs[I] := Edits[I].Text = '1'; 
end; 

TDictionary<K, V> 사용 :

이 개선 방법에는 여러 가지가 있습니다
uses 
    System.Generic.Collections; 

type 
    TInput = class 
    public 
    Active: Boolean; 
    end; 

var 
    InputByEdit : TObjectDictionary<TEdit, TInput>; 
    Pair  : TPair<TEdit, TInput>; 
begin 
    InputByEdit := TObjectDictionary<TEdit, TInput>.Create([doOwnsValues]); 
    try 
    InputByEdit.Add(Edit1, TInput.Create); 
    InputByEdit.Add(Edit2, TInput.Create); 
    // etc. 
    for Pair in InputByEdit do 
     Pair.Value.Active := Pair.Key.Text = '1'; 
    finally 
    InputByEdit.Free; 
    end; 
end; 

:

  • 컨테이너 클래스 멤버 확인 및 작성시에 한 번을 채울가 .
  • ComponentsComponentCount을 사용하여 컨테이너를 동적으로 채 웁니다.

또한 사용성을 향상시키기 위해 또한 그래서 당신이 Text을 구문 분석 할 필요는 없습니다 TCheckBox 대신 TEdit의 사용을 고려하고 있습니다.

+1

+1 또한 TEdit 대신 TCheckBox를 사용하여 텍스트를 구문 분석 할 필요가 없으며 사용성을 향상시킬 수도 있습니다. – CiucaS

+1

+1 (그리고 나는 실제로 투표를 던졌습니다!) XE7에서 다음과 같이 쓸 수 있다고 생각합니다 :'Edit : = [Edit1, Edit2, ..., Edit8]; ' –