Solved

FindComponent() across forms

Posted on 2003-10-24
2
5,597 Views
Last Modified: 2012-06-27
I'm having trouble with the FindComponent() function in Delphi 3.

I have 2 forms in my application, Form1 and Form2.  One form 1 I have an array of components that I want to access from Form2.

Here's sample code from Form2 trying to accessing the component  on Form1:

if Form1.Edit1.Text = '' then exit;

This works fine, and life is great.  However, I said that there was an array of components on Form1, and I need to cycle through them from Form2.  Example:

for Ctr := 1 to 4 do begin
  if Form1.Edit (insert Ctr's value here).Text = '' then exit;
end;

Of course the above code will not compile, but it gives you an idea of what I'm trying to do.

In the past, I typically have used the FindComponent function in such situations.  I  have tried the following code, but I keep getting "Access Violation" errors whenever program execution reaches this function:

for Ctr := 1 to 4 do begin
  if (FindComponent('Form1.Edit' + IntToStr(Ctr)) as TEdit).Text = '' then exit;
end;

Why does the above trip an exception when the following code does not trip any exceptions?:

if Form1.Edit1.Text = '' then exit;
if Form1.Edit2.Text = '' then exit;
if Form1.Edit3.Text = '' then exit;
if Form1.Edit4.Text = '' then exit;

If I try using the problem code on Form1 (instead of Form2), then this code works fine:

for Ctr := 1 to 4 do begin
  if (FindComponent('Edit' + IntToStr(Ctr)) as TEdit).Text = '' then exit;
end;

Any suggestions/ideas?

Thanks.
0
Comment
Question by:Kapusta
2 Comments
 
LVL 8

Accepted Solution

by:
gmayo earned 75 total points
ID: 9617344
Perhaps you need to call it like this:
for Ctr := 1 to 4 do begin
 if (Form1.FindComponent('Edit' + IntToStr(Ctr)) as TEdit).Text = '' then exit;
end;

...ie use the Form1's FindComponent rather than Form2's FindComponent (which presumably would only search its own components).

Geoff M.
0
 

Author Comment

by:Kapusta
ID: 9617643
That solved the problem!  Much thanks.  :-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 102
delphi prevent click fast 2 165
Find and Replace Stream with 0s 8 45
SUM 2 INTEGER ARRAYS INTO 1 10 57
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

12 Experts available now in Live!

Get 1:1 Help Now