Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pagecontrol access controls in a groupbox

Status
Not open for further replies.

p27br

Programmer
Aug 13, 2001
516
GB
hello

i have a form with a pagecontrol. On each page, there are several groupboxes with controls inside.
How can I access the value stored in each control of a groupbox ?

thanks


 
For this little example, I dropped a TPageControl on a form, and dropped 2 TGroupBox controls inside it. In each groupbox I dropped a TCheckBox. The code below finds the checkboxes and displays a message confirming whether they are checked or unchecked. The important properties in all of this are: Controls and ControlCount. Have a read in the Delphi help.
Code:
var
  i, j: Integer;
  groupBox: TGroupBox;
  checkBox: TCheckBox;
begin
  for i := 0 to PageControl1.Pages[0].ControlCount - 1 do
  begin
    if PageControl1.Pages[0].Controls[i] is TGroupBox then
    begin
      groupBox := (PageControl1.Pages[0].Controls[i] as TGroupBox);
      for j := 0 to groupBox.ControlCount - 1 do
      begin
        if groupBox.Controls[j] is TCheckBox {or whatever} then
        begin
          checkBox := (groupBox.Controls[j] as TCheckBox);
          if checkBox.Checked then
            ShowMessage(checkBox.Name + ' is checked')
          else
            ShowMessage(checkBox.Name + ' is unchecked');
        end;
      end;
    end;
  end;
end;

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
clive,

many thanks for the answer, the problem is I would like to cycle through all the controls in the groupbox and get their value.
In VB, i would do
Code:
dim ctrl as control
for each ctrl in groupbox.controls
  msgbox ctrl.value
next ctrl

but this doesn't work :
Code:
for j := 0 to groupBox.ControlCount - 1 do
begin
  stream.Write(groupbox.Controls[j].Value)
end;

how can i achieve this in Delphi ?

thanks
 
The Controls property returns objects of type TControl (which is "the base class for all components that are visible at runtime" i.e. a class that VCL components all share). What you need to do is cast each TControl to its proper type as I did with the checkbox, in order to access properties specific to that control.

So depending on the controls contained in your groupbox you would need to provide code to handle those that are relevant. Below I have amended the code to handle TEdit and TRadioButton controls as well as TCheckBox:
Code:
var
  i, j: Integer;
  groupBox: TGroupBox;
  checkBox: TCheckBox;
  radioButton: TRadioButton;
begin
  for i := 0 to PageControl1.Pages[0].ControlCount - 1 do
  begin
    if PageControl1.Pages[0].Controls[i] is TGroupBox then
    begin
      groupBox := (PageControl1.Pages[0].Controls[i] as TGroupBox);
      for j := 0 to groupBox.ControlCount - 1 do
      begin
        // check if control is a TCheckBox
        if groupBox.Controls[j] is TCheckBox then
        begin
          checkBox := (groupBox.Controls[j] as TCheckBox);
          if checkBox.Checked then
            ShowMessage(checkBox.Name + ' is checked')
          else
            ShowMessage(checkBox.Name + ' is unchecked');
        end;

        // check if control is a TEdit
        if groupBox.Controls[j] is TEdit then
          ShowMessage((groupBox.Controls[j] as TEdit).Text);

        // check if control is a TRadioButton
        if groupBox.Controls[j] is TRadioButton then
        begin
          radioButton := (groupBox.Controls[j] as TRadioButton);
          if radioButton.Checked then
            ShowMessage(radioButton.Name + ' is checked')
          else
            ShowMessage(radioButton.Name + ' is unchecked');
        end;
      end;
    end;
  end;
end;
Note: you do not need to create a local variable and assign to it for each control - but it makes it easier to read if you're using the control a lot. Notice with the TEdit I didn't assign it to a local variable of type TEdit.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
clive

that's really a pain. I have a bunch of TEdits and TComboBoxes and I thought I could retrieve their Value property by using polymorphism of the TControl class.
 
Neither TEdit, TComboBox nor TControl have a Value property. What property do you need to access from the TEdit and the TComboBox? I suspect it will be different for each component so you cannot retrieve it as you would like.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
well in the help for TControl.Text, it says

Contains a text string associated with the control.

property Text: TCaption;

Description

Use the Text property to read the Text of the control or specify a new string for the Text value. By default, Text is the control name. For edit controls and memos, the Text appears within the control. For combo boxes, the Text is the content of the edit control portion of the combo box.

Note: Controls that display text use either the Caption property or the Text property to specify the text value. Which property is used depends on the type of control. In general, Caption is used for text that appears as a window title or label, while Text is used for text that appears as the content of a control.

As Edits and Comboboxes inherit from TControl, I thought they might share a .Text property that I could access when cycling through the controls in the groupbox, without specifying the exact type of the object because they inherit the properties from TControl
 
TEdit and TComboBox do indeed share a Text property. However, if you look in the Controls unit you will see that the Text property is declared in the "protected" section which means it will only be visible to classes derived from TControl. If it were declared in the "public" section then you would be able to do the above by a call such as:
Code:
groupBox.Controls[j].Text;
As I see it your only solution is to write code to check if the TControl is a TEdit or a TComboBox and access each controls Text property accordingly, as at this level it has been made public and is accessible.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I've had an idea! One way you could get round this is to populate each control's Hint property with something meaningful then access it using the following code:
Code:
var
  i, j: Integer;
  groupBox: TGroupBox;
begin
  for i := 0 to PageControl1.Pages[0].ControlCount - 1 do
  begin
    if PageControl1.Pages[0].Controls[i] is TGroupBox then
    begin
      groupBox := (PageControl1.Pages[0].Controls[i] as TGroupBox);
      for j := 0 to groupBox.ControlCount - 1 do
      begin
        ShowMessage(groupBox.Controls[j].Name + ': ' + groupBox.Controls[j].Hint);
      end;
    end;
  end;
end;

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
ok so the problem is with the protected or published properties.
i think i'll follow your suggestion, testing for the type and casting before using the control's value.

but i don't understand why i can't access a protected property from an inheriting object of the TControl Class.

thanks for your help

Paul
 
>>but i don't understand why i can't access a protected property from an inheriting object of the TControl Class.

That's exactly what you are doing when you cast. You are casting it to an object derived from TControl, which gives you public access to the Text property.

When you access the TGroupBox.Controls property, you are accessing TControl objects which do not make their Text property visible.


Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
clive

i did a little more research and found the 2 following methods that allow to use the Text property of a TControl object :

.GetTextLen
.GetTextBuf

Here is the code I use to get all the .Text values from the controls in the groupbox :

Code:
var
  Buffer : PChar;
  Size : Byte;
  strValue : string[255];

begin
        for j := 0 to groupBox.ControlCount - 1 do
        begin
            size := groupBox.Controls[j].gettextlen;
            inc(size);
            getmem(buffer,size);
            groupBox.Controls[j].GetTextBuf(buffer,size);
            strvalue := strPas(buffer);
            freemem(buffer,size);
            showmessage(strvalue);

        end;
 
Good stuff, thanks for sharing!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
thanks to you for helping me help myself !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top