Recent

Author Topic: Access Violation when passing parameters between forms  (Read 3121 times)

alessandrodidi

  • New member
  • *
  • Posts: 9
Access Violation when passing parameters between forms
« on: July 22, 2021, 08:49:27 pm »
Hi guys

I'm having trouble passing parameters between two forms.
When I create the form in this way I manage to pass the parameters:
Code: Pascal  [Select][+][-]
  1.     Form1 := TForm1.Create(Self);
  2.     Form1.ShowModal;


But I developed a procedure to open my forms in tabs and when I use this procedure and try to pass the parameters from one form to another I get an access violation message.

Below is the part of the code that creates the form:
Code: Pascal  [Select][+][-]
  1. procedure MyTab(PageControl: TPageControl; FormClass: TFormClass);
  2. var
  3.   Form: TForm;
  4.   TabSheet: ETabSheet;
  5. begin
  6.     TabSheet := ETabSheet.Create(PageControl);
  7.     TabSheet.PageControl := PageControl;
  8.  
  9.     Form := FormClass.Create(TabSheet);
  10.     Form.Parent := TabSheet;
  11. ...

To call a procedure looks like this:
Code: Pascal  [Select][+][-]
  1. MyTab(PageControl1; TForm1);

The form that will pass the information to Form1 I'm creating it like this:
Code: Pascal  [Select][+][-]
  1.     Form2 := TForm2.Create(Self);
  2.     Form2.ShowModal;

I will be grateful if anyone can help me understand the reason for this problem in my procedure.

*Google translation*

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Access Violation when passing parameters between forms
« Reply #1 on: July 22, 2021, 09:28:08 pm »
Hi!

Only ONE form at a given time can be shown through ShowModal.

Replace Form2.ShowModal with Form2.Show.

Winni

alessandrodidi

  • New member
  • *
  • Posts: 9
Re: Access Violation when passing parameters between forms
« Reply #2 on: July 22, 2021, 09:44:31 pm »
Hi Winni, thanks for replying.

I tried to change the way the forms open, but I still have the same problem.

Any other suggestions?

Hi!

Only ONE form at a given time can be shown through ShowModal.

Replace Form2.ShowModal with Form2.Show.

Winni


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation when passing parameters between forms
« Reply #3 on: July 22, 2021, 09:54:50 pm »
Why are you trying to embed forms in tabsheets and not simply using the tabsheets themselves as containers for whatever you want on your "forms"?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Access Violation when passing parameters between forms
« Reply #4 on: July 22, 2021, 10:25:28 pm »
Hi!

What ever strange things you do with your forms: You should show us some more code.

And perhaps you create one form while the other is not done with creation - so too  early?

Winni

alessandrodidi

  • New member
  • *
  • Posts: 9
Re: Access Violation when passing parameters between forms
« Reply #5 on: July 23, 2021, 02:03:32 am »
Hi Howardpc, thanks for answering.


I'm using TabSheet this way because I followed some examples I found with concepts that were like this. From the examples I built my code.

Sorry for my ignorance but there are some concepts that I'm not familiar with, so if you could give me an example of this concept of using tabsheets as containers

Why are you trying to embed forms in tabsheets and not simply using the tabsheets themselves as containers for whatever you want on your "forms"?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Access Violation when passing parameters between forms
« Reply #6 on: July 23, 2021, 02:35:23 am »
Sorry for my ignorance but there are some concepts that I'm not familiar with, so if you could give me an example of this concept of using tabsheets as containers

A TTabSheet is, in that sense, basicaly the same as a TPanel: you add a tab to the page control and then put controls in it. Or it can be subclassed and the controls created at runtime, like it looks you're doing, but the concept is the same: the TTabSheet is itself basically a container so it can be used (almost) like any other container.

But if you still want, for whatever reason, a form-like container to embed in the tab sheet (or any other other container control) you'll be better off using frames. They were invented precisely for that and they can be designed in the IDE just as if they were forms.

Out of curiosity: where can one find those examples you talk about? It sounds as if they were a little strange ... :-\
« Last Edit: July 23, 2021, 02:43:27 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

alessandrodidi

  • New member
  • *
  • Posts: 9
Re: Access Violation when passing parameters between forms
« Reply #7 on: July 23, 2021, 03:29:34 am »
Hi lucamar, tanks for contribute.

You're right, I'm trying to create the controls at runtime.

I understand what you said about using frames for this. I'll test and see if that way I'll be able to pass the parameters from one window to another.

Answering your question, here is the link to one of the examples I used to build my code:
https://www.devmedia.com.br/forum/abrir-form-em-aba-tdi/470378


Sorry for my ignorance but there are some concepts that I'm not familiar with, so if you could give me an example of this concept of using tabsheets as containers

A TTabSheet is, in that sense, basicaly the same as a TPanel: you add a tab to the page control and then put controls in it. Or it can be subclassed and the controls created at runtime, like it looks you're doing, but the concept is the same: the TTabSheet is itself basically a container so it can be used (almost) like any other container.

But if you still want, for whatever reason, a form-like container to embed in the tab sheet (or any other other container control) you'll be better off using frames. They were invented precisely for that and they can be designed in the IDE just as if they were forms.

Out of curiosity: where can one find those examples you talk about? It sounds as if they were a little strange ... :-\

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation when passing parameters between forms
« Reply #8 on: July 23, 2021, 06:02:07 am »
You're right, I'm trying to create the controls at runtime.

I understand what you said about using frames for this. I'll test and see if that way I'll be able to pass the parameters from one window to another.
As lucamar says, frames were specifically designed as reusable, embeddable containers and are the way to go.

Although you can use forms in the way your quoted code shows I would not recommend it.

For one thing, as you found, you may assume your form instance exists when it does not and cause an immediate access violation.

For another, you are using a sledgehammer to crack a nut. A form is a heavyweight object carrying large volumes of data and references to methods, almost none of which you will use in your tabsheet. Having several of these (tabsheet + form) instances will bloat your program unnecessarily. Why have two containers when one will suffice?

But you may not even need any frames. You could design your tabsheets in the IDE editor and save them. Then use the .lfm as a template to create your tabsheets dynamically, after which you dispose of the .lfm and design phase, once you have everything in working code.

To transfer data (parameters) between tabsheets you could design a simple record to hold it all, and pass that around rather than a huge GUI object like a form. OOP people call it decoupling, it means keeping GUI code separate from the data the program processes. Unfortunately RAD, for all its benefits, can encourage sloppy design in which the GUI is so tightly interwoven with the program data it becomes difficult (or impossible) to unravel without completely changing the program design.

This thread has code that shows one way to work with dynamically created tabsheets.
https://forum.lazarus.freepascal.org/index.php/topic,55400.msg412187.html#msg412187
It is based on the code of the originator of the thread, so will not suit your case exactly, but shows the general approach needed.
« Last Edit: July 23, 2021, 09:24:43 am by howardpc »

 

TinyPortal © 2005-2018