Comandos Básicos
Limpar Lista:
//Deleta algo selecionado ou seja marcado na Lista ListBox1.DeleteSelected;
ou
//Deleta algo selecionado ou seja marcado na Lista ListBox1.Items.Delete(ListBox1.ItemIndex);
ou
//Limpar tudo que contiver na Lista ListBox1.Items.Clear();
ou
//Limpar tudo que contiver na Lista ListBox1.Clear;
Abrir Item:
//Executa o OpenDialog if OpenDialog1.Execute then //Abre texto para Lista do destino selecionado ListBox1.Items.LoadFromFile(OpenDialog1.FileName);
ou
//Caso houver alguma lista na pasta do prjeto com nome Lista de //Texto abre o item para a Lista ListBox1.Items.LoadFromFile('Lista de Texto');
Salvar Item:
//Executa o SaveDialog if SaveDialog1.Execute then //Salva texto da Lista com escolha de destino ListBox1.Items.SaveToFile(SaveDialog1.FileName);
ou
//Salva item na mesma pasta do projeto sem a necessidade de escolher destino ListBox1.Items.SaveToFile('Lista de Texto');
Adicionar:
//Adiciona o que estiver no Text do Edit para a Lista ListBox1.Items.Add(Edit1.Text);
ou
//Adiciona no Edit o item slecionado na Lista ou marcado Edit1.Text := ListBox1.Items.Strings[ListBox1.itemIndex];
ou
//Adiciona na Label o item slecionado na Lista ou marcado Label1.Caption := ListBox1.Items.Strings[ListBox1.itemIndex];
Ir para o último item da lista:
ListBox1.TopIndex: = -1 + ListBox1.Items.Count;
Mover items de posição no ListBox:
Antes de mais nada coloque como formulário de nível :
var Form1: TForm1; StartingPoint : TPoint;
Coloque no OnCreat do Form :
procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.DragMode:= dmAutomatic; end;
Coloque no OnDragDrop do ListBox :
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer); var DropPosition, StartPosition: Integer; DropPoint: TPoint; begin DropPoint.X := X; DropPoint.Y := Y; with Source as TListBox do begin StartPosition := ItemAtPos(StartingPoint,True); DropPosition := ItemAtPos(DropPoint,True); Items.Move(StartPosition, DropPosition); end; end;
Coloque no OnDragOver do Listbox :
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begin Accept:= Source = ListBox1; end;
E por fim coloque no OnMouseMove do ListBox :
procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin StartingPoint.X:= X; StartingPoint.Y := Y; end;
Mostrar Hint de items do ListBox:
Coloque
Listbox.ShowHint := true ;
Depois em OnMouseMove :
procedure TForm1.ListBox1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer) ; var lstIndex : Integer ; begin with ListBox1 do begin lstIndex:=SendMessage(Handle, LB_ITEMFROMPOINT, 0, MakeLParam(x,y)) ; if (lstIndex >= 0) and (lstIndex <= Items.Count) then Hint := Items[lstIndex] else Hint := '' end; end;
Pesquisa de texto no Listbox pelo Edit:
Coloque no OnChange do Edit :
procedure TForm1.Edit1Change(Sender: TObject); const indexStart = -1; var search : array[0..128] of Char; begin //make sure Length(Edit1.Text) <= 128 StrPCopy(search, Edit1.Text) ; ListBox1.ItemIndex := ListBox1.Perform(LB_SELECTSTRING, indexStart, LongInt(@search)); end;
Remover multiplos Items selecionados de um ListBox:
Coloque:
ListBox1.MultiSelect = true ;
Depois coloque num button “sei la” :
var ii : integer; begin with ListBox1 do begin for ii := -1 + Items.Count downto 0 do if Selected[ii] then Items.Delete(ii) ; end; end;
Ordenação do Listbox:
Descer um item selecionado :
var NovoIndex : Integer; begin if not (lbx1.ItemIndex = lbx1.Items.Count-1) then begin NovoIndex := lbx1.ItemIndex + 1; lbx1.Items.Move(lbx1.ItemIndex, NovoIndex); lbx1.ItemIndex := NovoIndex; lbx1.SetFocus; end; end;
Subir um item selecionado:
var NovoIndex : Integer; begin if not (lbx1.ItemIndex - 1 < 0) then begin NovoIndex := lbx1.ItemIndex - 1; lbx1.Items.Move(lbx1.ItemIndex, NovoIndex); lbx1.ItemIndex := NovoIndex; lbx1.SetFocus; end; end;
Manipulação Simples:
Exemplo de como utilizar uma string para formatação…
Eu tenho como string “botecounix” e pretendo formatar até boteco,então faço o seguinte :
procedure TForm1.Button1Click(Sender: TObject); var X : Integer; S1 : String; S2 : String; begin for X := 0 to ListBox1.Items.Count-1 do begin S1 := Form1.ListBox1.Items[X]; S2 := 'unix'; //Edit1.Text Delete(S1, Pos(S2, S1) , length( S2 ) ); Form1.ListBox1.Items[X] :=S1; end; end;
Obs: Caso estiver somente com a string sendo referência inicial de linha o código não irá removê-la.
Fonte: Boteco Unix
Amigo, é mais fácil assim…
Mover items de posição no ListBox:
Subir Posição :
if ListBox1.Items.Count = 0 then Exit;
if ListBox1.ItemIndex > 0 then
ListBox1.Items.Exchange(ListBox1.ItemIndex, (ListBox1.ItemIndex – 1));
Desc Posição :
if ListBox1.Items.Count = 0 then Exit;
if ListBox1.ItemIndex < (ListBox1.Items.Count – 1) then
ListBox1.Items.Exchange(ListBox1.ItemIndex, (ListBox1.ItemIndex + 1));
Brother obrigado pela contribuição! Sucesso.