I am working on Delphi 7. I have one TListBox and one TStringGrid with two columns (with no fixed row or column). I have data in the TListBox as follows:
Available Elements - a123 (a123) Available Elements - a1234 (a1234) Available Elements - a12345 (a12345)
And the TStringGrid is having following data as follows:
Column1 Column2
a1 Available Elements - a1 a2 Available Elements - a12
If I select the first item in the TListbox i.e. a123 and execute following button click event procedure, then the last item data ie a12345 is getting moved into the grid.
Could anybody put the focus on what I am doing wrong in the following code. Following code moves the seleted item in the TListbox to TStringgird's two columns:
procedure TForm1.btnMoveLeftClick(Sender: TObject);
var
sString : String;
i : Integer;
begin
for i := 0 to ListBox1.Items.Count - 1 do
begin
{-- Is this status selected? --}
if ListBox1.Selected[i] then
begin
sString := Trim(ListBox1.Items[i]);
{-- Delete selected status. --}
ListBox1.Items.Delete (i);
if ((grdVFormDetails.RowCount >= 1) And (Trim(grdVFormDetails.Cells[0, 0]) <> EmptyStr)) then
grdVFormDetails.RowCount := grdVFormDetails.RowCount+1;
grdVFormDetails.Cols[1].Add(Copy(sString, 1, Pos('(', sString) - 1));
sString := Copy(sString, Pos('(', sString) + 1, Length(sString));
sString := Copy(sString, Pos('(', sString) + 1, Length(sString) - 1);
grdVFormDetails.Cols[0].Add(sString);
break;
end;
end;
end;
ListBox1.Items[ListBox1.ItemIndex]if mutli-select is off. What else are you doing in that loop that you are not showing? - Marjan VenemaItemID, which will hold the value from inside the parentheses andItemTextwhich will hold the rest of the input string. Then you can simply useCells[]to assign the value. Also that input string parsing you should do from the end to the beginning, what if there will be a string likeText (comment) - (a12345). If you'll parse it your current way, you'll extractcommentinstead ofa12345. - TLama