Recent

Author Topic: How to rename default Form in Lazarus  (Read 2595 times)

aag031

  • Newbie
  • Posts: 4
How to rename default Form in Lazarus
« on: March 20, 2022, 12:10:17 pm »
Team

I observed the following behavior. When I create new GUI application Lazarus created default Form with class name TForm1 and variable Form1. I would like to rename this form to another name more suitable  for given project. If correct this name in source code of form, application does not start with message (screenshot has been attached) that "Form resource <new name> not found in resource
I found out that I should remove all  occurrence of old name from all project files manually and the cleanup and rebuilt project. After this manipulation project start correctly

Is there more suitable way for renaming default form?

Thank you in advance
Alex

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: How to rename default Form in Lazarus
« Reply #1 on: March 20, 2022, 12:11:50 pm »
Use the OBJECT INSPECTOR ... silly. Dumbest question this year by far. I am not in the mood to explain such a silly question and actually answer it to you. Do it yourself. >:D >:D >:D >:D
« Last Edit: March 20, 2022, 12:14:51 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: How to rename default Form in Lazarus
« Reply #2 on: March 20, 2022, 12:17:33 pm »
Use the OBJECT INSPECTOR ... silly. Dumbest question this year by far. I am not in the mood to explain such a silly solution to you. Do it yourself.

If you're not in the mood then how about you don't answer at all instead of ranting at a user who just made one of their first posts and was genuinly looking for help because they simply didn't know what the right solution is?

Is there more suitable way for renaming default form?

As Thaddy had implied: You should change the Name property of the form in the object inspector. This will automatically also update the source code.

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: How to rename default Form in Lazarus
« Reply #3 on: March 20, 2022, 12:21:44 pm »
Use the OBJECT INSPECTOR ... silly. Dumbest question this year by far. I am not in the mood to explain such a silly question and actually answer it to you. Do it yourself. >:D >:D >:D >:D

If you were born with all knowledge of the universe implanted in you, then you should also know that not everybody has such powers as you.
He wouldn't ask question if he knew the answer, would he?

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: How to rename default Form in Lazarus
« Reply #4 on: March 20, 2022, 12:30:18 pm »
Bad case of "Sunday morning coming down", Kris Kristofferson, Sarah. Tnx for the explanation.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: How to rename default Form in Lazarus
« Reply #5 on: March 20, 2022, 12:30:24 pm »
Is there more suitable way for renaming default form?

Generally, components that are in .lfm file (that includes form itself and all components, visual and non-visual put in the form) should be renamed using Object inspector.
Other identifiers are renamed in source (easiest is by pressing F2 or using menu Source / Refactoring / Rename Identifier or right click on the identifier and then Refactoring / Rename Identifier. It would be nice to get a warning when using this method and try to rename something that is also in .lfm file (I did so in couple of occasions which led me to similar problems you have).

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to rename default Form in Lazarus
« Reply #6 on: March 20, 2022, 12:39:04 pm »
I think aag031's problem came from manually renaming a variable in only one place in the source when that name occurs throughout the sources in all of *.pas, .lpr and .lfm files.
His problem is not related to using the Rename Identifier dialog, which was developed specifically to *avoid* the problem you mention. Rename Identifier carefully synchronises renaming across all affected parts of a project. If you find it does not, this is a bug which you should report with a compileable example.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to rename default Form in Lazarus
« Reply #7 on: March 20, 2022, 12:50:21 pm »
Although renaming identifiers in the Object Inspector or by using the Refactoring menu item works in most cases there is still the possibility that something goes wrong. Here is an example: Drop a TSynEdit on a form - the new form gets the name "SynEdit1", and a new unit "SynEdit" will appear in the uses clause. Now you decide to rename "SynEdit1" to "SynEdit" (without the appended "1"). When you do this with the Object Inspector, it works fine. But finally you decide: there should be a clearer name - since the SynEdit displays - say - xml data it should be named "XMLSynEdit". Again you use the Object Inspector to change the component name. But when you compile the project, it will not work any more because the unit "SynEdit" in the uses clause has been renamed to "XmlSynEdit", too! (https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/39669).

So, be careful.

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: How to rename default Form in Lazarus
« Reply #8 on: March 20, 2022, 12:59:52 pm »
His problem is not related to using the Rename Identifier dialog, which was developed specifically to *avoid* the problem you mention. Rename Identifier carefully synchronises renaming across all affected parts of a project. If you find it does not, this is a bug which you should report with a compileable example.

I put example in the attachment.

On the form there is a button named Button1 (both in unit1.pas and in Object inspector). In the OnCreate event of the form there is Button1.Click; statement.

Now, try to rename Button1 using F2 shortcut (e.g. to ButtonTest). It is renamed in unit1.pas, but not in unit1.lfm (and consequently in Object inspector).
When you try to run project now you get access violation error:
Code: Text  [Select][+][-]
  1. [Window Title]
  2. Error
  3.  
  4. [Content]
  5. Project project1 raised exception class 'External: ACCESS VIOLATION' with message:
  6. Access violation reading from address $0000000000000000.
  7.  
  8.  In file 'unit1.pas' at line 34:
  9. ButtonTest.Click;

If you can confirm that this should not happen, I'll report bug.

I tested this with Lazarus 2.2.0, FPC 3.2.2, Windows 11, but it is exactly the same with Lazarus 2.0.10.

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: How to rename default Form in Lazarus
« Reply #9 on: March 20, 2022, 01:06:41 pm »
I think aag031's problem came from manually renaming a variable in only one place in the source when that name occurs throughout the sources in all of *.pas, .lpr and .lfm files.
His problem is not related to using the Rename Identifier dialog, which was developed specifically to *avoid* the problem you mention. Rename Identifier carefully synchronises renaming across all affected parts of a project. If you find it does not, this is a bug which you should report with a compileable example.

I renamed form type from TForm1 to TFormTest and got message just like OP had:
Code: Text  [Select][+][-]
  1. [Debugger Exception Notification]
  2.  
  3.  
  4. [Break]
  5. Project project1 raised exception class 'EResNotFound' with message:
  6. Form resource TFormTest not found. For resourceless forms CreateNew constructor must be used. See the global variable RequireDerivedFormResource.
  7.  
  8.  In file 'customform.inc' at line 2053
  9.  
  10.  
  11. [Ignore this exception type]
  12.  
  13. [Continue]

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to rename default Form in Lazarus
« Reply #10 on: March 20, 2022, 01:18:38 pm »
Now, try to rename Button1 using F2 shortcut (e.g. to ButtonTest).
Have never done it like that... As already others told you, select the Button1 on the designer form, find its Name property in the Object Inspector, select it and type the new name. This renames all occurencies of "Button1" in the unit (maybe also in other units - not sure about that, but even in comments (which is correct normally) and - as I described above - in the uses clause (which is wrong, but very rare)).

aag031

  • Newbie
  • Posts: 4
Re: How to rename default Form in Lazarus
« Reply #11 on: March 20, 2022, 01:23:48 pm »
Team

Thank you for quick and polite answer. But one more question. If change the name attribute of form in "Object Inspector" I observed that the name of class where this form is implemented changes too. For example I have Form1 : TForm1. If change Form1 to MainAppForm (for example) name of type is change too from TForm1 to TMainAppForm.
Is it designed behavior?

Thanks in advance
Alex

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to rename default Form in Lazarus
« Reply #12 on: March 20, 2022, 01:26:02 pm »
I renamed form type from TForm1 to TFormTest and got message just like OP had:
Again: don't rename by typing in the editor.
* Right-click on TForm1, select "Refactoring" > "Rename identifier" from the context menu and type the new name in the "Renaming" box.

Another misconception by new users is often: How to rename a unit? Do not change the name.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: How to rename default Form in Lazarus
« Reply #13 on: March 20, 2022, 01:32:51 pm »
Is it designed behavior?
Yes, that is designed behavior. It is even core to Lazarus and Delph to keep your GUI and code synchronized. It also needs to work that way to keep everything in sync. Did you expect anything different? Then we can explain.
But plz: indeed use the object inspector or the refactoring option like wp suggested. If you don't you will get into trouble, unless you are a very seasoned user (meaning 10+ years experience with Lazarus or Delphi, and that is an estimate on the lower side).
« Last Edit: March 20, 2022, 01:43:39 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: How to rename default Form in Lazarus
« Reply #14 on: March 20, 2022, 02:36:34 pm »
I renamed form type from TForm1 to TFormTest and got message just like OP had:
Again: don't rename by typing in the editor.
* Right-click on TForm1, select "Refactoring" > "Rename identifier" from the context menu and type the new name in the "Renaming" box.

I didn't rename it by typing in the editor. Now you try this and tell me your results. I described my results.

 

TinyPortal © 2005-2018