Recent

Author Topic: how change index of component in a groupBox?  (Read 5102 times)

Weiss

  • Full Member
  • ***
  • Posts: 168
how change index of component in a groupBox?
« on: January 23, 2024, 01:36:30 am »
its is not the first time I run into this issue. Apparently location of control in a groupBox doesn't mean this is how they will be iterated. How do I change the index of contained component?

I remember in the past deleting the whole groupbox and starting anew. Maybe there is a simple way of fixing the iterator for contained components? Can't find online. My delphi book is at home, 500 km away.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: how change index of component in a groupBox?
« Reply #1 on: January 23, 2024, 02:05:51 am »
I am not sure what you mean with Index since a TGroupBox is not having such a property.
However, iterating over the controls inside a TGroupBox, their order is defined in the .lfm file, in most cases it is defined at creating the TGroupBox, how you visual display them has no impact.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: how change index of component in a groupBox?
« Reply #2 on: January 23, 2024, 02:17:02 am »
I am not sure what you mean with Index since a TGroupBox is not having such a property.
However, iterating over the controls inside a TGroupBox, their order is defined in the .lfm file, in most cases it is defined at creating the TGroupBox, how you visual display them has no impact.

by index I mean that little pesky 'i' within [] brackets. You know, the integer one.
Code: Pascal  [Select][+][-]
  1. TSomeComponent(groupBoxSomething.Controls[ i ]).ComponentProperty := newValue;
By now I figured that components are displayed in object inspector in order they will be iterated. The user, however, does not have control over that order, other than deleting components and placing them again, paying particular attention to have them in correct sequence.

I was just wondering if there was easier way. Some deeply secret backdoor, only cool programmers would know.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: how change index of component in a groupBox?
« Reply #3 on: January 23, 2024, 02:31:45 am »
Some deeply secret backdoor, only cool programmers would know.
Sorry that my 100% correct and working answer is not cool enough for you, happy day and good doing!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: how change index of component in a groupBox?
« Reply #4 on: January 23, 2024, 03:11:54 am »
looking in .lfm file was a good suggestion. Haven't tried shuffling chunks of code in .lfm file, it appeared safer to re-populate groupBox, and probably quicker too.

It is just strange, there is "tab order", but no iteration sequence number for grouped components. Would be cool if component could be just dragged up or down the list in object inspector. Or if user could enter correct sequential number in properties, something like that.


KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: how change index of component in a groupBox?
« Reply #5 on: January 23, 2024, 03:44:50 am »
looking in .lfm file was a good suggestion.
From my side, it was not a suggestion, it is the "uncool" answer to your question.
I agree, elements and their order inside .lfm <-> object inspector should be handled via drag and drop.
Well, actually it is able to do all within Lazarus, you can reorder them by pulling items out and back in, it needs longer than simply start any text editor and move block(s) to the position(s) you want them to be.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: how change index of component in a groupBox?
« Reply #6 on: January 23, 2024, 04:23:28 am »
looking in .lfm file was a good suggestion.
...
Well, actually it is able to do all within Lazarus, you can reorder them by pulling items out and back in...

not if components are already anchored or aligned. If they are, I have walk all the way to anchor tab and pull the anchor out of the water, and THEN move the component out and back in. Easier to delete and place new.

lets be fair. This is uncool.

Same as text copy/paste keeping in mind which one is which, by their name of course, written down in correct sequence prior to moving them. Trying not accidentally disturb a single character out of bounds by placing mouse pointer carefully.

KodeZwerg, please admit you are not cool. Just ordinary.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: how change index of component in a groupBox?
« Reply #7 on: January 23, 2024, 04:36:43 am »
Thanks for your kind words, good to know how you treat someone that try to help.
Be happy, "mr-ordinary-uncool" will be in your future topics stay absent.
No more conversation, end of transmission.
Adios amigo
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 15487
  • Censorship about opinions does not belong here.
Re: how change index of component in a groupBox?
« Reply #8 on: January 23, 2024, 08:05:51 am »
You can change the order with Tcomponent.ComponentIndex [rw] property.This changes the order in the components. E.g. for iterating the components. This does not change the taborder, though.
You can change the taborder with the taborder property of the component.
TabOrder is a range of -1..32767.
« Last Edit: January 23, 2024, 08:25:10 am by Thaddy »
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

CCRDude

  • Hero Member
  • *****
  • Posts: 608
Re: how change index of component in a groupBox?
« Reply #9 on: January 23, 2024, 08:28:29 am »
ComponentIndex seems to be RunTime only though, not a published property?

From what I've seen in the code of SetZOrder with a quick peek, it seems that the "Z-Order" context menu in the form designer will actually change the order (your index "i") of items (it calls Move on the parents TFPList containing the controls).

Thaddy

  • Hero Member
  • *****
  • Posts: 15487
  • Censorship about opinions does not belong here.
Re: how change index of component in a groupBox?
« Reply #10 on: January 23, 2024, 09:04:17 am »
ComponentIndex is public, not published.  It is also read/write.
If OP means the taborder, I would simply set that in code.
Here's a demo. Press the button and tab through the controls. Repeat that a couple of times...
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.   // create a form with a button, three edits and a memo
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Edit1: TEdit;
  18.     Edit2: TEdit;
  19.     Edit3: TEdit;
  20.     Memo1: TMemo;
  21.     procedure Button1Click(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.Button1Click(Sender: TObject);
  38. var i:TTabOrder;
  39. begin
  40.   memo1.lines.clear;
  41.   // set the taborder equal to the component index
  42.   for i := 0 to form1.componentcount-1 do
  43.      begin
  44.        memo1.lines.add(Components[i].name);
  45.        TWincontrol(form1.components[i]).TabOrder := i;
  46.      end;
  47.   // now, change the component order a bit
  48.   // try to tab through the controls
  49.   form1.components[3].ComponentIndex:=1;
  50. end;
  51.  
  52. end.
« Last Edit: January 23, 2024, 10:59:31 am by Thaddy »
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

CCRDude

  • Hero Member
  • *****
  • Posts: 608
Re: how change index of component in a groupBox?
« Reply #11 on: January 23, 2024, 06:05:37 pm »
ComponentIndex is public, not published.  It is also read/write.
If OP means the taborder, I would simply set that in code.

It is just strange, there is "tab order", but no iteration sequence number for grouped components. Would be cool if component could be just dragged up or down the list in object inspector. Or if user could enter correct sequential number in properties, something like that.

He seems to be aware of tab order, and of reordering in .lfm, and even speaks about changing the "i" in groupBoxSomething.Controls, so z-order would be the - imho - only way to go.

I think in Delphi there's a form where items can be re-arranged, but I never really missed t.

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: how change index of component in a groupBox?
« Reply #12 on: January 24, 2024, 01:01:39 am »
thank you fellas, yes I meant component index, not tab order. Good suggestions, although this is academic question at this point.

So, since componentIndex is not published, it has to be set in the code, right? Can I tweak it at design time, other than what CodeZwerg suggested, copy/paste, move out/in.

What I was driving at, there must be an accessible array of components, in project resources somewhere, that I can redact?

Joanna

  • Hero Member
  • *****
  • Posts: 965
Re: how change index of component in a groupBox?
« Reply #13 on: January 24, 2024, 03:15:51 am »
If you put things into the group box at design time Their index will be in order placed.
You don’t need to delete them Just use the object inspector to drag them out from inside groupbox box and then return them in the order you want.

In the future you might want to try creating and adding things at runtime, it’s a lot easier to switch around later when you change your mind.  8)
« Last Edit: January 24, 2024, 03:29:26 am by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 15487
  • Censorship about opinions does not belong here.
Re: how change index of component in a groupBox?
« Reply #14 on: January 24, 2024, 03:39:08 pm »
Yes, the best way is to set it in code. But Joanna's suggestion apparently seems to work?
You seem to be a programmer that overly relies on visual programming, which is not always a good idea to become a real programmer. Anyway, my example should be clear enough, if you can also do it the reverse way: that is homework...
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

 

TinyPortal © 2005-2018