Delphi 将TxpComboBox中的选定项分配给变量
我正在尝试将TxpListBox中的所有选定项分配给TStringList 我最初的想法是这样做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)
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。这就解决了,谢谢。