Solved

Accesing Data in second form without opening it

Posted on 1998-09-07
8
162 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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