We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Accesing Data in second form without opening it

venks asked
Medium Priority
Last Modified: 2010-04-06

I require a solution for the following problem
I have an application which has two forms
One of the controls of the first(Main Form) has to access rhe data in a list box available in the second form(Form2) with out opening the the second form.Both the forms uses each other's units .The list box data is loaded through a fromstorage component(RX lib component).
When you try to access the data of the list box with out opening the second form the following error message is received:-
List index out of Bounds

I got around the problem by
This produces an undesirable flicker though every thing else works fine.
Is there any other solution? (ie with out showing the second form can the data
from a control in the second form be  accessed)
Watch Question

Hi venks,

when you create a form you can access all the owned components.
I think your problem is caused because, perhaps, you're filling the list box in the OnShow event.

Could you check it ?

if your code is in form1 and you want to access form2 you need to do this....

//This will make the caption of Form1 same as Form2's caption

Caption := Form2.Caption;

About the Out of Bounds error this might be caused if you are trying to get an item that the listbox doesn't have...example.....

if you have many items in your listbox...
Item#1 Index = 0
Item#2 Index = 1
Last Item Index = ListBox.Count - 1
Also as Itamar said, if you are creating or filling the listbox with items in the OnShow() of the form then if you don't show that form those items aren't there and that's why you get the Out of Bounds error....
Hope this helps..

Viktor Ivanov
This is an example of what I was trying to tell you....

procedure TForm1.FormShow........
  ListBox.items.add('This will not show if you do not show the form');
  ListBox.items.add('Then it would be out of bounds');
procedure TForm2.Button1Click.....
  Caption := Form1.ListBox.Items.Strings[2];
  //If you haven't shown form1 this would be out of bounds because you haven't added any items   to the listbox....

Viktor Ivanov


Dear Viktornet and Itamar
Both of you have correctly identified the probelm .But what I want is the solution ie with out opening the second form How do I access the data in the List BOx(in the second form) from the main  form.
The list box is being populated by a component 'Formstorage'(from RX library)
dropped in the second component.This component stores what ever property value is defined during the design stage.I have defined that Listbox.items property be saved when the form closes .The component writes this property to aN INIFILE OR to the system registry as desired.The component does the job with out writing one piece of code and loads the data when the form is first opened.
The problem is how to access this data with out opening this form
from the main form.
I am aware that the trick is to populate the listbox
with the data with out opening the form so that When the main form calls the list box from the main form no error occurs(due to lack of data).The code may be tricky.But that is what i require

Thanks for the answers

Can't you fill out the data when the first form is created and shown???

Viktor Ivanov
Unlock this solution and get a sample of our free trial.
(No credit card required)
Ok, I tried the component and here is what you should do....

You have 2 forms (Form1 & Form2)

In Form2 you have a listbox that you want to be restored withouf showing the form right???Ok, here is what you do....

in Form1's OnCreate() add this....

Then you can call what ever you need from the listbox and the second form//////

Viktor Ivanov


Dear  viktornet  
Your answer regarding using streams is workable.But that is not what I want.But your comment to use restoreformplacement is what I neeeded.
Yours was a nice answer.But when I tried it there was an error.
Then I discovered that the statement
has to be put in form2's oncreate event instead of form1's oncreate event.Now It works fine and my solution is found
Hence I award you the points and thanks

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.