【发布时间】:2014-10-13 19:19:09
【问题描述】:
我按排序顺序将 StringList 中的项目加载到 ComboBox。当我在 ComboBox 中选择一个项目时,我想知道该项目在 StringList 中出现的相应顺序。
例如: 我的StringList 的第一项 是FFFFF,但那是我的ComboBox 中的最后一项。所以当在 ComboBox 中选择 FFFFF 时,我的程序需要告诉我它是 StringList 中的最后一项。
这是我的程序,编译得很好,但没有显示所需的结果,我找不到错误。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Edit1: TEdit;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var ItemList : TStringList;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if ComboBox1.ItemIndex = -1
then
begin
Edit1.Text := 'Select Items';
end
else if ComboBox1.ItemIndex <> -1
then
begin
if ComboBox1.ItemIndex = ComboBox1.Items.IndexOf('ItemList.IndexOf(0)')
then
begin
Edit1.Text := 'First Line Selected'
end
else if ComboBox1.ItemIndex = ComboBox1.Items.IndexOf('ItemList.IndexOf(1)')
then
begin
Edit1.Text := 'Second Line Selected'
end
else if ComboBox1.ItemIndex = ComboBox1.Items.IndexOf('ItemList.IndexOf(2)')
then
begin
Edit1.Text := 'Third Line Selected'
end
else if ComboBox1.ItemIndex = ComboBox1.Items.IndexOf('ItemList.IndexOf(3)')
then
begin
Edit1.Text := 'Forth Line Selected'
end
else if ComboBox1.ItemIndex = ComboBox1.Items.IndexOf('ItemList.IndexOf(4)')
then
begin
Edit1.Text := 'Fifth Line Selected'
end
else if ComboBox1.ItemIndex = ComboBox1.Items.IndexOf('ItemList.IndexOf(5)')
then
begin
Edit1.Text := 'Sixth Line Selected'
end
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ItemList.Clear;
ItemList.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Sorted := true;
ItemList := TStringList.Create;
with ItemList do
begin
Add('FFFFF');
Add('EEEEE');
Add('DDDDD');
Add('CCCCC');
Add('BBBBB');
Add('AAAAA');
end;
ComboBox1.Items.BeginUpdate;
try
begin
ComboBox1.Items.AddStrings(ItemList);
end
finally
begin
ComboBox1.Items.EndUpdate;
end
end;
end;
end.
【问题讨论】:
-
很难知道你的程序在做什么,它应该做什么,以及它如何不能满足你的需求。给定组合的内容,您对
ComboBox1.Items.IndexOf的每个调用都将返回-1。请尝试改进问题。 -
是的,它总是返回
-1。如果ComboBox中的选定项目等于TStringList的Fisrt Line,则执行某些操作并为Second Line 等等.. -
我不想使用
AAAAA。我想使用第一行字符串、第二行。AAAA可能是可变的。它将被更改,然后TSringList将再次更新,ComboBox将相应更新。 -
请不要在 cmets 中添加详细信息。请编辑问题。
标签: delphi delphi-xe2