Recent

Author Topic: How to Redirect TListview TEdit editor column from default 1 to others  (Read 351 times)

erik2727

  • New member
  • *
  • Posts: 5
I have programme a listview in Lazarus ver 2.0.10  windows 10 OS which allow specific row editable upon mouse double clicks or user specific button click which will then add a new row to ListView1 with vsreport style

Below Button click event  snippet  resulted in two TEdit rectangles being open for use input with one under the  default column caption 1  and another target  column 3 , my question is   how to programmatically default  the cursor to only one specific column other than  the default column 1  defined by in-built editor ?



procedure TForm1.Button2Click(Sender: TObject);
  var
  lv_cnt:integer=0;
  Item :TListItem;
  sel_item:TListItem;
  LVeditor:TEdit;
  LVRect:TRect;
  begin
  LvItem:=nil;

  lv_cnt:=ListView1.Items.Count ;
 
  Item := ListView1.Items.Add;
  Item.Caption := FloatToStr(lv_cnt);
  Item.SubItems.Add('08/15/2020');
  Item.SubItems.Add('');
  Item.SubItems.Add('');
  Item.SubItems.Add('');
  Item.SubItems.Add('');
  Item.SubItems.Add('');
  Item.SubItems.Add('');

    ListView1.Selected:= ListView1.Items[item.Index];
     ListView1.Items[item.Index].Focused:=true;
     ListView1.ItemFocused.MakeVisible(true);

LVRect:=ListView1.Items[item.Index].DisplayRect(drSelectBounds) ;
       LVRect.Left:=200;
       LVRect.Right:=300;
        LvItem:=Item;
     LVeditor:=TEdit.Create(Self);
     LVeditor.Parent:=ListView1;
     LVeditor.Visible:=true;
     LVeditor.Enabled:=true;
     LVeditor.OnExit:=@EditorExit;
     LVeditor.OnKeyDown:=@Edit1KeyDown;
      LVeditor.OnKeyPress:=@KeyPress;
     
           LVeditor.BoundsRect:=LVRect;
     
    LVeditor.Text:= Item.SubItems[1] ;
       ListView1.ReadOnly:=false;
    ListView1.Update;
  ListView1.Show;
« Last Edit: August 07, 2020, 03:24:32 pm by erik2727 »

Handoko

  • Hero Member
  • *****
  • Posts: 3752
  • My goal: build my own game engine using Lazarus
Hello erik2727,
Welcome to the forum.

Can you please provide a compile-able demo so we can run and test it easily? I tried to create a demo based on the lines you provided, but I couldn't make it work. We can better understand the problem if we can run the code.

If you're not willing to publicize the whole source code, you can write a simplified demo that showing the issue.

To do it:
Create a new folder, copy and paste all the necessary files except: the binary (exe file), *.bak, lib and backup folders. Compress the folder and send the zip here.
« Last Edit: August 06, 2020, 07:23:19 pm by Handoko »

erik2727

  • New member
  • *
  • Posts: 5
Hi,
I  try to recreate simple unit showing the issue , after adding new record to listview , double click on any column will appear two Tedit box pending input , one  box appear in the first column , the other box formed at the  target bound column 3.

I know the issue is due to the TEdit component  , just could not find a way to destroy it  safely .
TEdit.Clear  just not enough to get rid of the Bound Rectangle , TEdit.Destroy just crash the Application.
Quick way is to set listview readonly to true to suppress the default column 1 in built editor, this however can not make use of in built editor and redirect to other column functional needs.

Appreciate some suggestion as how to destroy the TEdit.Create(Self).
Thanks
« Last Edit: August 07, 2020, 05:21:39 pm by erik2727 »

howardpc

  • Hero Member
  • *****
  • Posts: 3517
When you create an edit passing a non-Nil Owner (such as Self) you do not need to destroy the edit yourself. The Owner will free it. That is the whole point of the Owner property.
The attached example shows one simple way to do roughly what you are after.
« Last Edit: August 07, 2020, 08:20:52 pm by howardpc »

erik2727

  • New member
  • *
  • Posts: 5
Hi Howardpc ,

thanks for your suggestion , however I afraid I just couldn't get your form driven project to work in latest lazarus v2.0.10 windows10 .

My solution is to   get the specific subitems  TRect bounded region using    TRect:= ListView1.Items[row_sel].DisplayRectSubItem(2,drSelectBounds); bind and superimposed  with the similar width  TEdit against an always ReadOnly ListView  .

I read from Delphi article that the default in built  editor  ListView1.Items[ListView1.ItemIndex].EditCaption is  quite complicated to redirect  to other columns to accept user data input .

To detect which column is the click come from , I make use of   procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: integer);   

to return a global X position for evaluation as Y is not needed since which particular row is being selected can easily determine using
ListView1.ItemIndex once a click or double click  event is initiated of which the TEdit is then make visible and enable to accept data input.



« Last Edit: August 10, 2020, 03:20:55 am by erik2727 »

 

TinyPortal © 2005-2018