Çeviri: Ramazan Gülbahar.
ViewStyle := Report;
CheckBoxes := True;
ViewStyle özelliği vsReport ve CheckBoxes true ayarlı iken, ListView içinde bir check box ListView nesnesinin yanında oluşur.
ListView içinden "checked" durumunu algılamak için Boolean Checked özelliği kullanılır.
OnItemChecked Olayı Eksikmi?
Malesef TListView "Checked" durumundaki değişikliği algılamaz.
Eğer TListView kontrolünün checked olayına tepki vermesini istiyorsan kontrole mesaj göndermelisin.
ListView's WindowProc procedure’sinin varsayılan davranışını değiştirerek, “checked” durumuna tepki verdirebilirsin.
WindowProc özel bir procedure dir, bütün Tcontrol’lere mesaj gönderebilir.
ListView “checked” ya da “un-checked” nasıl algılıyacağız:
1. Form’a bir TListView ekle ve birkaçtane nesne(item) gir.
2. ViewStyle – vsReport ayarla,
3. CheckBoxlara True değeri ver,
4. In the form's OnCreate event hijack the ListView1's WindowProc.
5. Eğer mesaj süreci CN_Notify içinde ve bildirme mesajı "LVN_ITEMCHANGED" ise,
6. NMLISTVIEW rkayıt sekmesinde verileri oku.
7. Eğer bir durum değişmesi olursa (LVIF_STATE) ve eğer bir nesne (LVIS_STATEIMAGEMASK) değişirse değişen nesneyi algıla, Checked özelliğini oku.
Kod: Tümünü seç
uses CommCtrl;
procedure TForm1.FormCreate(Sender: TObject) ;
begin
OriginalListViewWindowProc := ListView1.WindowProc;
ListView1.WindowProc := ListViewWindowProcEx;
end;
procedure TForm1.ListViewWindowProcEx(var Message: TMessage) ;
var
listItem : TListItem;
begin
if Message.Msg = CN_NOTIFY then
begin
if PNMHdr(Message.LParam)^.Code = LVN_ITEMCHANGED then
begin
with PNMListView(Message.LParam)^ do
begin
if (uChanged and LVIF_STATE) <> 0 then
begin
if ((uNewState and LVIS_STATEIMAGEMASK) shr 12) <> ((uOldState and LVIS_STATEIMAGEMASK) shr 12) then
begin
listItem := listView1.Items[iItem];
memo1.Lines.Add(Format('%s checked:%s', [listItem.Caption, BoolToStr(listItem.Checked, True)])) ;
end;
end;
end;
end;
end;
{Orjinal ListView mesaj handle’ı}
OriginalListViewWindowProc(Message) ;
end;
procedure TForm1.GetCheckedButtonClick(Sender: TObject) ;
var
li : TListItem;
begin
memo1.Lines.Clear;
memo1.Lines.Add('Checked Items:') ;
for li in listView1.Items do
begin
if li.Checked then
begin
memo1.Lines.Add(Format('%s %s %s', [li.Caption, li.SubItems[0], li.SubItems[1]])) ;
end;
end;
end;