Z
zflegend
Unregistered / Unconfirmed
GUEST, unregistred user!
前几天做了一个全文检索的小程序,其功能是在检索的同时将得到的文件名称分类加入到一个treeview控件中,并在检索过程中可以点击treeview控件将其展开。程序采用多线程,在线程Execute方法中调用主线程的过程进行检索。如果等到检索完毕再将treeview控件展开是没有问题的,但是在检索过程中将treeview控件展开就会出错,发生结点插入混乱及一些结点得不到子结点等错误。
最让我绝望的是错误仅发生在treeview控件的立滚动条出现后,只要立滚动条不出现,怎么点击也不会有问题。这个问题我始终无法解决,只好求助各位大虾了!
由于全文检索源程序比较大,还需要用来搜索的文本,各位看起来会比较麻烦,于是用我原来程序的框架编了个小程序贴出来,出现的问题也和我原来的程序相同,请大家帮忙看看。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
StartButton: TButton;
TreeView1: TTreeView;
procedure FormCreate(Sender: TObject);
procedure JobStart;
procedure Cycbegin
;
//循环开始
procedure AddNode(iint:integer);
//添加结点
procedure StartButtonClick(Sender: TObject);
private
{ Private declarations }
public
end;
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
constructor Create;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor TMyThread.Create;
begin
inherited Create(false);
end;
procedure TMyThread.Execute;
begin
Form1.JobStart;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
firstnode,dirnode : TTreeNode;
begin
firstnode:=TreeView1.Items.GetFirstNode;
dirnode := TreeView1.Items.AddChild(firstnode, '根结点');
dirnode.HasChildren := true;
end;
procedure TForm1.JobStart;
var
topnode:TTreeNode;
begin
topnode:=TreeView1.TopItem;
topnode.DeleteChildren;
//开始前清空TreeView
Cycbegin
;
end;
procedure TForm1.Cycbegin
;
var
i:integer;
begin
for i:=1 to 400do
begin
AddNode(i);
sleep(50);
end;
end;
procedure TForm1.AddNode(iint:integer);
var
dirnode,topnode,searchnode,leafnode : TTreeNode;
begin
//先将1、2、3做为一级子结点加入
topnode:=TreeView1.TopItem;
if (iint=1) or (iint=2) or (iint=3) then
dirnode := TreeView1.Items.AddChild(topnode, inttostr(iint));
//将十位数与父结点相等的二位数加入
if length(inttostr(iint))=2 then
begin
searchnode:=topnode.getFirstChild;
while searchnode<>nildo
begin
if pos(searchnode.Text,inttostr(iint))=1 then
dirnode := TreeView1.Items.AddChild(searchnode, inttostr(iint));
searchnode:=searchnode.getNextSibling;
end;
end;
//将百、十位与父结点相等的三位数加入
if length(inttostr(iint))=3 then
begin
dirnode:=topnode.getFirstChild;
while dirnode<>nildo
begin
searchnode:=dirnode.getFirstChild;
while searchnode<>nildo
begin
if pos(searchnode.Text,inttostr(iint))=1 then
leafnode := TreeView1.Items.AddChild(searchnode, inttostr(iint));
searchnode:=searchnode.getNextSibling;
end;
dirnode:=dirnode.getNextSibling;
end;
end;
end;
procedure TForm1.StartButtonClick(Sender: TObject);
var
MyThread_1:TMyThread;
begin
MyThread_1:=TMyThread.Create;
end;
end.
最让我绝望的是错误仅发生在treeview控件的立滚动条出现后,只要立滚动条不出现,怎么点击也不会有问题。这个问题我始终无法解决,只好求助各位大虾了!
由于全文检索源程序比较大,还需要用来搜索的文本,各位看起来会比较麻烦,于是用我原来程序的框架编了个小程序贴出来,出现的问题也和我原来的程序相同,请大家帮忙看看。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
StartButton: TButton;
TreeView1: TTreeView;
procedure FormCreate(Sender: TObject);
procedure JobStart;
procedure Cycbegin
;
//循环开始
procedure AddNode(iint:integer);
//添加结点
procedure StartButtonClick(Sender: TObject);
private
{ Private declarations }
public
end;
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
constructor Create;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor TMyThread.Create;
begin
inherited Create(false);
end;
procedure TMyThread.Execute;
begin
Form1.JobStart;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
firstnode,dirnode : TTreeNode;
begin
firstnode:=TreeView1.Items.GetFirstNode;
dirnode := TreeView1.Items.AddChild(firstnode, '根结点');
dirnode.HasChildren := true;
end;
procedure TForm1.JobStart;
var
topnode:TTreeNode;
begin
topnode:=TreeView1.TopItem;
topnode.DeleteChildren;
//开始前清空TreeView
Cycbegin
;
end;
procedure TForm1.Cycbegin
;
var
i:integer;
begin
for i:=1 to 400do
begin
AddNode(i);
sleep(50);
end;
end;
procedure TForm1.AddNode(iint:integer);
var
dirnode,topnode,searchnode,leafnode : TTreeNode;
begin
//先将1、2、3做为一级子结点加入
topnode:=TreeView1.TopItem;
if (iint=1) or (iint=2) or (iint=3) then
dirnode := TreeView1.Items.AddChild(topnode, inttostr(iint));
//将十位数与父结点相等的二位数加入
if length(inttostr(iint))=2 then
begin
searchnode:=topnode.getFirstChild;
while searchnode<>nildo
begin
if pos(searchnode.Text,inttostr(iint))=1 then
dirnode := TreeView1.Items.AddChild(searchnode, inttostr(iint));
searchnode:=searchnode.getNextSibling;
end;
end;
//将百、十位与父结点相等的三位数加入
if length(inttostr(iint))=3 then
begin
dirnode:=topnode.getFirstChild;
while dirnode<>nildo
begin
searchnode:=dirnode.getFirstChild;
while searchnode<>nildo
begin
if pos(searchnode.Text,inttostr(iint))=1 then
leafnode := TreeView1.Items.AddChild(searchnode, inttostr(iint));
searchnode:=searchnode.getNextSibling;
end;
dirnode:=dirnode.getNextSibling;
end;
end;
end;
procedure TForm1.StartButtonClick(Sender: TObject);
var
MyThread_1:TMyThread;
begin
MyThread_1:=TMyThread.Create;
end;
end.