Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 06-20-2016, 05:14 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default already exist

How to find if Listview Item already exist by Caption and How to uncheck Listview item by Caption.

I already tried

Code:
if SameText(ListView1.Items[i].Caption, Caption) then Exit;
but it does not allow adding other items while caption exist.

Last edited by robert; 06-20-2016 at 05:21 AM.
  #2  
Old 06-20-2016, 07:27 AM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

UPDATED

Use the below function:

Code:
Function FindListViewItem(lv: TListView; const sCaption: String; iColumnIndex: Integer): TListItem;
var
         i: Integer;
         found: Boolean;
begin
         Assert(Assigned(lv));
         Assert((lv.viewstyle = vsReport) or (iColumnIndex = 0));
         Assert(sCaption <> '');

         for i := 0 to lv.Items.Count - 1 do begin
             Result := lv.Items[i];
             if iColumnIndex = 0 then
                found := AnsiCompareText(Result.Caption, sCaption) = 0
             else if iColumnIndex > 0 then
                found := AnsiCompareText(Result.SubItems[iColumnIndex - 1], sCaption) = 0
             else
                found := False;

             if found then
                exit;
         end;

         Result := nil;
end;
Example:

Code:
var
          lvItem: TListItem;
begin
          // Search for an item using the caption.
          lvItem := FindListViewItem(ListView1, 'The Caption', 1);

          // If the item is found then uncheck it.
          if lvItem <> nil then begin
             lvItem.Checked := False;
          end;
__________________
Regards,
Abdulaziz Jasser

Last edited by Jasser; 06-20-2016 at 07:32 AM.
  #3  
Old 06-20-2016, 08:22 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default

It seems not working from my side

Code:
var
          lvItem: TListItem;
begin
          // Search for an item using the caption.
          lvItem := FindListViewItem(ListView1, 'The Caption', 1);

          // If the item is found then uncheck it.
          if lvItem <> nil then begin
            ShowMessage('Similar Item already found');
exit;
          end;
It does nothing but keeps adding similar Captions in Listview.

If I have Listview Caption = forum
and another Caption is delphi

then I add again forum .. it is added wheres there should be been prevention adding similar item.
  #4  
Old 06-20-2016, 09:13 AM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

My posted code does NOT add any thing to the listview. Show me your complete code.
__________________
Regards,
Abdulaziz Jasser
  #5  
Old 06-20-2016, 09:22 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default

I know your does not add any item but I am adding items in Listview and if similar item is already added and If I try to add it again your code should stop doing this.
  #6  
Old 06-20-2016, 09:37 AM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

Did you used the code with UPDATED title at the top?


Please show me your code.
__________________
Regards,
Abdulaziz Jasser
  #7  
Old 06-20-2016, 09:44 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default

Same . Again adding same item more than one time ..

Code:
with ListView1.Items.Add do begin
Caption := Edit1.text;
 lvItem := FindListViewItem(ListView1, Edit1.text, 1);

          // If the item is found then uncheck it.
          if lvItem <> nil then begin
            ShowMessage('Similar Item already found');
exit;
  #8  
Old 06-20-2016, 09:49 AM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

Wrong code. Your code should be like this:



Code:
lvItem := FindListViewItem(ListView1, Edit1.text, 1);

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
  #9  
Old 06-20-2016, 10:13 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default

Error while adding another item

List Index Out of bounds(0)
  #10  
Old 06-20-2016, 10:17 AM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

This has nothing to do with my code. This error means that you are dealing with an item with wrong index.

Again show me the code.
__________________
Regards,
Abdulaziz Jasser
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:47 PM.


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