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;
Assuming, you want to parse the input string like this:
into a part with a trimmed string from the beginning to the first opening parenthesis (first from the end) of the input string to get a value like this:
and the string from inside the last parentheses of the input string:
If so, you can use the code that follows. Note that is expected to have list box items terminated by closing parentheses. You may check the
commented versionof this code or download asample projectif you want.Here is the part, which moves the focused item from the list box to the string grid:
And here is the part which moves the selected row from the string grid to the list box: