我正在使用一个ComboBox
显示许多项目的列表(不用担心,项目之间的差异允许通过AutoComplete
:-).
该列表是在创建表单 (OnCreate
事件),但为了使表单在填写时不会冻结,我添加了一个TThread
执行填充TStringList
然后将其分配给ComboBox.Items
.
它可以工作,但是当显示表单时,它不会正确绘制,直到线程完成并且组合中的内容已经显示。难道不应该使用线程来保存这个吗?
这是我的代码:
(目前它不是在 IDE 中编写的,因此可能存在拼写错误...)
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;
HebNumber 函数在 HebNumFunc 单元中声明如下:
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;
我在 OnCreate 事件中简单地这样调用它:
var
LoadList: TLoadList;
begin
LoadList := TLoadList.Create;
LoadList.Start;
{...}
end;
里面没有别的东西了OnCreate
除了调用创建和运行线程之外,导致窗体着色延迟的事件。其中也没有执行任何额外的操作。