Recent

Author Topic: Edit ListBox item on double click  (Read 697 times)

Hansvb

  • Sr. Member
  • ****
  • Posts: 474
Edit ListBox item on double click
« on: March 19, 2023, 06:15:41 pm »
How can you edit a listbox item when the user double clicks on an item?
I searched but did only find one example which i can't get to work. This example is:
Code: Pascal  [Select][+][-]
  1. https://www.tek-tips.com/viewthread.cfm?qid=1470817

The line
Code: Pascal  [Select][+][-]
  1. ed.OnExit := EditDone;
gives the next error:
Code: Pascal  [Select][+][-]
  1. Error: Incompatible types: got "untyped" expected "<procedure variable type of procedure(TObject) of object;Register>"



bytebites

  • Hero Member
  • *****
  • Posts: 550
Re: Edit ListBox item on double click
« Reply #1 on: March 19, 2023, 06:39:33 pm »
Code: Pascal  [Select][+][-]
  1. ed.OnExit := @EditDone;

Hansvb

  • Sr. Member
  • ****
  • Posts: 474
Re: Edit ListBox item on double click
« Reply #2 on: March 19, 2023, 07:05:13 pm »
Thanks i have the example working but it does not what i want. After editing i leaves the editbox in the listbox.

How can i edit a listbox item when i double click it?



KodeZwerg

  • Hero Member
  • *****
  • Posts: 1398
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Edit ListBox item on double click
« Reply #3 on: March 19, 2023, 07:38:52 pm »
ListView is the better thing to use I guess, anyway, this is how you can modify an entry from TListBox by doing a double click
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. var
  3.   lb: TListBox;
  4.   s: string;
  5. begin
  6.   lb := (Sender as TListBox);
  7.   if ((lb = nil) or (lb.ItemIndex < 0)) then
  8.     Exit;
  9.   s := 'Edited'; // or put here anything else in that fills "s" with a value, like a TEdit for example.
  10.   lb.Items[lb.ItemIndex] := s;
  11. end;
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Hansvb

  • Sr. Member
  • ****
  • Posts: 474
Re: Edit ListBox item on double click
« Reply #4 on: March 19, 2023, 07:51:47 pm »
I will look at it tomorrow.
Listview could be the better component. I have to try it.

jamie

  • Hero Member
  • *****
  • Posts: 5350
Re: Edit ListBox item on double click
« Reply #5 on: March 19, 2023, 08:57:01 pm »
That example you linked to has leaks in it, the EDIT control isn't being freed.

In any case, you can use a TStringGrid. .

 Set fixed columns to 0 and cell count to what you want. Turn off the grid lines etc.

 In the end you get what looks like a ListBox but the string grid has Editors built in that you can configure.
The only true wisdom is knowing you know nothing

cdbc

  • Sr. Member
  • ****
  • Posts: 493
    • http://www.cdbc.dk
Re: Edit ListBox item on double click
« Reply #6 on: March 20, 2023, 01:34:56 am »
Hi
Building on KodeZwergs example, couldn't you do this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. var
  3.   lb: TListBox;
  4.   s: string;
  5. begin
  6.   lb := (Sender as TListBox);
  7.   if ((lb = nil) or (lb.ItemIndex < 0)) then
  8.     Exit;
  9.   // inputbox('caption','message','default text'); -> returns new text or untouched string
  10.   s := InputBox('Edit Item','Please enter new text for item:',lb.Items[lb.ItemIndex]);
  11.   // i  think this should work, untested...
  12.   lb.Items[lb.ItemIndex] := s;
  13. end;
  14.  
Just my 2 cents...  :)
Regards Benny
« Last Edit: March 20, 2023, 08:51:43 am by cdbc »
If it ain't broke, don't fix it ;)

dseligo

  • Hero Member
  • *****
  • Posts: 984
Re: Edit ListBox item on double click
« Reply #7 on: March 20, 2023, 02:50:02 am »
Try project in attachment.
It's based on your link. I added handling of enter and escape keys.
It works on Windows 11.

Hansvb

  • Sr. Member
  • ****
  • Posts: 474
Re: Edit ListBox item on double click
« Reply #8 on: March 25, 2023, 05:05:17 pm »
Almost solved. I changed TEdit for TCombobox. After the combobox is created i need to click it twice to open the dropdownlist. Can i simulate a mousclick?

jamie

  • Hero Member
  • *****
  • Posts: 5350
Re: Edit ListBox item on double click
« Reply #9 on: March 25, 2023, 05:42:38 pm »
Hmm
 Using my old Lazurus I see the AutoDropDown drops down when you select a different control.

 Using the Trunk, it seems AutoDropDown does not work anymore, does nothing.


 as for the older test, all I did was change the TAB order and that cause the combobox to drop down when I clicked on a different control.

  But like i said, don't seem to work anymore in trunk, unless there is some secret code I 'am missing?

 EDIT:
   Apparently there is a bug in my older version of Lazurus.
  I am not going to worry about it.
 
But in any case you can use the DroppedDown property at anytime to drop it, in the OnEnter, onClick etc.
« Last Edit: March 25, 2023, 05:49:51 pm by jamie »
The only true wisdom is knowing you know nothing

Hansvb

  • Sr. Member
  • ****
  • Posts: 474
Re: Edit ListBox item on double click
« Reply #10 on: March 26, 2023, 10:51:21 am »
Looks like  I found it.

This creates a TComboBox dynamically and when you click on the arrow down the itemslist collapse. No need anymore to click it twice.

Code: Pascal  [Select][+][-]
  1. procedure TFrm_main.ListBoxOnDblClick(Sender: TObject);
  2. var
  3.   r: TRect;
  4.   cb: TComboBox;
  5.   Level : Byte;
  6.   itemsNames : AllItemsObjectData;
  7.   i : Integer;
  8. begin
  9.   FCurrListBox := Sender as TListbox;
  10.   if FCurrListBox.ItemIndex < 0 then Exit;
  11.  
  12.   cb := TComboBox.Create(Self);
  13.   cb.Font.Size := 9;
  14.   r := FCurrListBox.ItemRect(FCurrListBox.itemindex);
  15.   r.topleft := FCurrListBox.ClientToScreen(r.topleft);
  16.   r.BottomRight := FCurrListBox.clienttoscreen(r.bottomright);
  17.   r.topleft := ScreenToClient(r.topleft);
  18.   r.BottomRight := screenToClient(r.bottomright);
  19.  
  20.   Level := StrToInt(GetNumbers(FCurrListBox.Name));
  21.   itemsNames := GetListBoxItems(Level);
  22.   for i := 0 to Length(itemsNames)-1 do begin
  23.     cb.Items.Add(itemsNames[i].Name);
  24.   end;
  25.  
  26.   cb.Text :=  FCurrListBox.Items[FCurrListBox.itemindex];
  27.   cb.setbounds(r.left, r.top-2, FCurrListBox.clientwidth, r.bottom - r.top + 4);
  28.   cb.OnExit := @ComboBoxDone;
  29.   cb.OnMouseDown:= @ComboBoxMouseDown;
  30.   cb.OnKeyUp := @ComboBoxKeyUp;
  31.   cb.Parent := Self;
  32. //  SetCapturecontrol(cb);  <== when active you have to click the drowdown arrow twice.  
  33.   cb.SetFocus;
  34. end;  

 

TinyPortal © 2005-2018