リストで少なくとも行が選択されているかどうかに応じて、ボタンを有効または無効にする必要があります。
以下は、この問題を再現するためのコードです。リストは OnData イベントを使用して設定され、複数の行を選択できます。
OnSelectItem を使用してユーザーが選択を変更したことを検出し、TListView SelCount 関数を使用して選択された行の数を検出できると考えました。
問題は、ユーザーが複数の行を選択すると、SelCount が 0 を返すことです。リストが手動で入力されている場合 (つまり、OnData イベントを使用していない場合)、これは正常に機能します。
何か案は?
ありがとう
更新: 代わりに OnChange イベントを使用するとうまくいくようです。それでも、(SelectItem イベント内から) 複数の行が選択されたときに SelCount が 0 を返す理由を理解することは興味深いでしょう。
別の更新: テスト プロジェクトを投稿しました: https://dl.dropboxusercontent.com/u/35370420/TestListView2.zipとスクリーンショット:
この問題を再現するには、アプリを実行して Item1 を選択し、Shift キーを押しながら Item2 をクリックします。ボタンは無効です。私の意図は、リストで少なくとも 1 つの項目が選択されている限り、ボタンを動的に有効にすることでした。選択された項目がない場合、ボタンは無効になります。
PAS ファイル:
unit MainUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;
type
TForm3 = class(TForm)
ListView1: TListView;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ListView1Data(Sender: TObject; Item: TListItem);
procedure ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.FormCreate(Sender: TObject);
begin
ListView1.Items.Count := 5;
end;
procedure TForm3.ListView1Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := String.Format('Item%d', [Item.Index]);
end;
procedure TForm3.ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
begin
Button1.Enabled := ListView1.SelCount > 0;
OutputDebugString(pchar(String.Format('SelCount = %d', [ListView1.SelCount])));
end;
end.
形:
object Form3: TForm3
Left = 0
Top = 0
Caption = 'Form3'
ClientHeight = 600
ClientWidth = 952
Color = clBtnFace
DoubleBuffered = True
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object ListView1: TListView
Left = 168
Top = 160
Width = 250
Height = 150
Columns = <
item
AutoSize = True
Caption = 'Test'
end>
HideSelection = False
MultiSelect = True
OwnerData = True
TabOrder = 0
ViewStyle = vsReport
OnData = ListView1Data
OnSelectItem = ListView1SelectItem
end
object Button1: TButton
Left = 168
Top = 120
Width = 75
Height = 25
Caption = 'Some Action'
Enabled = False
TabOrder = 1
end
end