Willino Blog

Sono un ingegnere informatico, appassionato di fotografia, chitarra acustica, micologia e Tennis Tavolo. In questo blog cercherò di raccontare eventi ed esperienze che in qualche modo potrebbero risultare utili a tutti.

martedì 19 maggio 2009

[Delphi] Un Class Helper per TStringList

Di seguito una estensione a supporto di tutte le istanze TStringlist utilizzate nelle applicazioni delphi.

L'utilizzo è banalissimo, per una qualsiasi istanza di TStringlist :

• Esempio Object Pascal
  1.Var
2. MyStrings : TStringList;
3. lIndex : Integer;
4.Begin
5. MyString:=TStringList.Create;
6. MyString.Add('Prova 1');
7. MyString.Add('Prova 2');
8. MyString.Add('Prove 3');
9. MyString.Add('Prove 4');
10. MyString.Add('Provo 5');
11. lIndex:=MyString.FindByPart('Prove');
12. If lIndex<>-1 Then
13. Begin
14. // Trovato Prove in MyString[lIndex];
15. End;
16.End;
Esempio di utilizzo


Di seguito il codice dell'Helper :

• Esempio Object Pascal
  1.Interface
2.
3. TStringListHelper = Class Helper For TStringList
4. Public
5. // True se aSubString Š contenuta in una delle righe
6. // a partire dalla riga StartFrom;
7. // Restituisce la prima riga con l'occorrenza
8. Function FindByPart(Const aSubString : String;
9. Var aIndex : Integer;
10. Const StartFrom : Integer = -1) : Boolean; Overload; Inline;
11. Function FindByPart(Const aSubString : String) : Boolean; Overload; Inline;
12. // Scorre le righe a partire dalla riga StartFrom
13. // sino a trovare la prima riga aIndex non vuota
14. Procedure FindNextLine(Var aIndex : Integer; Const
15. StartFrom : Integer = -1); Inline;
16. // True se aSubString Š contenuta in una delle righe
17. // a partire dalla riga StartFrom
18. Function Contain(Const aSubString : String;
19. Const StartFrom : Integer = -1) : Boolean; Inline;
20. End;
21.
22.Implementation
23.
24.function TStringListHelper.FindByPart(const aSubString: String;
25. Var aIndex: Integer;
26. Const StartFrom : Integer = -1): Boolean;
27.Var
28. S,
29. I : Integer;
30. C : Integer;
31.begin
32. aIndex:=-1;
33. Result:=False;
34. If Self.Count>0 Then
35. Begin
36. S:=0;
37. C:=Pred(Self.Count);
38. If (StartFrom>=0) And (StartFrom<(C+1)) Then S:=StartFrom;
39. For I:=S To C Do
40. Begin
41. If Pos(aSubString,Self[I])<>0 Then
42. Begin
43. aIndex:=I;
44. Result:=True;
45. Break;
46. End;
47. End;
48. End;
49.end;
50.
51.function TStringListHelper.FindByPart(const aSubString: String): Boolean;
52.Var
53. S,
54. I : Integer;
55. C : Integer;
56.begin
57. Result:=False;
58. S:=0;
59. C:=Pred(Self.Count);
60. For I:=S To C Do
61. Begin
62. If Pos(aSubString,Self[I])<>0 Then
63. Begin
64. Result:=True;
65. Break;
66. End;
67. End;
68.end;
69.
70.procedure TStringListHelper.FindNextLine(var aIndex: Integer;
71. const StartFrom: Integer);
72.Var
73. S,
74. I : Integer;
75. C : Integer;
76.begin
77. aIndex:=-1;
78. S:=0;
79. C:=Pred(Self.Count);
80. If (StartFrom>=0) And (StartFrom<(C+1)) Then S:=StartFrom;
81. For I:=S To C Do
82. Begin
83. If Trim(Self[I])<>'' Then
84. Begin
85. aIndex:=I;
86. Break;
87. End;
88. End;
89.end;
90.
91.function TStringListHelper.Contain(const aSubString: String;
92. const StartFrom: Integer): Boolean;
93.begin
94. If (StartFrom>=0) And (StartFrom<(Pred(Self.Count)+1)) Then
95. Result:=(Pos(aSubString,Self[StartFrom])<>0)
96. Else
97. Result:=False;
98.end;
Class Helper per TStringList

Nessun commento:

ANSA.it - Top News