Link to home
Start Free TrialLog in
Avatar of venks
venks

asked on

Accesing Data in second form without opening it


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
form2.show;
form2.hide;
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)
venks
Avatar of itamar
itamar

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 ?

IHTH,
Itamar
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..

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

procedure TForm1.FormShow........
begin
  ListBox.items.add('Hello');
  ListBox.items.add('This will not show if you do not show the form');
  ListBox.items.add('Then it would be out of bounds');
end;
procedure TForm2.Button1Click.....
begin
  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....
end;

Regards,
Viktor Ivanov
Avatar of venks

ASKER

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
Venks

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

Regards,
Viktor Ivanov
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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....
Form2.FormStorage1.RestoreFormPlacement;

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

Regards,
Viktor Ivanov
Avatar of venks

ASKER

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
Form2.FormStorage1.RestoreFormPlacement;
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
Venks