ListBox combobox的常用功能

delphi TStringList 用法详解

//TStringList 常用方法与属性 :
var
  List: TStringList;
  i: Integer;
begin
  List := TStringList.Create;
  List.Add('Strings1');           {添加}
  List.Add('Strings2');
  List.Exchange(0,1);             {置换}
  List.Insert(0,'Strings3');      {插入}
  i := List.IndexOf('Strings1');  {第一次出现的位置}
  List.Sort;                      {排序}
  List.Sorted := True;   {指定排序}
  List.Count;                     {总数}
  List.Text;                      {文本集合}
  List.Delete(0);                 {删除, 0是第一个数据}
  List.LoadFromFile('c:/tmp.txt');{打开}
  List.SaveToFile('c:/tmp.txt');  {保存}
  List.Clear;                     {清空}
  List.Free;                      {释放}
end;

combobox1.Items.Add(edit1.Text); //将编辑框内容添加到combobox
edit1.Text:=combobox1.Items[0] ;

 
//对listbox中的整型数据重小到大排序
procedure sortfunc(ListBox:TListBox);
var
  I,J:Integer;
  temp:string;
begin
  with  ListBox  do
  begin
   for i:=0 to Count-1   do
    for j:=i to Count-1   do
      if StrToInt(Items[i])>StrToInt(Items[j])   then
        begin
          temp:=Items[i];
          Items[i]:=Items[j];
          Items[j]:=temp;
        end;
  end;
end;
 
listbox排序
1 sort
 
{产生随机数}
procedure TForm1.Button1Click(Sender: TObject);
var
  result:Integer;
  i:Integer;
begin
  ListBox1.Clear;
  for i:=0 to StrToInt(Edit1.Text) do
  begin
      Randomize;
      Result:=Random(100)+1;
      listbox1.Items.Add(IntToStr(result));
  end;
end;

{进行排序}
procedure TForm1.Button5Click(Sender: TObject);
var
I,J:Integer;
temp:string; {速度有慢}
begin
  with  ListBox1  do
  begin
   for i:=0 to Count-1   do
    for j:=i to Count-1   do
      if StrToInt(Items[i])>StrToInt(Items[j])   then
        begin
          temp:=Items[i];
          Items[i]:=Items[j];
          Items[j]:=temp;
        end;
  end;
end;
 
 
 
 
 
 
{添加Edit1.text}
   if ListBox1.Items.IndexOf(Edit1.Text)<0 then  ListBox1.Items.Add(Edit1.Text);
   Edit1.SetFocus;

{添加listbox1选中的那一项}
    listbox2.Items.Add(ListBox1.Items[listbox1.ItemIndex]);

{删除选中的项目}
  ListBox2.Items.Delete(ListBox2.ItemIndex);
  ListBox2.DeleteSelected; //删除选中的项目

{清空列表框}
ListBox1.Items.Clear; 
listbox2.Clear;

{全选}
  ListBox1.MultiSelect:=True;
  ListBox1.SelectAll;

{如何判断 listbox中的内容为空}
    if   listbox1.Items.Count=0  then  { 为空};
{全部添加}
var
  i:Integer;
begin
  for i:=0 to ListBox1.Count-1 do
  begin
   ListBox2.Items.Add(listbox1.Items[i]);  //循环添加
  end;
end;

{使第1项被高亮选中}
    ListBox1.selected[0]:=True];//listbox1的选中项目

{listbox1的选中项目}
    Edit1.Text:=ListBox1.Items[ListBox1.ItemIndex];

{在第1行插入字符}
    ListBox1.Items.Insert(0,Edit1.Text);
{先判断后添加,不添加列表已存在的信息 }
   if ListBox1.Items.IndexOf(Edit1.Text)<0 then  ListBox1.Items.Add(Edit1.Text);
   if ComboBox1.Items.IndexOf(Edit1.Text)<0 then  ComboBox1.Items.Add(Edit1.Text);
 
//listbox dragDrop DragOver
procedure TForm1.Edit1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := True;
end;

procedure TForm1.Edit1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
(Sender as TEdit).Text := (Source as TListBox).Items[(Source as TListBox).ItemIndex];
end;
 
// Edit Label配合ListBox

Label1.Caption := Listbox1.Items[Listbox1.ItemIndex];
Edit1.text := Listbox1.Items[Listbox1.ItemIndex];
 
 
 
//CheckListBox1常用属性和方法事件
 
//列表成员的 序列说明
第1位 0
第2位 1
...
第n位 n-1
不在成员类 -1
 
//在项目中添加字符串(子项目的最后一位接着添加)
      CheckListBox1.Items.Add(edit1.Text);
 
//全选 高亮选中Selected
     CheckListBox1.MultiSelect := True;
     CheckListBox1.SelectAll;
 
//全选 Checked All
procedure TForm1.Button11Click(Sender: TObject);
var i :integer;
begin
  for i := 0 to CheckListBox1.Items.Count - 1 do
    begin
      CheckListBox1.Checked[i] := True;//反选设置为False
    end;
end;
 
//让第n行被高亮选中
CheckListBox1.Selected[1]:=true;//第2行
 
//取消高亮选中
  CheckListBox1.ClearSelection;
 
//第3行的项目灰色不可用
CheckListBox1.ItemEnabled[2] := False;//True可用
 
//删除高亮选中的项目,(只管高亮选中就会被删除,和checked是否无关)
        CheckListBox1.DeleteSelected;//删除选中项目,即使该给项目 没勾上也会被删除
 
//删除已勾选的中项目
procedure TForm1.Button5Click(Sender: TObject);
var i : integer;
begin
for i := CheckListBox1.Items.Count-1 downto 0 do //从后面往前面删
   begin
   if CheckListBox1.Checked[i] then
      begin
         CheckListBox1.Items.Delete(i);
      end;
   end;
end;
 
//清空项目
  CheckListBox1.Items.Clear;
 
//将CheckListBox1的全部添加到CheckListBox2的Items中
 
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
CheckListBox2.Items.Clear;
for i := CheckListBox1.Items.Count - 1 downto 0 do
   begin
      CheckListBox2.Items.Add(CheckListBox1.Items[i]);
   end;
end;
 
//CheckListBox1的items操作,如单击CheckListBox1第1项弹出消息???
 
//items的拖拽调换items的位置???
//改变选中行的背景颜色???
//CheckListBox1信息 读取 和写入
 
 




原文地址:https://www.cnblogs.com/xe2011/p/2518892.html