Lining up checkboxes on a form

calebS used Ask the Experts™
I have a form, which has three checkboxes in it.
I want the checkboxes to always be visible in exactly the same positions.
ie if the form is resixed, then the checkboxes change size.

I don't want to just fix the forms onto the page, because that may look good
on my screen. but at a different screen resolution the user will either have
to scroll alot, or it will look like only half the page has been used.

Any suggestions??

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Go to your Form's Events in the Object Insector and Double click on the OnResize event, then place code here for the positions of relocatable Controls - - -

CheckBox1.Left := Form1.ClientRect.Right - CheckBox1.Width - 10;

CheckBox1.Top := Form1.ClientRect.Bottom - CheckBox1.Height - 16;


I had attempted something like that, but it is not working.

void __fastcall TInputWin::FormResize(TObject *Sender)
        ValueListEditor1->Top = 20;
        ValueListEditor1->Left = 20;
        ValueListEditor1->Height = InputWin->Height - 40;
        ValueListEditor1->Width = InputWin->Width / 4;

        Label1->Top = 20;
        Label1->Left = ValueListEditor1->Width + 40;

        CheckListBox1->Top = Label1->Top + Label1->Height + 10;
        CheckListBox1->Left = Label1->Left;
        CheckListBox1->Height = InputWin->Height*(2/3) - Label1->Top;
        CheckListBox1->Width = InputWin->Width - CheckListBox1->Left - 20;

        Label2->Top = CheckListBox1->Top + CheckListBox1->Height + 20;
        Label2->Left = Label1->Left;

        CheckListBox2->Top = Label2->Top + Label2->Height + 10;
        CheckListBox2->Left = CheckListBox1->Left;
        CheckListBox2->Height = InputWin->Height - CheckListBox2->Top - 20;
        CheckListBox2->Width = InputWin->Width - CheckListBox2->Left - 20;

try to relocate your controls one at a time, to see if it works, first of all it is wasted code to set a control to a constant value, like your line -

ValueListEditor1->Left = 20;
just set it to 20 in your object inspector and leave this line out.

I do not think you are grasping the Idea, If this is a single line edit, you change the Height of the Edit, but you should be changing the Top and width

Next, use the parent's ClientRect for your reference.

ValueListEditor1->Top = InputWin->ClientRect->Bottom - ValueListEditor1->Height - 40;
ValueListEditor1->Width = InputWin->ClientRect->Right / 4;
Label1->Left = ValueListEditor1->Width + 40;

CheckListBox1->Top = Label1->Top + Label1->Height + 10;
//you have the Label1->Top as a constant 20, so the above line is not needed in a resize

the rest of your code does not make any sense to me, you are ajusting the height and width of a check box, I don't get it.

the code below should change the position of the Edit and the CheckBox, so the checkbox stays under the Edit and the Edit grows in width with the form

void __fastcall TInputWin::FormResize(TObject *Sender)
Edit1->Top = Form1->ClientRect->Bottom - Edit1->Height - CheckBox1->Height -40;
Edit1->Width = Form1->ClientRect->Right / 10;
CheckBox1->Top = ClientRect->Bottom - CheckBox1->Height -30;

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.


I shall try that.
though note, it is not a checkBox, but a CheckListBox.

But I do need to adjust the height as well.
Becuase the form is resize in both Width and Height, so I would like the lists on the form to respond correctly.

You can use percentage resizing in your delphi program. For example, i show horizontal resizing code for Resize function for newly created component or OnResize event for application program:

int p[2]= {30, 40}; for first and cesont checkboxes of 3 checkboxex:
CheckBox1->Left = 0
CheckBox1->Width = Width * p[0] / 100;
CheckBox2->Left = CheckBox1->Width;
CheckBox2->Width = Width * p[1] / 100;
CheckBox3->Left = CheckBox1->Width + CheckBox2->Width;
CheckBox3->Width = Width - (Width * (p[0] + p[1]) / 100);

here the same code with indents:

CheckBox1->Left = 8;
CheckBox2->Left = Width * p[0] / 100 + 4;
CheckBox3->Left = Width * p[1] / 100 + 4;
CheckBox1->Width = CheckBox2->Left - 12;
CheckBox2->Width = CheckBox3->Left - CheckBox2->Left - 8;
CheckBox3->Width = Width - CheckBox3->Left - 12;

In order to perform simulaneous resize operation for any one checkbox you can use SetBounds method.


You helped, though I am not sure you realise why.
The problem was this.

Instead of:
Form->ValueList->Height = Form->Height / 2;
I had to type:
Form->ValueList->Height = Form->ClientHeight / 2;

Small difference, but that aside from that, everything else was correct.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial