Link to home
Start Free TrialLog in
Avatar of Deti
DetiFlag for Poland

asked on

Editable ListView ?

Hello there..

I have this problem and can`t fix it :/

I need Editable ListView (with many Columns).. and each column in every Row must be editable (you know, edit when click.. or double click .. whatever).

When I was searching google I found something like this:

if ListView1.SelCount > 0 then
  ListView1.Selected.EditCaption;

It`s working fine, but the problem is that this code works only with the first column .. SubItems don`t want to edit :/

I tried other components (StringGrid sux because of some problems while saving it into TFileStream, also TValueListEditor - can`t have more than 2 columns).

Of course I can add some TEdit and edit items above.. but I want to edit items exactly in the TListView.

So. how to do it ?
Avatar of RadikalQ3
RadikalQ3
Flag of Spain image

Hi!
There are serveral answers for this question, like:

https://www.experts-exchange.com/questions/20370729/problem-with-Listview.html
Avatar of Deti

ASKER

I red those solutions and they aren`t good for me (I wrote everything above).
ok here is another possible way but it still won't let you directly edit subitems (this is not possible unless you use a third party component.

See if this is a possible solution.

// UNIT
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ListView1DblClick(Sender: TObject);
  private
    xPos: Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  liTemp: TListItem;
  sSub: string;
begin
  // check there is an item there and save the x position across of the area
  // that has been selected
  liTemp := ListView1.GetItemAt(X,Y);
  if liTemp <> nil then
    xPos := x;
end;

procedure TForm1.ListView1DblClick(Sender: TObject);
var
  i,xs, xe, iCol: Integer;
  bFound: Boolean;
  ADefault: string;
begin
  // make sure something is selected
  if ListView1.Selected = nil then Exit;
  // default
  xs := 0;
  xe := 0;
  iCol := 0;
  bFound := False;
  // loop through the columns
  for i := 0 to Pred(ListView1.Columns.Count) do
  begin
    // work out the width of the current column
    xe := xs + ListView1.Columns[i].Width;
    // check the x position is within the current column
    if (xe > xPos) and (xs < xPos) then
    begin
      // if we find the item
      bFound := True;
      break
    end
    else
      // move column
      Inc(iCol);
    // store beginning of next column
    xs := xe;
  end;
  // did we find the item
  if bFound then
  begin
    // give the user a message box to change the entry
    if iCol = 0 then
      ListView1.Selected.Caption :=
        InputBox('Change Value',
                 'Enter new value',
                 Trim(ListView1.Selected.Caption))
    else
      ListView1.Selected.SubItems[iCol-1] :=
        InputBox('Change Value',
                 'Enter new value',
                 Trim(ListView1.Selected.SubItems[iCol-1]));
  end;
end;

end.


// FORM
object Form1: TForm1
  Left = 300
  Top = 145
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 16
    Top = 24
    Width = 705
    Height = 481
    Columns = <
      item
        Width = 100
      end
      item
        Width = 100
      end
      item
        Width = 100
      end>
    HideSelection = False
    Items.Data = {
      620000000300000000000000FFFFFFFFFFFFFFFF020000000000000001300131
      013200000000FFFFFFFFFFFFFFFF020000000000000001310132013300000000
      FFFFFFFFFFFFFFFF0200000000000000013201330134FFFFFFFFFFFFFFFFFFFF
      FFFF}
    ReadOnly = True
    RowSelect = True
    TabOrder = 0
    ViewStyle = vsReport
    OnDblClick = ListView1DblClick
    OnMouseDown = ListView1MouseDown
  end
end
Avatar of Deti

ASKER

:(

Okej, but: is there any component that works exactly the way I wrote ?
Not without writing your own component derived from TListView, or using a third party component like TAdvStringGrid from
http://www.tmssoftware.com
Avatar of DavidBirch2dotCom
DavidBirch2dotCom

It *may* be possible to do this with the Vertual Treeview Component from http://www.delphi-gems.com it is an extremely versatile component For examples see this page.
http://www.delphi-gems.com/VirtualTreeview/VTGallery.php
ASKER CERTIFIED SOLUTION
Avatar of EugeneK-biruza
EugeneK-biruza

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial