Forum > Beginners

First Item in ListBox by index/ID (solved)

(1/2) > >>

pentilisea:
The easy bit sometimes gets aekward.

With Gambas it's:
ListBox7[0].Text - and you have a string of the first Item of the listbox.

Tried a lot, but didn't find something working on my lazarus project.

Thanks for helping out without yawning.... but it gets a bit more wiered.

The basic idea is to transfer all items from ListBox1 to ListBox6.

Clear ListBox1 and then transfer all items of listbox6 back to
ListBox1 - but! in reverse order.

So result should be that previous first Item of ListBox1 is now
last item in ListBox1.

Trying all afternoon and if I had an Uzzi, I'd shoot the computer
if that helped....

So a txt file with 150 numbers, 1 number each line, is nicely loaded
into ListBox1, as needed for a lot of operations which all are working
OK.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---OpenDialog1.Filter:='txt-Dateien|*.txt';  if OpenDialog1.Execute then  ListBox1.Items.LoadFromFile(OpenDialog1.Filename);   for i:=ListBox1.Items.Count - 1 downto 0 do begin    ListBox1.Items[i] := Trim(ListBox1.Items[i]);    if not TryStrToInt(ListBox1.Items[i], iTest) then      ListBox1.Items.Delete(i);       end; //its designed to eliminate unwanted text 
The "First To Last" button has the following code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button96Click(Sender: TObject);  vari: Integer; begin   for i := ListBox1.Count -1 downto 0 do begin;  ListBox1.ItemIndex := ListBox1.Items.Count-1;   ListBox6.Items.Add((ListBox1.Items.Strings[Pred(ListBox1.Count -1)]));  ListBox1.Items.Delete(ListBox1.ItemIndex);  end;  ListBox6.ItemIndex := ListBox6.Items.Count-1;  ListBox1.ItemIndex := ListBox1.Items.Count-1;  ListBox1.Clear; end;                   
Although only haf of the task -
That does not work and LazTracer comes with some complaint.
If I set the downto to 1, instead of 0, hi, all goes well.
(I allways check that all the end; statements are wehre they should be...)

As said before, now all items of LixtBox 6 need to get back into
ListBox1 in revers order....

Maybe the operation can be done with using ListBox1 only.

paweld:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ShowMessage(ListBox7.Items[0]);  //or    //ShowMessage(ListBox7.Items.Strings[0]);

pentilisea:

--- Quote from: paweld on March 24, 2023, 03:08:29 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ShowMessage(ListBox7.Items[0]);  //or    //ShowMessage(ListBox7.Items.Strings[0]);
--- End quote ---

Thanks Pawl, works ok, ols see the extended post...

paweld:
Please do not edit the content of the post after answering, because the answer looks like out of context. If you have other questions then post them in new posts. 
As for your question about reordering, it can only be done with ListBox1 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var   i: Integer;  begin     ListBox1.Items.BeginUpdate;     i := ListBox1.Count - 1;    while i > 0 do    begin      ListBox1.Items.Move(0, i);      Dec(i);    end;    ListBox1.Items.EndUpdate;  end; 

KodeZwerg:

--- Quote from: pentilisea on March 24, 2023, 02:57:08 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button96Click(Sender: TObject);  vari: Integer; begin   for i := ListBox1.Count -1 downto 0 do begin; /// <<<<<<<<<< look how your line ends and when you use "i", why not use it at all?  ListBox1.ItemIndex := ListBox1.Items.Count-1;   ListBox6.Items.Add((ListBox1.Items.Strings[Pred(ListBox1.Count -1)]));  ListBox1.Items.Delete(ListBox1.ItemIndex);  end;  ListBox6.ItemIndex := ListBox6.Items.Count-1;  ListBox1.ItemIndex := ListBox1.Items.Count-1;  ListBox1.Clear;end;                  (I allways check that all the end; statements are wehre they should be...)
--- End quote ---
You should also check at "begin" what you doing.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  if (ListBox1.Count > 0) then    for i := Pred(ListBox1.Count) downto 0 do      begin        ListBox6.Items.Add((ListBox1.Items.Strings[i]));        ListBox1.Items.Delete(i);      end;Just another way of possibility

Navigation

[0] Message Index

[#] Next page

Go to full version