Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #11  
Old 06-20-2016, 10:26 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default

Same Function plus under button click event post# 8 code.

First Item is added properly when I try to add another different item with Edit I get error.
  #12  
Old 06-20-2016, 10:37 AM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

Which line exactly gives the error?
__________________
Regards,
Abdulaziz Jasser
  #13  
Old 06-20-2016, 10:56 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default

if lvItem <> nil then begin
  #14  
Old 06-20-2016, 11:19 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default

My aim is to not add duplicate listview items
  #15  
Old 06-20-2016, 11:34 AM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

UPDATED
On 21 June 2016 at 1:09pm KSA time.

Use the below function instead of the old one:


Code:
Function FindListViewItem(lv: TListView; const sCaption: String): TListItem;
         Function FindSubItem(lt : TListItem; sCaption: String): Boolean;
         var
                  i : Integer;
         begin
                  result := False;

                  for i:=0 to lt.SubItems.Count-1 do begin
                      result := AnsiCompareText(lt.SubItems[i], sCaption) = 0;

                      if result then
                         break;
                  end;
         end;
var
         i: Integer;
begin
         for i:=0 to lv.Items.Count-1 do begin
             Result := lv.Items[i];

             if AnsiCompareText(Result.Caption, sCaption) = 0 then
                exit;

             if result.SubItems.Count > 0 then begin
                if FindSubItem(result,sCaption) then
                   exit;
             end;
         end;

         Result := nil;
end;

Example:


Code:
var
          lvItem: TListItem;
begin
          lvItem := FindListViewItem(ListView1, Edit1.text);

          if lvItem <> nil then begin
             ShowMessage('Similar Item already found');
             exit;
          end else begin
             with ListView1.Items.Add do begin
                  Caption := Edit1.text;
             end;
          end;
__________________
Regards,
Abdulaziz Jasser

Last edited by Jasser; 06-21-2016 at 10:21 AM.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 03:31 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.