• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Editing Listbox at runtime 2

Hey,

I asked before how to edit a listbox at runtime by adding a string "locked" after a SELECTED Listbox item  when a button has been clicked.

Now I want to do the opposite so ...

When I click a button it removes the string "locked" after the SELECTED item.

Cheers,

Computer Programer
0
computerprogramer
Asked:
computerprogramer
  • 2
1 Solution
 
mokuleCommented:
procedure TForm1.Button2Click(Sender: TObject);
var
  ind: integer;
begin
  ind := Pos(' Locked',ListBox2.Items[ListBox2.ItemIndex]);
ListBox2.Items[ListBox2.ItemIndex] := Delete(ListBox2.Items[ListBox2.ItemIndex],ind,Length(' Locked')) ;
end;
0
 
mokuleCommented:
or better
procedure TForm1.Button2Click(Sender: TObject);
var
  ind: integer;
begin
  ind := Pos(' Locked',ListBox2.Items[ListBox2.ItemIndex]);
  if ind > 0 then
    ListBox2.Items[ListBox2.ItemIndex] := Delete(ListBox2.Items[ListBox2.ItemIndex],ind,Length(' Locked')) ;
end;
0
 
shaneholmesCommented:
procedure TForm1.btnDeleteClick(Sender: TObject);
var
 tmpStr: String;
begin
 //if item is selected
 if ListBox1.ItemIndex <> - 1 then
 begin
  //shut off control painting
  ListBox1.Items.BeginUpdate;
  //get selected item from list
  tmpStr:= ListBox1.Items[ListBox1.ItemIndex];
  //delete string from selected item
  Delete(tmpStr,pos(' - LOCKED',tmpStr),9);
  //post selected item back to list
  ListBox1.Items[ListBox1.ItemIndex]:= tmpStr;
  //turn back on control painting
  ListBox1.Items.EndUpdate;
  end;
end;

procedure TForm1.btnAddClick(Sender: TObject);
var
 tmpStr: String;
begin
 //if item is selected
 if ListBox1.ItemIndex <> - 1 then
 begin
  //shut off control painting
  ListBox1.Items.BeginUpdate;
  //get selected item from list
  tmpStr:= ListBox1.Items[ListBox1.ItemIndex];
  tmpStr:= tmpStr + ' - LOCKED';
  //post selected item back to list
  ListBox1.Items[ListBox1.ItemIndex]:= tmpStr;
  //turn back on control painting
  ListBox1.Items.EndUpdate;
  end;
end;
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now