我在窗体上放了listbox,imagelist,image
procedure TForm1.FormCreate(Sender: TObject); var
  i:Integer ;
  bmp:TBitmap ;
begin
 listbox.Clear ;
  for i:= 0 to ImageList.Count -1 do
  begin
    bmp:=TBitmap.Create ;
    ImageList.GetBitmap(i,bmp) ;
    ListBox.AddItem('头像'+inttostr(i),bmp);
    end;end;
我如何实现一点listbox中的一项就能在image中显示对应的图片

解决方案 »

  1.   

    ListBox1 的 Click 事件写代码
    procedure TForm1.ListBox1Click(Sender: TObject);
    var
      Bitmap: TBitmap;
    begin
      //Image1.Picture.LoadFromFile(ImageList1.);
      Bitmap := TBitmap.Create;
      try
        ImageList1.GetBitmap(ListBox1.ItemIndex, Bitmap);
        Image1.Picture.Bitmap := Bitmap;
      finally
        Bitmap.Free;
      end;
    end;
      

  2.   

    同楼上。Image1.Picture.Bitmap := Bitmap;
    一句,改为这样好点:
    Image1.Picture.Bitmap.Assign(Bitmap);
      

  3.   

    onClick事件里面写代码,根据不同的选项,image就load不同图片
      

  4.   

    procedure TForm1.ListBox1Click(Sender: TObject);
    var
      Bitmap: TBitmap;
    begin
      Bitmap := TBitmap.Create;
      try
        ImageList1.GetBitmap(ListBox1.ItemIndex, Bitmap);
        Image1.Picture.Bitmap.Assign(Bitmap); 
      finally
        Bitmap.Free;
      end;
    end;
      

  5.   

    Image1.Picture.Bitmap := Bitmap;最终也终也是调用Aissgn方法的, 但比Aissgn更好var
      NewGraphic: TGraphic;
    begin
      NewGraphic := nil;
      if Value <> nil then
      begin
        NewGraphic := TGraphicClass(Value.ClassType).Create;
        NewGraphic.Assign(Value);
        NewGraphic.OnChange := Changed;
        NewGraphic.OnProgress := Progress;
      end;
      try
        FGraphic.Free;
        FGraphic := NewGraphic;
        Changed(Self);
      except
        NewGraphic.Free;
        raise;
      end;
      

  6.   

    其实楼主已经把它存到了listbox当中,就不需要再去getbitmap.procedure TForm1.ListBox1Click(Sender: TObject);
    var
      Bitmap: TBitmap;
    begin
      Bitmap := TBitmap(ListBox1.Items.Objects[ListBox1.ItemIndex]);
      Image1.Picture.Assign(Bitmap); 
    end;
      

  7.   

    如果用相同的图片就用僵哥的:procedure TForm1.ListBox1Click(Sender: TObject);
    var
      Bitmap: TBitmap;
    begin
      Bitmap := TBitmap(ListBox1.Items.Objects[ListBox1.ItemIndex]);
      Image1.Picture.Assign(Bitmap); 
    end;如果要显示不同的图片:procedure TForm1.ListBox1Click(Sender: TObject);
    var
      Bitmap: TBitmap;
    begin
      Bitmap := TBitmap.Create;
      try
        ImageList1.GetBitmap(ListBox1.ItemIndex, Bitmap);
        Image1.Picture.Bitmap.Assign(Bitmap); 
      finally
        Bitmap.Free;
      end;
    end;