delphitlistview

Check if multiple items are selected, and show if items are the same


Delphi Listview is loaded with music information. When you select multiple items from the Listview, it will show <multi>. How can you select multiple items and the value is the same, showing its value instead of <multi>?

Unit1.asp

unit Unit1;
interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls;
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    ListView1: TListView;
    Panel2: TPanel;
    strTrack: TEdit;
    strArtist: TEdit;
    LTrack: TLabel;
    LBand: TLabel;
    strSong: TEdit;
    strAlbum: TEdit;
    LSong: TLabel;
    LAlbum: TLabel;
    LYear: TLabel;
    strYear: TEdit;
    strCheck: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Column: TListColumn;
  Item: TListItem;
begin
ListView1.Items.BeginUpdate;
  Panel1.Align := alClient;
  ListView1.Align := alClient;
  Column := ListView1.Columns.Add;
  Column.Caption := 'ID';
  Column.Alignment := taLeftJustify;
  Column.Width := 60;

  Column := ListView1.Columns.Add;
  Column.Caption := 'Band Name';
  Column.Alignment := taLeftJustify;
  Column.Width := 300;

    Column := ListView1.Columns.Add;
  Column.Caption := 'Song Name';
  Column.Alignment := taLeftJustify;
  Column.Width := 300;

    Column := ListView1.Columns.Add;
  Column.Caption := 'Album Name';
  Column.Alignment := taLeftJustify;
  Column.Width := 300;

  Item := ListView1.Items.Add;
  Item.Caption := '07';
  Item.SubItems.Add('KISS');
  Item.SubItems.Add('Tears are Falling');
  Item.SubItems.Add('Asylum');
   Item.SubItems.Add('1985');


   Item := ListView1.Items.Add;
    Item.Caption := '08';
  Item.SubItems.Add('W.A.S.P.');
  Item.SubItems.Add('The Idol');
  Item.SubItems.Add('The Crimson Idol ');
   Item.SubItems.Add('1992');

   Item := ListView1.Items.Add;
    Item.Caption := '04';
  Item.SubItems.Add('Bobaflex');
  Item.SubItems.Add('Hey You');
  Item.SubItems.Add('Eloquent Demons');
  Item.SubItems.Add('2017');

   Item := ListView1.Items.Add;
      Item.Caption := '04';
  Item.SubItems.Add('Blacktop Mojo');
  Item.SubItems.Add('Prodigal');
  Item.SubItems.Add('Burn the Ships');
   Item.SubItems.Add('2017');

   Item := ListView1.Items.Add;
      Item.Caption := '08';
  Item.SubItems.Add('Whitechapel');
  Item.SubItems.Add('Orphan');
  Item.SubItems.Add('Kin');
   Item.SubItems.Add('2021');

  Item := ListView1.Items.Add;
  Item.Caption := '09';
  Item.SubItems.Add('KISS');
  Item.SubItems.Add('Dreamin');
  Item.SubItems.Add('Psycho Circus');
   Item.SubItems.Add('1999');
ListView1.Items.EndUpdate;
end;

procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
  var
  ls: TListItem;
begin       // LS has to be here, if not, you will receive  an access violation when you click on items after the first item.
ls := ListView1.Selected;
 if Assigned(ls) then
  begin
  strTrack.Text := ListView1.Selected.Caption;
    strArtist.Text := ListView1.Selected.SubItems[0];
    strSong.Text := ListView1.Selected.SubItems[1];
    strAlbum.Text := ListView1.Selected.SubItems[2];
    strYear.Text := ListView1.Selected.SubItems[3];

     ls := ListView1.GetNextItem(ls, sdAll, [isSelected]);
     while Assigned(ls) do
    begin
    strTrack.Text := '<multi>';
    strArtist.Text := '<multi>';
    strSong.Text := '<multi>';
    strAlbum.Text := '<multi>';
    strYear.Text := '<multi>';
     ls := ListView1.GetNextItem(ls, sdAll, [isSelected]);
    end;
   end;
end;
end.

Project1.dpr

program Project1;

uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Unit1.dfm

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 523
  ClientWidth = 626
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  OnCreate = FormCreate
  TextHeight = 15
  object Panel1: TPanel
    Left = 40
    Top = 224
    Width = 529
    Height = 249
    TabOrder = 0
    object ListView1: TListView
      Left = 40
      Top = 48
      Width = 425
      Height = 150
      Columns = <>
      MultiSelect = True
      RowSelect = True
      TabOrder = 0
      ViewStyle = vsReport
      OnSelectItem = ListView1SelectItem
    end
  end
  object Panel2: TPanel
    Left = 0
    Top = 0
    Width = 626
    Height = 97
    Align = alTop
    TabOrder = 1
    object LTrack: TLabel
      Left = 184
      Top = 34
      Width = 37
      Height = 15
      Caption = 'Track #'
    end
    object LBand: TLabel
      Left = 7
      Top = 13
      Width = 27
      Height = 15
      Caption = 'Band'
    end
    object LSong: TLabel
      Left = 8
      Top = 42
      Width = 27
      Height = 15
      Caption = 'Song'
    end
    object LAlbum: TLabel
      Left = 184
      Top = 8
      Width = 36
      Height = 15
      Caption = 'Album'
    end
    object LYear: TLabel
      Left = 402
      Top = 8
      Width = 22
      Height = 15
      Caption = 'Year'
    end
    object strTrack: TEdit
      Left = 227
      Top = 34
      Width = 166
      Height = 23
      TabOrder = 0
    end
    object strArtist: TEdit
      Left = 40
      Top = 5
      Width = 121
      Height = 23
      TabOrder = 1
    end
    object strSong: TEdit
      Left = 41
      Top = 34
      Width = 121
      Height = 23
      TabOrder = 2
    end
    object strAlbum: TEdit
      Left = 226
      Top = 5
      Width = 167
      Height = 23
      TabOrder = 3
    end
    object strYear: TEdit
      Left = 430
      Top = 5
      Width = 43
      Height = 23
      TabOrder = 4
    end
  end
end

Solution

  • It is difficult to understand exactly what you are asking for, but I think you are looking for something like this:

    procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    var
      ls: TListItem;
      sTrack, sArtist, sSong, sAlbum, sYear: string; 
    
      procedure CheckForMulti(var selectedStr: string; const value: string);
      begin
        if selectedStr = '' then
          selectedStr := value
        else if selectedStr <> value then
          selectedStr := '<multi>';
      end;
    
    begin
      ls := ListView1.Selected;
      while Assigned(ls) do
      begin
        CheckForMulti(sTrack,  ls.Caption);
        CheckForMulti(sArtist, ls.SubItems[0]);
        CheckForMulti(sSong,   ls.SubItems[1]);
        CheckForMulti(sAlbum,  ls.SubItems[2]);
        CheckForMulti(sYear,   ls.SubItems[3]);
        ls := ListView1.GetNextItem(ls, sdAll, [isSelected]);
      end;
      strTrack.Text  := sTrack;
      strArtist.Text := sArtist;
      strSong.Text   := sSong;
      strAlbum.Text  := sAlbum;
      strYear.Text   := sYear;
    end;