Recent

Author Topic: Manual set height of button  (Read 533 times)

las

  • New Member
  • *
  • Posts: 26
Manual set height of button
« on: September 17, 2019, 10:46:48 pm »
Hi!

I put two buttons on the form jButton1, jButton2.

The text in jButton1 takes 2 lines
The text in jButton2 takes 1 lines

Coded:

jButton1.LayoutParamHeight := lpWrapContent;
jButton2.LayoutParamHeight := lpExact;
jButton2.height:=jButton1.height;
JButton2.UpdateLayout;

But height of jButton2 not equal height of jButton1 on screen.
Why? What wrong?

zorfox

  • New member
  • *
  • Posts: 5
Re: Manual set height of button
« Reply #1 on: September 17, 2019, 11:52:08 pm »
This works but may cause issues across devices... 

jButton1.LayoutParamHeight:=lpExact;
  jButton1.Height := 300;

  jButton2.LayoutParamHeight:=lpExact;
  jButton2.Height := 300;

las

  • New Member
  • *
  • Posts: 26
Re: Manual set height of button
« Reply #2 on: September 18, 2019, 12:01:07 am »
The problem is in different devices. It is necessary that the size of the large button is determined automatically.

zorfox

  • New member
  • *
  • Posts: 5
Re: Manual set height of button
« Reply #3 on: September 18, 2019, 06:56:18 pm »
There is no [left , top, width, height] exposed at run time. As far as I know, height properties will need to be derived from the parent. Unfortunately, jButton2.Parent := jButton1 won't work. You'll need to use some sort of container such as a jPanel.

Code: Pascal  [Select][+][-]
  1.   jButton1.Parent := jPanel1;
  2.   jButton1.LayoutParamHeight := lpMatchParent;
  3.   jButton1.PosRelativeToParent := [rpTop,rpLeft];
  4.   jButton1.Text := 'This is' + #10 + 'two lines';
  5.  
  6.   jButton1.Parent := jPanel1;
  7.   jButton2.Anchor := jButton1;
  8.   jButton2.PosRelativeToAnchor := [raToRightOf,raAlignTop];
  9.   jButton2.LayoutParamHeight:=lpMatchParent;
  10.   jButton2.Text := 'This is one';
  11.  

Here is a good explanation about auto-sizing and layout on the lazarus Wiki page. Maybe you can come up with a better solution after digesting that.
https://wiki.freepascal.org/Autosize_/_Layout

las

  • New Member
  • *
  • Posts: 26
Re: Manual set height of button
« Reply #4 on: September 18, 2019, 10:23:04 pm »
In your example, the height of the buttons is generally not clear what, because the height of the jPanel is not tied to the height of jButton1, which is automatically calculated.

 

TinyPortal © 2005-2018