| |

VerySource

 Forgot password?
 Register
Search
View: 3429|Reply: 11

How does listview know, which column is the mouse click?

[Copy link]

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-30 11:00:01
| Show all posts |Read mode
Thank you!
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-7 10:15:01
| Show all posts
listview is of type vsReport
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-7 23:15:01
| Show all posts
Column's Index can know which one you ordered
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-9 14:45:02
| Show all posts
How does this work?

ListView.Column ??

ListView.Selected.Column ??

Or in which event?
Reply

Use magic Report

1

Threads

13

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-10 19:00:01
| Show all posts
Look at what the foreigner wrote.

Extended GetItemAt

This method (GetItemAt) only provides the information about which ListItem (if any) is located at the specified coordinates passed as parameters, but only works with the first column of the TListView. The rest are ignored. If we needed to know if the user clicked on an element in another column, we can declare a new method in a derived class:

uses ComCtrls;

type
  TListViewX = class (TListView)
  public
    function GetItemAtX (X, Y: integer; var Col: integer): TListItem;
  end;

implementation

function TListViewX.GetItemAtX (X, Y: integer;
    var Col: integer): TListItem;
var
  i, n, RelativeX, ColStartX: Integer;
  ListItem: TlistItem;
begin
  Result: = GetItemAt (X, Y);
  if Result <> nil then begin
    Col: = 0; // First column
  end else if (ViewStyle = vsReport)
      and (TopItem <> nil) then begin
    // First, let's try to find the row
    ListItem: = GetItemAt (TopItem.Position.X, Y);
    if ListItem <> nil then begin
      // Now let's try to find the Column
      RelativeX: = X-ListItem.Position.X-BorderWidth;
      ColStartX: = Columns [0] .Width;
      n: = Columns.Count-1;
      for i: = 1 to n do begin
        if RelativeX <ColStartX then break;
        if RelativeX <= ColStartX +
            StringWidth (ListItem.SubItems [i-1]) then
        begin
          Result: = ListItem;
          Col: = i;
          break;
        end; // if
        Inc (ColStartX, Columns [i] .Width);
      end; // for
    end; // if
  end; // if
end;

Casting to the new class

We don't need to intall this new component and register it in the components palette as we explained in another article. Instead, any time we want to access this method, we can just cast the object (for example ListView1) to our new class . For example in a MouseDown event:

procedure TForm1.ListView1MouseDown (Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  col: integer;
  li: TListItem;
begin
  li: = TListViewX (ListView1) .GetItemAtX (x, y, col);
  if li <> nil then
    ShowMessage ('Column #' + IntToStr (col));
end;
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-20 23:15:01
| Show all posts
Not quite right, some can, some can't
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-25 13:45:01
| Show all posts
Most of them are in column 0
Reply

Use magic Report

1

Threads

13

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-14 06:45:01
| Show all posts
Modified:
function TListViewX.GetItemAtX (X, Y: integer;
  var Col: integer): TListItem;
var
  i, n, RelativeX, ColStartX: Integer;
  ListItem: TlistItem;
  OldRowSelect: Boolean;
  Found: Boolean;
begin
  Result: = GetItemAt (X, Y);
  if (not RowSelect) and (Result <> nil) then
  begin
    Col: = 0; // First column
  end
  else if (ViewStyle = vsReport)
    and (TopItem <> nil) then
  begin
    // First, let's try to find the row
    // ListItem: = GetItemAt (TopItem.Position.X, Y);

    // save rowselect setting
    OldRowSelect: = RowSelect;
    RowSelect: = True;
    ListItem: = GetItemAt (X, Y);
    // Restore rowselect setting;
    RowSelect: = OldRowSelect;

    if ListItem <> nil then
    begin
      // Now let's try to find the Column
      RelativeX: = X-ListItem.Position.X-BorderWidth;
// ColStartX: = Columns [0] .Width;
      ColStartX: = 0;
      n: = Columns.Count-1;
      for i: = 0 to n do
      begin
        if RelativeX <ColStartX + Columns [i] .Width then
        begin
          Result: = ListItem;
          Col: = i;
          break;
        end;
{if RelativeX <= ColStartX +
          StringWidth (ListItem.SubItems [i-1]) then
        begin
          Result: = ListItem;
          Col: = i;
          break;
        end; // if}
        Inc (ColStartX, Columns [i] .Width);
      end; // for
// Col: = i;
    end; // if
  end; // if
end;
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-16 02:15:01
| Show all posts
In the cloumnonclick event of Listview, cloumn can be used directly

You only need to use its cloumn.index to know which one you ordered
Reply

Use magic Report

0

Threads

1

Posts

1.00

Credits

Newbie

Rank: 1

Credits
1.00

 China

Post time: 2020-4-17 13:11:46
| Show all posts
listview.Selected is fine
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list