[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Search For Column in a ListView

Posted on 2007-10-02
4
Medium Priority
?
267 Views
Last Modified: 2010-04-05
Hi all,

I've got a liitle prolem with a listview.

I need some code to search IF a Column Called 'ID' exists or not in the ListView.

How can I do that please?

Thanks

ST3VO
0
Comment
Question by:ST3VO
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 19996918
       Hi!

You could do it something like this:

function getColumnIndex(sname : String) : Integer;
var
 res : Integer;
begin
  res := -1
  with ListView1 do
     for ItemIndex := 0 to Columns.Count - 1 do
     if CompareStr(Columns[ItemIndex].Caption, sname) = 0 then
     begin
        Result := ItemIndex;
        Break;
      end;
  end;
  Result := res;
end;

Regards,
   Tomas Helgi
0
 
LVL 5

Accepted Solution

by:
xr1140 earned 2000 total points
ID: 19996921
try this, i`m sure there is a more elegant way to do it ...





uMain.dfm
===============================




object Form1: TForm1
  Left = 869
  Top = 388
  Width = 461
  Height = 243
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 175
    Top = 10
    Width = 266
    Height = 196
    Columns = <>
    TabOrder = 0
    ViewStyle = vsReport
  end
  object butSearch: TButton
    Left = 10
    Top = 180
    Width = 156
    Height = 26
    Caption = 'Search for ...'
    TabOrder = 1
    OnClick = butSearchClick
  end
  object txtName: TEdit
    Left = 10
    Top = 150
    Width = 151
    Height = 21
    TabOrder = 2
  end
end















uMain.pas
===============================



unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    butSearch: TButton;
    txtName: TEdit;
    procedure FormShow(Sender: TObject);
            procedure butSearchClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
var
  newCol: TListColumn;
begin
  newCol := ListView1.Columns.Add;
  newCol.Caption := 'Col ' + IntToStr(ListView1.Columns.Count);

  newCol := ListView1.Columns.Add;
  newCol.Caption := 'Col ' + IntToStr(ListView1.Columns.Count);

  newCol := ListView1.Columns.Add;
  newCol.Caption := 'Col ' + IntToStr(ListView1.Columns.Count);
end;


procedure TForm1.butSearchClick(Sender: TObject);
var
      i : integer;
      bFound : boolean;
begin
      bFound := False;

      for i:=0 to ListView1.Columns.Count-1 do begin

            if ListView1.Columns.Items[i].Caption = txtName.Text then begin
                  ShowMessage('found it !');
                  bFound := True;
            end;
      end;

      if not bFound then ShowMessage('Column not found!');
end;

end.
0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 19996927
Sorry small error in the first code.

function getColumnIndex(sname : String) : Integer;
var
 res,i  : Integer;
begin
  res := -1
  with ListView1 do
     for i := 0 to Columns.Count - 1 do
     if CompareStr(Columns[i].Caption, sname) = 0 then
     begin
        Result := i;
        Break;
      end;
  end;
  Result := res;
end;

Regards,
    Tomas Helgi
0
 

Author Comment

by:ST3VO
ID: 19996996
Thanks!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question