Solved

clear group of edit text

Posted on 2002-03-28
18
248 Views
Last Modified: 2010-04-04
How to fill group of edit.text with zero's(ex: edit1.text,edit2.text........,edit100.text ) with any of loop statments.
thanks
0
Comment
Question by:auokok
  • 7
  • 4
  • 2
  • +4
18 Comments
 
LVL 2

Expert Comment

by:isstorr
ID: 6902004
 for i := 0 to ComponentCount-1 do
    begin
    if (Components[i].ClassType = TEdit) then
      (Components[i] as TEdit).Text := '0';
    end;
0
 

Author Comment

by:auokok
ID: 6902043
what is the decleration of components
0
 
LVL 2

Accepted Solution

by:
isstorr earned 30 total points
ID: 6902063
hi,

ComponentCount and Components are properties of TComponent. The example goes through each component on the form, sees if its a TEdit and if it is, sets its Text property to '0'.

Si
0
 
LVL 2

Expert Comment

by:aubs
ID: 6902305
isstorr is right, but that will change ALL Edit boxes on a form.  You could put your edit boxes in a panel and then call the procedure below.


procedure SetText(Panel: TPanel; Str: String);
var
  i: integer;
  Component: TComponent;
begin
  for i:= Panel.Owner.ComponentCount -1 downto 0 do
  begin
    Component:= Panel.Owner.Components[i];
    if Component is TEdit then
      if (Component as TEdit).Parent= Panel then
         (Component as TEdit).Text:= Str;
     
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetText(panel1, '0');
end;

Aubs
0
 
LVL 2

Expert Comment

by:isstorr
ID: 6902323
As they're properties of TComponent, the technique will work for any container-style component, not just a form.

Aubs - why do you use the Panels Owner? Seems a long-winded way of going about things... Surely you can just use -

procedure SetText(Panel: TPanel; Str: String);
var
 i: integer;
begin
 for i := 0 to Panel.ComponentCount-1 do
   begin
   if (Panel.Components[i].ClassType = TEdit) then
     (Panel.Components[i] as TEdit).Text := Str;
   end;
end;
0
 
LVL 2

Expert Comment

by:aubs
ID: 6902358
Isstorr

Yes you are correct in this instance, but I use something similar to this function to manipulate components on a variety of different forms. (which is where I pulled the bulk of the code from)

I have the procedure stored in a separate unit, and call it from several different forms. I cant remember the exact reason why, but it didn't seem to work properly unless I added the owner bit.

Aubs
0
 

Author Comment

by:auokok
ID: 6902359
what is the decleration of components
0
 
LVL 2

Expert Comment

by:isstorr
ID: 6902372
TComponent is in the classes unit
0
 
LVL 2

Expert Comment

by:aubs
ID: 6902377
The 'Components' property lists all components 'owned' by the component.

In certain cases the 'host' component (in this case the panel) may be the parent, but not necessarily the owner of its children.

Aubs



0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:isstorr
ID: 6902541
Yes I can see how this would work if the edits weren't actually owned by the panel, but in this case we only care about the parent?
0
 
LVL 2

Expert Comment

by:aubs
ID: 6902619


Suppose you have an empty panel (Panel1) and create a button at runtime:

  Button:= TButton.Create(self);  //owner is the form
  Button.Parent:= Panel1;  //parent is the panel
 

Panel.ComponentCount is zero. Looping through Panel1.components would not find the button.

You need to use the Form.Components property.

As I use the SetText function with many forms, the function needs to know which form I mean. (I might have 4 forms each with a panel1).

I could send the form as a parameter: SetText(Form: TForm; Panel: TPanel;...), but as I can get this from the Panel.Owner property, it saves me having to do so.



Here is the function I use to automatically space buttons when a form gets resized. I put it in a unit all on its own.

Drop a panel on a form and set its alignment to alClient.

Drop three buttons on the panel.

In the OnResize event of the panel call the procedure.

procedure ArrangeButtonsHoriz(Panel: TPanel);
var
  i, ButtonCount, ButtonIndex, tmpLeft: integer;
  Component: TComponent;
  Button: TButton;
begin
  ButtonCount:= 0;
  for i:= 0 to Panel.Owner.ComponentCount -1 do
  begin
    Component:= Panel.Owner.Components[i];
    if Component is TButton then
      if (Component as TButton).Parent= Panel then
        inc(ButtonCount);
  end;
  ButtonIndex:= 0;
  for i:= 0 to Panel.Owner.ComponentCount -1 do
  begin
    Component:= Panel.Owner.Components[i];
    if Component is TButton then
      if (Component as TButton).Parent= Panel then
      begin
        Button:= Component as TButton;
        tmpLeft:= Trunc((Panel.Width - (ButtonCount * Button.Width))/ (ButtonCount + 1));
        tmpLeft:= tmpLeft + ButtonIndex * (Button.Width + tmpLeft);
        Button.Left:= tmpLeft;
        inc(ButtonIndex);
      end;
  end;
end;

0
 
LVL 3

Expert Comment

by:VSF
ID: 6903197
Try this!
This procedure enables or disable any component inside a TGroupbox or TPanel, etc...

It can be easely adapted to change any properties of the components inside the group!

//------ Procedure to Enable All Components on a group ------
procedure EnableAll (AWinControl: TWinControl);
var
 i: integer;
begin
 AWinControl.Enabled := TRUE;
 for I := 0 to (AWinControl.ControlCount - 1) do
  begin
   if (AWinControl.Controls [I] is TWinControl) then
    EnableAll (TWinControl (AWinControl.Controls [I]))
   else
    AWinControl.Controls [I].Enabled := TRUE;
  end; {for}
end; {--EnableAll--}


//------ Procedure to Disable All Components on a group ------
procedure DisableAll (AWinControl: TWinControl);
var
 I: integer;
begin
 AWinControl.Enabled := False;
 for I := 0 to (AWinControl.ControlCount - 1) do
  begin
   if (AWinControl.Controls [I] is TWinControl) then
    DisableAll (TWinControl (AWinControl.Controls [I]))
   else
    AWinControl.Controls [I].Enabled := False;
  end; {for}
end; {--DisableAll--}

Hope it helps!

VSF
www.victory.hpg.com.br
www.boatoda.hpg.com.br
0
 
LVL 2

Expert Comment

by:cqhall
ID: 6915391
If you only want to affect the edit boxes that are on a panel (or other container), then use controlcount and controls instead of componentcount and components.  Control lists are based on the parent rather than the owner.

procedure SetText(Panel: TPanel; Str: String);
var
i: integer;
begin
for i := 0 to Panel.ControlCount-1 do
 begin
  if Panel.Controls[i] is tEdit then
     (Panel.Controls[i] as TEdit).Text := Str;
 end;
end;
0
 
LVL 3

Expert Comment

by:D-Master
ID: 6923230
procedure TForm1.Button1Click(Sender: TObject);
var
  i, ComNum: integer;
begin
  ComNum := 100;
  for i := 1 to ComNum do
    TEdit(FindComponent('Edit'+IntToStr(i))).Text := '0';
end;
0
 
LVL 2

Expert Comment

by:isstorr
ID: 6924845
hi D-Master,

It is customary on EE to always post as comments, to allow the question asker to evaluate all proposed solutions to the problem. See the 'Comments and Answers' tip for more information.

Si
0
 
LVL 3

Expert Comment

by:D-Master
ID: 6926680
hi all

Thanx isstorr for that tip, but I am new at EE and I didn't notice the difference. I read the 'Comments and Answers'.

Thanx again
0
 
LVL 2

Expert Comment

by:isstorr
ID: 6927574
np HTH :)
0
 

Expert Comment

by:SpideyMod
ID: 8280683
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now