Recent

Author Topic: ListBox delete item  (Read 12499 times)

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
ListBox delete item
« on: December 24, 2014, 08:03:41 pm »
I tried:
    if (myListBox.ItemIndex > -1) then
        myListBox.Items.Delete(myListBox.ItemIndex);
also I tried this after delete:
        myListBox.Items.Objects[currIndex].Free; {currIndex = myListBox.ItemIndex}

But always got an error message(in both case): Invalid floating point.

Lazarus 1.2.6 Fpc 2.6.4 32bit (standard) Debian 7.7. 32bit
Suggestions?
 Thank you

p.s. after clear(listbox.clear or listbox.items.clear) lclproc error
« Last Edit: December 24, 2014, 08:28:49 pm by exdatis »

kapibara

  • Hero Member
  • *****
  • Posts: 662
Re: ListBox delete item
« Reply #1 on: December 24, 2014, 11:57:59 pm »
I think Object should be deleted first and the Item last:

Code: [Select]
  if (myListBox.ItemIndex > -1) then
  begin
    myListBox.Items.Objects[myListBox.ItemIndex].Free;
    myListBox.Items.Delete(myListBox.ItemIndex);
  end;
Lazarus trunk / fpc 3.3.1 / Kubuntu 24.04 - 64 bit

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: ListBox delete item
« Reply #2 on: December 25, 2014, 08:54:48 am »
Thank you for your response, I tried it but unsuccessful, lclproc error.

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: ListBox delete item
« Reply #3 on: December 25, 2014, 12:48:29 pm »
Did you store objects in the listbox?
If not, there is no need for freeing them obviously.

Does your code also fail on a minimal testcase (1 form, 1 listbox with > 0 items)?
If not, it most likely is something in your own code.

Bart

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: ListBox delete item
« Reply #4 on: December 25, 2014, 01:33:48 pm »
@Bart, just one listbox with text-items(attachment-path), nothing more. Thanks for response

p.s.Instead TListBox I use TListView, works excellent(the same form)see picture:

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: ListBox delete item
« Reply #5 on: December 25, 2014, 02:46:32 pm »
Attached sample project works for me on Fedora
Lazarus 1.3, fpc 2.6.2, GTK2

Please test.

Bart

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: ListBox delete item
« Reply #6 on: December 25, 2014, 08:32:35 pm »
It seems to be simle:
myListBox.Items.Delete(myListBox.ItemIndex-1);
Because myListBox.ItemIndex is amount of listbox items, and items start count from 0, i.e. the last item index is myListBox.ItemIndex-1

{$R+}{$Q+} usually really helps with such errors.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: ListBox delete item
« Reply #7 on: December 25, 2014, 09:22:36 pm »
@Bart, thank you very much. I tried your example, it works perfectly.  That's confusing me because I can't remember any of differences in my source code(I cleanup my code and I replace it with TListView, it works as I expected). Thank you very much.

@Eugene, thank you but it's not the point because if I delete 3th of 5 items I've got the same error  in my tests.

Thank you guys!

p.s. from now TListView is my first and best choice  :)

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: ListBox delete item
« Reply #8 on: December 25, 2014, 09:35:45 pm »
After completing the application I will publish a part of it as an example of sending mail with attachments using synapse(synalist) library.
Regards
p.s. I enjoy using synalist lib. It's so powerful and easy to use.

kapibara

  • Hero Member
  • *****
  • Posts: 662
Re: ListBox delete item
« Reply #9 on: December 25, 2014, 09:55:47 pm »
Thats much appreciated, synapse needs more documentation.

After completing the application I will publish a part of it as an example of sending mail with attachments using synapse(synalist) library.
Regards
p.s. I enjoy using synalist lib. It's so powerful and easy to use.
Lazarus trunk / fpc 3.3.1 / Kubuntu 24.04 - 64 bit

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: ListBox delete item
« Reply #10 on: December 25, 2014, 10:05:44 pm »
I agree with a few examples and explanations that will be easy to use and understand.

p.s eg.
{ original code}
Code: [Select]
function SendToRaw(const MailFrom, MailTo, SMTPHost: string;
  const MailData: TStrings; const Username, Password: string): Boolean;
var
  SMTP: TSMTPSend;
  s, t: string;
begin
  Result := False;
  SMTP := TSMTPSend.Create;
  try
// if you need SOCKS5 support, uncomment next lines:
    // SMTP.Sock.SocksIP := '127.0.0.1';
    // SMTP.Sock.SocksPort := '1080';
// if you need support for upgrade session to TSL/SSL, uncomment next lines:
    SMTP.AutoTLS := True;
// if you need support for TSL/SSL tunnel, uncomment next lines:
    SMTP.FullSSL := True;
    SMTP.TargetHost := Trim(SeparateLeft(SMTPHost, ':'));
    s := Trim(SeparateRight(SMTPHost, ':'));
    if (s <> '') and (s <> SMTPHost) then
      SMTP.TargetPort := s;                   
.......

So, probably you'll uncomment : SMTP.AutoTLS := True and
SMTPHost: string will be something like : smtp.googlemail.com:465 ...
« Last Edit: December 25, 2014, 10:13:57 pm by exdatis »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: ListBox delete item
« Reply #11 on: December 25, 2014, 10:30:43 pm »
It seems to be simle:
myListBox.Items.Delete(myListBox.ItemIndex-1);
Because myListBox.ItemIndex is amount of listbox items, and items start count from 0, i.e. the last item index is myListBox.ItemIndex-1
False, ItemIndex is always something from -1 .. Count-1. If it's -1 then it's either never given a value or count is 0. myListBox.Count would return amount of items in list, ItemIndex is merely the list index that is currently selected.

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: ListBox delete item
« Reply #12 on: December 26, 2014, 01:23:24 am »
Oooh.. sorry, my bad. Mixed itemindex and items.count...
« Last Edit: December 26, 2014, 07:27:43 am by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

 

TinyPortal © 2005-2018