Recent

Author Topic: Trying to create a dynamic form  (Read 4128 times)

fxeconomist

  • New Member
  • *
  • Posts: 36
Trying to create a dynamic form
« on: February 18, 2024, 06:31:19 pm »
Alright, read about the code, I put it in... and it blows up.

I am trying to create a Debug window to cover the lack of a debug window (something like the Immediate window in VBA)

I put this code in a unit.

Code: Pascal  [Select][+][-]
  1. procedure SetDebugWindowParent(parent:tform);
  2. begin
  3.    DebugWindowParent:=parent;
  4. end;
  5.  
  6. procedure CreateDebugWindow;
  7. begin
  8.    if DebugWindowEnabled=true then if DebugWindow=nil then begin
  9.       DebugWindow:=TForm.Create(nil);
  10.       DebugWindow.Parent:=DebugWindowParent;
  11.       DebugWindow.SetBounds(0, 0, 800, 600);
  12.       DebugWindow.BorderStyle := bsNone;
  13.       DebugWindow.Caption:='Debug Window';
  14.    end;
  15. end;
  16.  
  17. procedure ShowDebugWindow;
  18. begin
  19.   CreateDebugWindow();
  20.   DebugWindow.ShowModal();
  21. end;                          

And I'm calling this from the initialization section of the unit1 (the main form)

Code: Pascal  [Select][+][-]
  1. SetDebugWindowParent(Form1);
  2. ShowDebugWindow();
  3.  

All of this crashes at the ShowModal() call with error 1407, "Cannot find window class".
Don't have any idea from this point...

Thaddy

  • Hero Member
  • *****
  • Posts: 14572
  • Sensorship about opinions does not belong here.
Re: Trying to create a dynamic form
« Reply #1 on: February 18, 2024, 06:58:04 pm »
This part is wrong
Code: Pascal  [Select][+][-]
  1.  if DebugWindowEnabled=true then
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

fxeconomist

  • New Member
  • *
  • Posts: 36
Re: Trying to create a dynamic form
« Reply #2 on: February 18, 2024, 07:17:21 pm »
This part is wrong
Code: Pascal  [Select][+][-]
  1.  if DebugWindowEnabled=true then

Well, I set that to true in the initialization sequence of my unit. It's blowing up without it as well.

ASerge

  • Hero Member
  • *****
  • Posts: 2257
Re: Trying to create a dynamic form
« Reply #3 on: February 18, 2024, 07:18:01 pm »
All of this crashes at the ShowModal() call with error 1407, "Cannot find window class".
Don't have any idea from this point...
This code should not cause such an error. The problem is in another code.
There are flaws in this code:
1. TForm.Create(nil). Application (not nil) is usually used to avoid thinking about the need to release the form at the end.
2. DebugWindow can be nil, when DebugWindowEnabled = False. ShowDebugWindow not test it.
3. BorderStyle = bsNone. i.e. Caption is not visible - do you really need to assign Caption?
4. Logical variables are usually checked as is, without excessive comparison. if DebugWindowEnabled then... Not if DebugWindowEnabled = True then... or if (DebugWindowEnabled = true) = true then...etc.
5. Your new form is created inside Form1, in the form of a dialog, but it does not have any controls to close itself.

fxeconomist

  • New Member
  • *
  • Posts: 36
Re: Trying to create a dynamic form
« Reply #4 on: February 18, 2024, 07:31:25 pm »
All of this crashes at the ShowModal() call with error 1407, "Cannot find window class".
Don't have any idea from this point...
This code should not cause such an error. The problem is in another code.
There are flaws in this code:
1. TForm.Create(nil). Application (not nil) is usually used to avoid thinking about the need to release the form at the end.
2. DebugWindow can be nil, when DebugWindowEnabled = False. ShowDebugWindow not test it.
3. BorderStyle = bsNone. i.e. Caption is not visible - do you really need to assign Caption?
4. Logical variables are usually checked as is, without excessive comparison. if DebugWindowEnabled then... Not if DebugWindowEnabled = True then... or if (DebugWindowEnabled = true) = true then...etc.
5. Your new form is created inside Form1, in the form of a dialog, but it does not have any controls to close itself.

These settings are merely the very beginning. I just need to get the form displayed first and then I'll start tinkering with it.
DebugWindowEnabled is a sort of a global switch, so that if I put it to false in the beginning of the program should inhibit any debug window functionality without actually removing the debug lines.

I'll fix the mechanic of the boolean checks, but still I'll need the window to at least appear.
Yes, it doesn't have a button to actually close, and I don't think I actually want one. I'll see what I can do about a minimize/restore button.
As I've seen, without the ShowModal() call, there is no error. ShowModal() actually creates the window, with a CreateWindowExW from Win32WSControls. That function actually is the one that fails and returns the error message.

fxeconomist

  • New Member
  • *
  • Posts: 36
Re: Trying to create a dynamic form
« Reply #5 on: February 18, 2024, 07:35:45 pm »
With this it fails as well

Code: Pascal  [Select][+][-]
  1. DebugWindow:=TForm.Create(Application);

jamie

  • Hero Member
  • *****
  • Posts: 6223
Re: Trying to create a dynamic form
« Reply #6 on: February 18, 2024, 07:36:48 pm »
Use :

 TheItem := TForm.CreateNew(Theowner);

Create will tried the read the resources.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6223
Re: Trying to create a dynamic form
« Reply #7 on: February 18, 2024, 07:37:26 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   A:TForm;
  4. begin
  5.   A := TForm.CreateNew(Self);
  6.   A.Parent := Self;
  7.   A.Setbounds(0,0,200,200);
  8.   A.Show;
  9. end;                        
  10.  
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2257
Re: Trying to create a dynamic form
« Reply #8 on: February 18, 2024, 07:41:09 pm »
As I've seen, without the ShowModal() call, there is no error. ShowModal() actually creates the window, with a CreateWindowExW from Win32WSControls. That function actually is the one that fails and returns the error message.
I tried your code - it creates an inconvenient, unmanageable window, but it does not cause an error. Maybe you didn't show something?

fxeconomist

  • New Member
  • *
  • Posts: 36
Re: Trying to create a dynamic form
« Reply #9 on: February 18, 2024, 07:43:08 pm »
As I've seen, without the ShowModal() call, there is no error. ShowModal() actually creates the window, with a CreateWindowExW from Win32WSControls. That function actually is the one that fails and returns the error message.
I tried your code - it creates an inconvenient, unmanageable window, but it does not cause an error. Maybe you didn't show something?

Maybe Lazarus behaves differently, I run this in CodeTyphon.

jamie

  • Hero Member
  • *****
  • Posts: 6223
Re: Trying to create a dynamic form
« Reply #10 on: February 18, 2024, 07:48:24 pm »
To show modal I don't think it can be parented but I could be wrong.

Edit:
  Just tried, I can be parented but whatever.
« Last Edit: February 18, 2024, 07:50:53 pm by jamie »
The only true wisdom is knowing you know nothing

fxeconomist

  • New Member
  • *
  • Posts: 36
Re: Trying to create a dynamic form
« Reply #11 on: February 18, 2024, 07:51:35 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   A:TForm;
  4. begin
  5.   A := TForm.CreateNew(Self);
  6.   A.Parent := Self;
  7.   A.Setbounds(0,0,200,200);
  8.   A.Show;
  9. end;                        
  10.  

Yes, this way it works, but it seems to be related to the Form1. It actually makes it as a child. Not exactly what I wanted, maybe it can be improved.
But there is an interesting "feature". When I move it on the screen, it deletes the Shape objects below it - they don't redraw.

jamie

  • Hero Member
  • *****
  • Posts: 6223
Re: Trying to create a dynamic form
« Reply #12 on: February 18, 2024, 07:55:08 pm »
If you want it to be parented then don't set the parent.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6223
Re: Trying to create a dynamic form
« Reply #13 on: February 18, 2024, 07:57:15 pm »
I am not exactly sure what you are trying to do here.

Maybe you want to stay on top window?
The only true wisdom is knowing you know nothing

fxeconomist

  • New Member
  • *
  • Posts: 36
Re: Trying to create a dynamic form
« Reply #14 on: February 18, 2024, 08:01:48 pm »
If you want it to be parented then don't set the parent.

Unparented works much better, no more redraw issues. Now I think I have to find a way to remove the close button ;)  Hehe, getting BPW flashbacks.

 

TinyPortal © 2005-2018