Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 将TxpComboBox中的选定项分配给变量_Delphi_Delphi 7 - Fatal编程技术网

Delphi 将TxpComboBox中的选定项分配给变量

Delphi 将TxpComboBox中的选定项分配给变量,delphi,delphi-7,Delphi,Delphi 7,我正在尝试将TxpListBox中的所有选定项分配给TStringList 我最初的想法是这样做 Function AssignListBoxToList(ComponentName : TxpListBox) : Boolean; var slComponentValue : TStringList; begin slComponentValue := TStringList.Create; slComponentValue.Add(ComponentName.Items)

我正在尝试将TxpListBox中的所有选定项分配给TStringList

我最初的想法是这样做

Function AssignListBoxToList(ComponentName : TxpListBox) : Boolean;
var
  slComponentValue : TStringList;
begin    
  slComponentValue := TStringList.Create;
  slComponentValue.Add(ComponentName.Items);
end;
但它抛出以下异常
不兼容类型:“String”和“TString”

有没有办法创建一个TStringList的TStringList,或者在我的TxpListBox中使用String而不是TString是否安全,以及/或者我是否遗漏了什么


TxListBox是一个TListBox,其外观经过修改以符合Windows XP的设计美学

它看起来像
TxpComboBox.Items
可能是
tString
的后代(如标准的
TComboBox.Items
)。如果是这样的话,像这样的方法应该会奏效:

slComponentValue := TStringList.Create;
slComponentValue.Add(ComponentName.Items[ComponentName.ItemIndex]);
但是,您的函数不能按原样工作,因为它不返回
slComponentValue

从函数返回对象通常不是一个好主意(没有具体的原因),因为不清楚释放它的责任在哪里。我更愿意让一个过程接受一个已经创建的对象实例,从而更清楚地说明这一点:

procedure AssignComboBoxToList(ComponentName : TxpComboBox; 
    ListToFill: TStrings) : Boolean;
begin    
  Assert(Assigned(ListToFill));
  ListToFill.Add(ComponentName.Items[ComponentName.ItemIndex);
end;
然后您可以这样使用它:

slComponentValue := TStringList.Create;
try
  AssignComboBoxToList(YourComboBox, slComponentValue);
  if slComponentValue.Count > 0 then
    // Do whatever with the slComponentValue list
finally
  slComponentValue.Free;
end;
但是,由于您只处理单个字符串,因此只使用单个字符串可能更容易;这里真的不需要TStringList:

strResult := YourComboBox.Items[YourComboBox.ItemIndex];

尽管如此,
TComboBox
不支持多选
TListBox
可以,但是
TComboBox
显示一个下拉列表并允许选择单个项目,这使您的问题有些不清楚。

由于TxComboBox不是标准Delphi VCL的一部分,如果您提到它的确切含义并提供一点详细信息,可能会有所帮助。对不起,我忘记了这一点。它是xpStyles模块的一部分。它本质上只是一个组合框,看起来像是来自XP界面。嗯,我觉得自己像个白痴。我意识到我选择了错误的组件,它来自TListBox。这就解决了,谢谢。