I'm using a ComboBox
to display a list of many items (don't worry, the differences between the items allow a quick selection by AutoComplete
:-).
This list is created during the creation of the form (OnCreate
event), but so that the form does not freeze while filling, I added a TThread
that performs the filling to TStringList
and then assigns it to ComboBox.Items
.
It works, but when the form is displayed it is not drawn properly until the thread has finished and the content in the Combo is already displayed. Shouldn't using thread save this?
Here is my code: (it is not currently written in an IDE, so there may be typos...)
type
TLoadList = class(TThread)
public
constructor Create;
destructor Destroy; override;
private
SL: TStrings;
procedure UpdateCombo;
procedure Execute; override;
end;
implementation
uses uMain, HebNumFunc; //The main form unit
constructor TLoadList.Create;
begin
FreeOnTerminate := True;
SL := TStringList.Create;
inherited Create(True);
end;
procedure TLoadList.UpdateCombo;
begin
MainFrm.YListCombo.Items := SL;
end;
procedure TLoadList.Execute;
begin
for var I: Integer := 1 to 6000 do SL.Add(HebNumber(I)); //HebNumber This is a function that converts the value of the number to a Hebrew alphabetic value
Synchronize(UpdateCombo);
end;
destructor TLoadList.Destroy;
begin
SL.Free;
inherited;
end;
The HebNumber function is declared in the HebNumFunc unit in this way:
function HebNumber(const Number: Integer): string;
const
Letters1: Array of String = ['','א','ב','ג','ד','ה','ו','ז','ח','ט'];
Letters10: Array of String = ['','י','כ','ל','מ','נ','ס','ע','פ','צ'];
Letters100: Array of String = ['','ק','ר','ש','ת','תק','תר','תש','תת','תתק'];
function _ThousandsConv(const Value: Integer): string;
var
Input, Hundreds, Tens, Some: Integer;
begin
if Value <= 0 then Exit('');
if Value = 1 then Exit(Format('%s'' ', [Letters1[Value]]));
if Value in [2..9] then Exit(Format('%s"א ', [Letters1[Value]]));
if Value >= 10 then
begin
Input := Value;
Hundreds := Input div 100;
Input := Input mod 100;
Tens := Input div 10;
Some := Input mod 10;
Result := Format('%s%s%s"א ', [Letters100[Hundreds], Letters10[Tens], Letters1[Some]]);
end;
end;
var
Input, Thousands, Hundreds, Tens, Some: Integer;
begin
Input := Number;
Thousands := Input div 1000;
if Thousands > 999 then Exit('חריגה');
Input := Input mod 1000;
Hundreds := Input div 100;
Input := Input mod 100;
Tens := Input div 10;
Some := Input mod 10;
if (Thousands > 0) and (Hundreds + Tens + Some = 0) then
Exit(Format('%sתתר', [_ThousandsConv(Thousands - 1)]));
Result := Format('%s%s%s%s', [_ThousandsConv(Thousands),
Letters100[Hundreds], Letters10[Tens], Letters1[Some]]);
if Result.Contains('יה') then Exit(Result.Replace('יה', 'טו'));
if Result.Contains('יו') then Result := Result.Replace('יו', 'טז');
end;
I call it in the OnCreate event simply like this:
var
LoadList: TLoadList;
begin
LoadList := TLoadList.Create;
LoadList.Start;
{...}
end;
There is nothing else in the OnCreate
event that causes a delay in coloring the form, other than the call to create and run the Thread. There is also nothing extra an additional operation is performed in it.