检查是否选择了多个项目,并显示项目是否相同[关闭]

问题描述 投票:0回答:1

Delphi Listview 加载了音乐信息。当您从列表视图中选择多个项目时,它将显示

<multi>
。如何选择多个项目且 值相同,显示其值而不是
<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.

项目1.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.

单元1.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
delphi tlistview
1个回答
2
投票

很难准确理解您的要求,但我认为您正在寻找这样的东西:

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;
© www.soinside.com 2019 - 2024. All rights reserved.