Recent

Author Topic: Associating a label and and a control  (Read 3696 times)

coucout

  • Newbie
  • Posts: 3
Associating a label and and a control
« on: October 30, 2021, 01:00:58 pm »
Hello,

I'm used to work with PC Soft Windev. In this IDE, each control has an associated caption (that you can display or not). So i you move the control, the label move with it because there isn't 2 controls but only one.

What is the best way to do that in Lazarus ? I think to TPanel to assemble TEdit (for example) and TLabel together but I don't know if it is the best solution.

Another question : If I've created a control and a label, how to move them in a TPanel. Because, if I move them in the components treeview, they become invisible. Wich property must I set to become them visible ?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Associating a label and and a control
« Reply #1 on: October 30, 2021, 01:21:55 pm »
There are various options, none is ideal.

You can use two separate controls, one an associated label, and use the controls' anchoring properties to keep them together when dropped on a form, and when moved.

You can also place any control within a TGroupBox and use its Caption as the associated label.

For edits, you can use TLabeledEdit. But IMO this LCL control has severe shortcomings, so I never use it.

Or you can design your own hierarchy of TLabelledxxx controls which have a built-in label (and it is not hard to improve on TLabeledEdit).

wp

  • Hero Member
  • *****
  • Posts: 13414
Re: Associating a label and and a control
« Reply #2 on: October 30, 2021, 01:49:24 pm »
Another question : If I've created a control and a label, how to move them in a TPanel. Because, if I move them in the components treeview, they become invisible. Wich property must I set to become them visible ?
Yes, you a drag a control in the component tree to its new parent: Select the edit in the tree and drag it immediately below the panel item so that the cursor indicates that insertion is allowed, release the mouse button now. The reason why you cannot see the edit any more on the designer form, is that the edit keeps is left/top coordinates. Before the drag operation they were relative to the form, now they are relative to the panel, and if the panel is small they may be beyond the bounds of the panel. To fix this select the edit in the tree (which is a child of the panel, now), and set its Left and Top to 0 - now the edit should be visible in the top/left corner of the panel, and you can drag it in the designer to the place where you want it.

If you want to combine a label and a control (say: a TCombobox) within a panel you should anchor the controls to the panel so that they have a well-defined and fixed position within the panel. At first drop a label on the panel. Open the Anchor Editor and select the panel in the top and left anchoring sibling comboboxes. Now drop the combobox on the panel. In the anchor editor link its left side to the left side of the panel as you did with the label. Do the same with the top side, but click the lower of the three "top anchoring" buttons; this moves the combobox below the label. Enter the spacing between label and combobox in the upper edit field of the "Border space" group when the combobox is selected (or in the lower edit field of the same group when the Label is selected). Finally set the panel's AutoSize to true. Now the panel closely  embraces both controls.

If you want to change the width of the panel you must change the width of the combobox (because the panel width is defined by the maximum of label and combobox width). Note that you can move that group only by clicking on empty space not covered by label or combobox.

So, working with anchored controls is a bit counter-intuitive especially for a beginner. But the anchor editor is one of the most powerful tools of Lazarus.

If you need the same combined controls over and over again in the same project you should use a TFrame rather than a TPanel as container of the label and the other control. Select "File" > "New" > "Frame". Add a label and a combobox to the frame and anchor them as described for the panel. Store the frame with an appropriate name, e.g. TLabeledComboboxFrame. In order to insert the frame into a form of your application click on the "Frame" icon in the component palette, page "Standard" at the right, your are prompted which frame is to be selected. Finally drop it on the form like any other component.
« Last Edit: October 30, 2021, 02:14:51 pm by wp »

jamie

  • Hero Member
  • *****
  • Posts: 7601
Re: Associating a label and and a control
« Reply #3 on: October 30, 2021, 11:27:27 pm »
Wouldn't a TFlowControl be appropriate for such things?

Drop a single control in there of what ever and then insert the label, in this case it may need to be a static label.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7601
Re: Associating a label and and a control
« Reply #4 on: October 30, 2021, 11:40:06 pm »
btw, I just tried using a TFlowControl with the trunk, it crashes the IDE totally when you drop a Tbutton on the flow panel..

Guess this is a regression because there were issues with this control before doing such things, now we are back to it again!
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7601
Re: Associating a label and and a control
« Reply #5 on: October 30, 2021, 11:57:01 pm »
I meant to say TFlowPanel
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 13414
Re: Associating a label and and a control
« Reply #6 on: October 31, 2021, 01:21:45 am »
btw, I just tried using a TFlowControl with the trunk, it crashes the IDE totally when you drop a Tbutton on the flow panel..
It's not only a button which causes the problem, I can drop any other control and get the same issue. It happens when I move the mouse over the dropped control. However, this does not occur when the control is added at runtime.

Please report this. There was some work with the object inspector some months ago, and I guess that there is still some unreckognized issue. Assign the report to Juha.

jamie

  • Hero Member
  • *****
  • Posts: 7601
Re: Associating a label and and a control
« Reply #7 on: October 31, 2021, 02:19:53 am »
I did make a report and I also had to correct the mistake I made for the name of the control..

However, I do not see any method on the page to assign the report to Juha
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018