Solved

Accesing Data in second form without opening it

Posted on 1998-09-07
8
163 Views
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
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
Comment
Question by:venks
  • 5
  • 2
8 Comments
 
LVL 4

Expert Comment

by:itamar
ID: 1338929
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1338930
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1338931
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
 
LVL 1

Author Comment

by:venks
ID: 1338932
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
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 10

Expert Comment

by:viktornet
ID: 1338933
Can't you fill out the data when the first form is created and shown???

Regards,
Viktor Ivanov
0
 
LVL 10

Accepted Solution

by:
viktornet earned 100 total points
ID: 1338934
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1338935
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
 
LVL 1

Author Comment

by:venks
ID: 1338936
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

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

27 Experts available now in Live!

Get 1:1 Help Now