user1557219
user1557219

Reputation: 23

Groupbox 1 or 2; Based on which Checkbox is chosen after Button Click

On the form I have two groupboxes, two checkboxes and one button. In the button code, I am currently having the following two lines:

if CheckBox1.Checked then GroupBox1.Show; 
if CheckBox2.Checked then GroupBox2.Show;

What I would like to have is; after I click button,

IF checkbox1 is checked then groupbox1 should appear and IF checkbox2 is checked then groupbox2 should appear.

However there are two further requirements that are confusing me:

  1. Neither of two groupbox should be shown on the form before I press the mentioned button.
  2. Both groupboxes (as said, which one appears depends on which checkbox is chosen after button click) should appear on the exact same location of the form. But I cannot set on exact location two different elements (in my case groupboxes).

Version of delphi is 7.

Upvotes: 2

Views: 3056

Answers (3)

Arioch 'The
Arioch 'The

Reputation: 16045

Neither of two groupbox should be shown on the form

set .Visible to false in Delphi Object Inspector before u compile the program

Did you read help on .Show ? it mentions that property. look SEE ALSO section http://docwiki.embarcadero.com/Libraries/XE2/en/Vcl.Controls.TControl.Show

Frankly, more laconic would be listbox.visible := checkbox.checked


should appear on the exact same location of the form

Well, you cannot show them at the same time at the same place ?

How do you want to show them BOTH if BOTH the checkboxes are on ?

I believe you should remove checkboxes and put radiobuttons instead that only one or another can be pressed.

Try the TRadioGroup

http://docwiki.embarcadero.com/Libraries/XE2/en/Vcl.ExtCtrls.TRadioGroup.ItemIndex

 ~~~~~~~~~~~~~~~~~~~
 |  ( )   No lists |
 |  (*)   List 1   |
 |  ( )   List 2   |
 ~~~~~~~~~~~~~~~~~~~

RadioGrououp.OnClick := ... (* procedure .... List1.Visible := RadioGroup.ItemIndex = 1; List2.Visible := RadioGroup.ItemIndex = 2; end. *)


Both groupboxes should appear on the exact same location

1) Use TNotebook and place them on different pages. Switch pages instead of toggling visibility. That approach would allow you make many more controls, not only those. And you can palce them at same coords on different pages easily.

2) in form.OnShow: List2.Top := List1.Top and same for Left, Width and Height

Upvotes: 1

Whiler
Whiler

Reputation: 8086

As your 2 GroupBox are at the same position, I'd use radiobutton instead of checkbox as the 2 groupbox can't be displayed at the same time...

Here is an example:

// Click event for a radiogroup see DFM below
procedure TForm1.rg1Click(Sender: TObject);
begin
  gb1.Visible := False;
  gb2.Visible := False;
  if rg1.ItemIndex = 0 then
    gb1.Visible := True
  else
    gb2.Visible := True;
end;

The form is designed like this (you can look the different properties I set):

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form6'
  ClientHeight = 337
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object gb1: TGroupBox
    Left = 40
    Top = 60
    Width = 185
    Height = 105
    Caption = 'gb1'
    TabOrder = 0
    Visible = False
  end
  object gb2: TGroupBox
    Left = 40
    Top = 60
    Width = 185
    Height = 105
    Caption = 'gb2'
    TabOrder = 1
    Visible = False
  end
  object rg1: TRadioGroup
    Left = 40
    Top = 8
    Width = 185
    Height = 33
    Caption = 'rg1'
    Columns = 2
    Items.Strings = (
      'GB1'
      'GB2')
    TabOrder = 2
    OnClick = rg1Click
  end
end

Upvotes: 1

mj2008
mj2008

Reputation: 6747

Try this:

GroupBox1.Visible := CheckBox1.Checked;
GroupBox2.Visible := CheckBox2.Checked;

Upvotes: 2

Related Questions