StringGridのCellからComboBoxに入れて同じ項目は消したいんですけどどうしたらいいんですか?
procedure TForm1.Button1Click(Sender: TObject);
var
F : TextFile;
Str : String;
RowCNT : Integer;
begin
if not OpenDialog1.Execute then exit;
AssignFile(F,OpenDialog1.FileName);
ReSet(F);
RowCNT := 0;
try
While not EOF(F) do
begin
Readln(F,Str);
Str := StringReplace(Str,',' , '","' ,[rfReplaceAll]);
Str := '"'+ Str +'"';
Str := StringReplace(Str, '""','" "', [rfReplaceAll]);
StringGrid1.Rows[RowCNT].CommaText:= Str;
if RowCNT<>0 then
if StringGrid1.Cells[8,RowCNT]<>' ' then
ComboBox1.Items.Append(StringGrid1.Cells[8,RowCNT]);
Inc(RowCNT);
end;
finally
CloseFile(F);
end;
end;
TStrings型ですから、Indexofで検索して無ければ追加する様にすればどうですか?
初心者なのでわからないんですけどどういう感じにしたらいいんですか?
indexOfはヘルプで検索しましたか?
わからないならまず調べてみたらどうですか。
調べたんですけどやっぱりわかりませんでした。
if ComboBox1.Items.Indexof(StringGrid1.Cells[8,RowCNT]) < 0 then
ComboBox1.Items.Append(StringGrid1.Cells[8,RowCNT]);
の様にします。
TComboBoxをヘルプで調べると、プロパティにItemsがあります。
Itemsはproperty Items: TStrings;となっていますので、
TStrings型とわかります。
TStringsをヘルプで調べると、メソッドにIndexOfがあります。
思っていたとおりにうごきました。ありがとうございます。
ツイート | ![]() |