delphi与列表框项目的显示
我使用一个列表框来显示一个简单的文件名列表。我还有一个编辑组件,允许我通过一个简单的:delphi与列表框项目的显示,delphi,listbox,Delphi,Listbox,我使用一个列表框来显示一个简单的文件名列表。我还有一个编辑组件,允许我通过一个简单的: procedure TForm1.Edit1Change(Sender: TObject); const indexStart = -1; var search : array[0..256] of Char; begin if edit1.Text='' then exit; StrPCopy(search, Edit1.Text) ; ListBox1.ItemIndex := Lis
procedure TForm1.Edit1Change(Sender: TObject);
const
indexStart = -1;
var
search : array[0..256] of Char;
begin
if edit1.Text='' then exit;
StrPCopy(search, Edit1.Text) ;
ListBox1.ItemIndex := ListBox1.Perform(LB_SELECTSTRING, indexStart, LongInt(@search));
end;
现在,有没有一种方法可以“有选择地”在列表框上显示项目?我的意思是,如果我搜索一个以“hello”开头的项目,那么只会显示那个些将显示hello的项目,要么使那个些项目变暗,要么使其可见:=false。
有没有一种方法可以使用列表框执行此操作
谢谢
哦,这是Delphi 7…我总是这样做(而且我经常这样做):
我有一个字符串数组
或一个TStringList
包含列表框项。然后,在Edit1Change
中,我清除Items属性,只添加与编辑框中的文本匹配的字符串
字符串数组
如果使用字符串数组,例如
var
arr: array of string;
以某种方式初始化的,如
procedure TForm1.FormCreate(Sender: TObject);
begin
SetLength(arr, 3);
arr[0] := 'cat';
arr[1] := 'dog';
arr[2] := 'horse';
end;
那你就可以了
procedure TForm1.Edit1Change(Sender: TObject);
var
i: Integer;
begin
ListBox1.Items.BeginUpdate;
ListBox1.Items.Clear;
if length(Edit1.Text) = 0 then
for i := 0 to high(arr) do
ListBox1.Items.Add(arr[i])
else
for i := 0 to high(arr) do
if Pos(Edit1.Text, arr[i]) > 0 then
ListBox1.Items.Add(arr[i]);
ListBox1.Items.EndUpdate;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var
i: Integer;
begin
ListBox1.Items.BeginUpdate;
ListBox1.Items.Clear;
if length(Edit1.Text) = 0 then
ListBox1.Items.AddStrings(arr)
else
for i := 0 to arr.Count - 1 do
if Pos(Edit1.Text, arr[i]) = 1 then
ListBox1.Items.Add(arr[i]);
ListBox1.Items.EndUpdate;
end;
这将仅显示数组中包含Edit1.Text
的字符串;字符串不需要以Edit1.Text
开头。要完成此操作,请替换
Pos(Edit1.Text, arr[i]) > 0
与
斜纹夜光
如果出现t字符串glist
,如
var
arr: TStringList;
及
你能行
procedure TForm1.Edit1Change(Sender: TObject);
var
i: Integer;
begin
ListBox1.Items.BeginUpdate;
ListBox1.Items.Clear;
if length(Edit1.Text) = 0 then
for i := 0 to high(arr) do
ListBox1.Items.Add(arr[i])
else
for i := 0 to high(arr) do
if Pos(Edit1.Text, arr[i]) > 0 then
ListBox1.Items.Add(arr[i]);
ListBox1.Items.EndUpdate;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var
i: Integer;
begin
ListBox1.Items.BeginUpdate;
ListBox1.Items.Clear;
if length(Edit1.Text) = 0 then
ListBox1.Items.AddStrings(arr)
else
for i := 0 to arr.Count - 1 do
if Pos(Edit1.Text, arr[i]) = 1 then
ListBox1.Items.Add(arr[i]);
ListBox1.Items.EndUpdate;
end;
区分大小写
上面的代码使用区分大小写的匹配,例如,“bo”将不匹配“Boston”。要使代码对大小写不敏感,请编写
if Pos(AnsiLowerCase(Edit1.Text), AnsiLowerCase(arr[i])) > 0 then
而不是
if Pos(Edit1.Text, arr[i]) > 0 then
您所要求的可以通过将标准Win32 API接口连接到标准TEdit来实现,无需TListBox。将TStrings对象连接到IAutoComplete并不太困难,这样它就知道哪些字符串可用于搜索。这可能有效。我看到的问题是,我不知道列表框中有多少项。这不是固定的。用SetLength设置数组的大小是很困难的。除非。。。我可以稍后使用SetLength重置数组的长度吗?或者最好使用TStringList?大多数人总是使用
TStringList
,但就个人而言,我想手动执行所有操作,所以我总是使用字符串数组。您可以随时通过SetLength
更改动态数组的长度。如果增加长度,旧项目仍将存在。hmmm。我正在试着用一根绳子。当我尝试搜索时,所有项目都消失了。哦,糟糕,我装错了。。。但是它不起作用。我有两个文件:第一册和第二册。当我搜索book时,它会找到一个名为“places in boston”的文件的“bo”,但就是这样,我只是在Pos(Lowercase(Edit1.Text)、Lowercase(arr[I])中添加了Lowercase(),使其不区分大小写。谢谢@是的,我认为这就是问题所在,所以我甚至更新了我的答案!