Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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
0
venks
Asked:
venks
  • 5
  • 2
1 Solution
 
itamarCommented:
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
0
 
viktornetCommented:
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
0
 
viktornetCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
venksAuthor Commented:
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

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

Regards,
Viktor Ivanov
0
 
viktornetCommented:
Here is an example.......

to save properties of a control use this :

var fStream : TFileStream;
begin
  fStream := TFileStream.Create('listv.dat', fmCreate);
  try
    fStream.WriteComponent(listview1);
  finally
    fStream.free;
  end;
end;

To read it back, do this://You can read it bac any time you want...e.g when you creat the first form////
 
var fStream : TFileStream;
begin
  if fileexists('listv.dat') then begin
     fStream := TFileStream.Create('listv.dat', fmOpenRead);
     try
       listview1 := fStream.ReadComponent(listview1) as TListView;
     finally
       fStream.free;
     end;
  end;
end;

Regards,
Viktor Ivanov
0
 
viktornetCommented:
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
0
 
venksAuthor Commented:
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


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now