Recent

Author Topic: Using same procedure to generate menu on Form1 and Combobox on Frame2  (Read 1695 times)

wasp41p

  • New Member
  • *
  • Posts: 12
I should move all comboboxes to another Form (Form2, for example) and I would like to use the same procedures I use to generate menus and combobox in Form1, is there a way to do this without rewriting everything? This is a part I'd like to use for the menu in Form1 and the comboboxes in Form2, at the moment they're all in Form1:
Code: Pascal  [Select][+][-]
  1.     procedure TForm1.FormCreate(Sender: TObject);
  2.       procedure SetDefaultColors(const vHighLighter:TSynCustomHighlighter);
  3.        begin
  4.          if Assigned(vHighLighter.CommentAttribute)    then vHighLighter.CommentAttribute.Foreground    := clSilver;
  5.          if Assigned(vHighLighter.IdentifierAttribute) then vHighLighter.IdentifierAttribute.Foreground := clNone;
  6.          if Assigned(vHighLighter.CommentAttribute)    then vHighLighter.CommentAttribute.Foreground    := $00A2A2A2;
  7.          if Assigned(vHighLighter.KeywordAttribute)    then vHighLighter.KeywordAttribute.Foreground    := clNavy;
  8.          if Assigned(vHighLighter.StringAttribute)     then vHighLighter.StringAttribute.ForeGround     := $003FB306;
  9.          if Assigned(vHighLighter.SymbolAttribute)     then vHighLighter.SymbolAttribute.ForeGround     := $00A25151;
  10.        end;
  11.     var
  12.       i: Integer;
  13.       item: TMenuItem;
  14.       HL: TSynCustomHighlighter;
  15.       SynEdit: TSynEdit;
  16.     begin
  17.       CreateNewTab;
  18.       ComboBox3.Items.Clear;
  19.       Caption := FORM_CAPTION;
  20.       CurrentSynEdit.OnStatusChange:= @OnEditorStatusChange;
  21.       //CurrentSynEdit.OnChange:= @OnSynChange;
  22.      
  23.       FHighlighters := TFPList.Create;
  24.       FHighlighters.Add(TSynADSP21xxSyn.Create(self));
  25.       FHighlighters.Add(TSynFortranSyn.Create(self));
  26.       FHighlighters.Add(TSynFoxproSyn.Create(self));
  27.       FHighlighters.Add(TSynGalaxySyn.Create(self));
  28.       FHighlighters.Add(TSynBaanSyn.Create(self));
  29.       FHighlighters.Add(TSynAWKSyn.Create(self));
  30.       FHighlighters.Add(TSynHaskellSyn.Create(self));
  31.       FHighlighters.Add(TSynCacheSyn.Create(self));
  32.       FHighlighters.Add(TSynModelicaSyn.Create(self));
  33.       FHighlighters.Add(TSynCobolSyn.Create(self));
  34.       FHighlighters.Add(TSynCSSyn.Create(self));
  35.       FHighlighters.Add(TSynDmlSyn.Create(self));
  36.       FHighlighters.Add(TSynProgressSyn.Create(self));
  37.       FHighlighters.Add(TSynEiffelSyn.Create(self));
  38.       FHighlighters.Add(TSynGWScriptSyn.Create(self));
  39.       FHighlighters.Add(TSynHP48Syn.Create(self));
  40.       FHighlighters.Add(TSynVBScriptSyn.Create(self));
  41.       FHighlighters.Add(TSynUnrealSyn.Create(self));
  42.       FHighlighters.Add(TSynVrml97Syn.Create(self));
  43.       FHighlighters.Add(TSynTclTkSyn.Create(self));
  44.       FHighlighters.Add(TSynLDRSyn.Create(self));
  45.       FHighlighters.Add(TSynRubySyn.Create(self));
  46.       FHighlighters.Add(TSynInnoSyn.Create(self));
  47.       FHighlighters.Add(TSynAsmSyn.Create(self));
  48.       FHighlighters.Add(TSynDOTSyn.Create(self));
  49.       FHighlighters.Add(TSynIdlSyn.Create(self));
  50.       FHighlighters.Add(TSynKixSyn.Create(self));
  51.       FHighlighters.Add(TSynSDDSyn.Create(self));
  52.       FHighlighters.Add(TSynSMLSyn.Create(self));
  53.       FHighlighters.Add(TSynFreePascalSyn.Create(self));
  54.       FHighlighters.Add(TSynM3Syn.Create(self));
  55.       FHighlighters.Add(TSynRCSyn.Create(self));
  56.       FHighlighters.Add(TSynPrologSyn.Create(Self));
  57.       FHighlighters.Add(TSynLuaSyn.Create(Self));
  58.       FHighlighters.Add(TSyn8051Syn.Create(Self));
  59.       FHighlighters.Add(TSynCACSyn.Create(Self));
  60.       FHighlighters.Add(TSynJavaSyn.Create(self));
  61.       FHighlighters.Add(TSynSQLSyn.Create(self));
  62.       FHighlighters.Add(TSynVHDLSyn.Create(Self));
  63.       FHighlighters.Add(TSynVerilogSyn.Create(Self));
  64.       FHighlighters.Add(TSynJSONSyn.Create(Self));
  65.       FHighlighters.Add(TSynPasSyn.Create(self));
  66.       FHighlighters.Add(TSynXMLSyn.Create(self));
  67.       FHighlighters.Sort(@CompareHighlighters);
  68.       for i:=0 to FHighlighters.Count-1 do begin
  69.         HL := TSynCustomHighlighter(FHighlighters[i]);
  70.         if Assigned(HL) then
  71.         begin
  72.           item := TMenuItem.Create(self);
  73.           MenuFileHL.Add(item);
  74.           try
  75.             item.Caption := Format('%d - %s', [i, GetHighlighterCaption(HL)]);
  76.           except
  77.             on E : Exception  do
  78.               ShowMessage(E.Message+LineEnding+' at index '+inttostr(i));
  79.           end;
  80.           item.Tag := i+1;
  81.           ComboBox3.Items.Add(item.Caption);
  82.           item.OnClick := @MenuClick;
  83.           end;
  84.       end;
  85.       SelectHighlighter(28);
  86.      
  87.       ComboBox1.Items.Clear;
  88.       //load fonts                
  89.       g := TFPFontCacheList.Create;
  90.       g.SearchPath.Add('C:\Windows\Fonts');
  91.       g.BuildFontCache;
  92.       for i := 0 to g.Count - 1 do
  93.       begin
  94.         if ComboBox1.Items.IndexOf(g.Items[i].FamilyName) < 0 then
  95.           ComboBox1.Items.Add(g.Items[i].FamilyName);
  96.       end;
  97.       SynEdit := CurrentSynEdit;
  98.       if SynEdit <> nil then
  99.       begin
  100.         ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(SynEdit.Font.Name);
  101.         SpinEdit1.Value := SynEdit.Font.Size;
  102.       end;
  103.      
  104.     end;

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #1 on: August 15, 2023, 11:25:48 am »
Move the shared code into a separate unit which can be "used" wherever needed.

For example (quick and dirty, and untested):
Code: Pascal  [Select][+][-]
  1. unit uCommon;
  2.  
  3. interface
  4.  
  5. uses
  6.   ... (whatever needed);
  7.  
  8. procedure PrepareHighlighters(AList: TFPList; AMenu: TMenuItem; ACombobox: TComboBox; AClickHandler: TNotifyEvent);
  9.  
  10. implementation
  11.  
  12. function GetHighligherCaption(vHighlighter: TSynCustomHighlighter): String;
  13. begin
  14.   ...
  15. end;
  16.  
  17. procedure PrepareHighlighters(AList: TFPList; AMenu: TMenuItem; ACombobox: TComboBox; AClickHandler: TNotifyEvent);
  18. var
  19.   i: Integer;
  20.   HL: TSynCustomHighlighter;
  21.   item: TMenuitem;
  22. begin
  23.   AList.Add(TSynADSP21xxSyn.Create(self));
  24.   AList.Add(TSynFortranSyn.Create(self));
  25.   AList.Add(TSynFoxproSyn.Create(self));
  26.   AList.Add(TSynGalaxySyn.Create(self));
  27.   AList.Add(TSynBaanSyn.Create(self));
  28.   AList.Add(TSynAWKSyn.Create(self));
  29.   AList.Add(TSynHaskellSyn.Create(self));
  30.   // etc.
  31.  
  32.   if (AMenu <> nil) then
  33.     for i:=0 to AList.Count-1 do begin
  34.       HL := TSynCustomHighlighter(AList[i]);
  35.       if Assigned(HL) then
  36.       begin
  37.         item := TMenuItem.Create(self);
  38.         AMenu.Add(item);
  39.         item.Caption := Format('%d - %s', [i, GetHighlighterCaption(HL)]);
  40.         item.Tag := i+1;
  41.         item.OnClick := AClickHandler;
  42.         if AComboBox <> nil then AComboBox.Items.Add(item.Caption);
  43.       end;
  44.     end;
  45.   end;
  46. end;
  47.  
  48. end.
  49.  
  50. //---------------------------
  51.  
  52. unit Unit1;
  53.  
  54. interface
  55.  
  56. uses
  57.   ...
  58.  
  59. type
  60.   TForm1 = class(TForm)
  61.     procedure FormCreate(Sender: TObject);
  62.     ...
  63.   private
  64.     FHighlighers: TFPList;
  65.   end;
  66.  
  67. implementation
  68.  
  69. procedure TForm1.Formcreate(Sender: TObject);
  70. begin
  71.   FHighlighters := TFPList.Create;
  72.   PrepareHighlighters(FHighlighters, MenuFileHL, Combobox3, @MenuClick);
  73.   ...
  74. end;
  75.  

RayoGlauco

  • Full Member
  • ***
  • Posts: 194
  • Beers: 1567
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #2 on: August 15, 2023, 11:28:14 am »
Maybe you can create a procedure that receives the target form as a parameter:

Code: Pascal  [Select][+][-]
  1. procedure SetCombos(aForm:TForm);
  2. var
  3.   aCombo: TComboBox;
  4. begin
  5.   aCombo:= TComboBox(aForm.FindComponent('ComboBox1'));
  6.   if aCombo <> nil then
  7.    aCombo.Items.Clear;
  8.   // etc...
  9. end;
  10.  

Same as previous answer, quick and untested!
« Last Edit: August 15, 2023, 11:31:50 am by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

wasp41p

  • New Member
  • *
  • Posts: 12
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #3 on: August 15, 2023, 05:29:59 pm »
So far so good except for the procedure which assigns the active synedit, it refers to the pagecontrol1 of form1, if I move it to the common unit in this way it gives me an access violation during compilation.
Code: Pascal  [Select][+][-]
  1. function CurrentSynEdit(APageControl1: TPageControl): TSynEdit;
  2. begin
  3.   if APageControl1.ActivePage <> nil then
  4.     Result := TSynEdit(APageControl1.ActivePage.Tag)
  5.   else
  6.     Result := nil;
  7. end;                                  

I'm stuck at this :'(

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #4 on: August 15, 2023, 05:44:15 pm »
I have a feeling you do things wrong, attach a demo again that shows current progress.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

wasp41p

  • New Member
  • *
  • Posts: 12
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #5 on: August 15, 2023, 06:12:02 pm »
Yes, Sure, It's me.

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #6 on: August 15, 2023, 06:19:30 pm »
So far so good except for the procedure which assigns the active synedit, it refers to the pagecontrol1 of form1, if I move it to the common unit in this way it gives me an access violation during compilation.
Code: Pascal  [Select][+][-]
  1. function CurrentSynEdit(APageControl1: TPageControl): TSynEdit;
  2. begin
  3.   if APageControl1.ActivePage <> nil then
  4.     Result := TSynEdit(APageControl1.ActivePage.Tag)
  5.   else
  6.     Result := nil;
  7. end;                                  
Something is wrong here: Your code shows the opposite of what you are saying. It does not assign the synedit to the ActivePage - it's the other way: it retrieves the synedit from the ActivePage.

wasp41p

  • New Member
  • *
  • Posts: 12
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #7 on: August 15, 2023, 06:22:45 pm »
Yes. You are right, I misspelled

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #8 on: August 15, 2023, 06:36:52 pm »
And then? Is the code shown causing the issue? If yes: Are you sure that APageControl1 is not nil? Or: Is the code calling CurrentSynEdit able to handle the case that CurrentSynEdit may return a nil result?

wasp41p

  • New Member
  • *
  • Posts: 12
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #9 on: August 15, 2023, 07:15:54 pm »
I was checking and that´s not the problem.
The problem is this and I don´t understand how to solve it, I should assign him the owner but I don´t know how, since he´s in another unit.
Code: Pascal  [Select][+][-]
  1. procedure PrepareHighlighters(AList: TFPList; AMenu: TMenuItem; ACombobox: TComboBox; AClickHandler: TNotifyEvent);
  2. var
  3.   i: Integer;
  4.   HL: TSynCustomHighlighter;
  5.   item: TMenuitem;
  6. begin
  7. AList := TFPList.Create;
  8. AList.Add(TSynADSP21xxSyn.Create(self));
  9. AList.Add(TSynFortranSyn.Create(self));
  10. AList.Add(TSynGalaxySyn.Create(self));
  11. AList.Add(TSynBaanSyn.Create(self));
  12. AList.Add(TSynAWKSyn.Create(self));
  13. AList.Add(TSynHaskellSyn.Create(self));
  14. AList.Add(TSynCacheSyn.Create(self));
  15. AList.Add(TSynModelicaSyn.Create(self));
  16. AList.Add(TSynCobolSyn.Create(self));
  17. AList.Add(TSynCSSyn.Create(self));
  18. AList.Add(TSynDmlSyn.Create(self));
  19. AList.Add(TSynProgressSyn.Create(self));
  20. AList.Add(TSynEiffelSyn.Create(self));
  21. AList.Add(TSynGWScriptSyn.Create(self));
  22. AList.Add(TSynHP48Syn.Create(self));
  23. AList.Add(TSynVBScriptSyn.Create(self));
  24. AList.Add(TSynUnrealSyn.Create(self));
  25. AList.Add(TSynVrml97Syn.Create(self));
  26. AList.Add(TSynTclTkSyn.Create(self));
  27. AList.Add(TSynLDRSyn.Create(self));
  28. AList.Add(TSynRubySyn.Create(self));
  29. AList.Add(TSynInnoSyn.Create(self));
  30. AList.Add(TSynAsmSyn.Create(self));
  31. AList.Add(TSynDOTSyn.Create(self));
  32. AList.Add(TSynIdlSyn.Create(self));
  33. AList.Add(TSynKixSyn.Create(self));
  34. AList.Add(TSynSDDSyn.Create(self));
  35. AList.Add(TSynSMLSyn.Create(self));
  36. AList.Add(TSynFreePascalSyn.Create(self));
  37. AList.Add(TSynM3Syn.Create(self));
  38. AList.Add(TSynRCSyn.Create(self));
  39. AList.Add(TSynPrologSyn.Create(self));
  40. AList.Add(TSynLuaSyn.Create(self));
  41. AList.Add(TSyn8051Syn.Create(self));
  42. AList.Add(TSynCACSyn.Create(self));
  43. AList.Add(TSynJavaSyn.Create(self));
  44. AList.Add(TSynSQLSyn.Create(self));
  45. AList.Add(TSynVHDLSyn.Create(self));
  46. AList.Add(TSynVerilogSyn.Create(self));
  47. AList.Add(TSynJSONSyn.Create(self));
  48. AList.Add(TSynPasSyn.Create(self));
  49. AList.Add(TSynXMLSyn.Create(self));
  50. AList.Sort(@CompareHighlighters);
  51.  
  52. if (AMenu <> nil) then
  53.   for i:=0 to AList.Count-1 do begin
  54.     HL := TSynCustomHighlighter(AList[i]);
  55.     if Assigned(HL) then
  56.     begin
  57.       item := TMenuItem.Create(nil);
  58.       AMenu.Add(item);
  59.       item.Caption := Format('%d - %s', [i, GetHighlighterCaption(HL)]);
  60.       item.Tag := i+1;
  61.       item.OnClick := AClickHandler;
  62.       if AComboBox <> nil then AComboBox.Items.Add(item.Caption);
  63.     end;
  64.   end;
  65. end;                                            

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #10 on: August 15, 2023, 07:36:59 pm »
You mean this?
Code: Pascal  [Select][+][-]
  1.       item := TMenuItem.Create(nil);
I had missed the "self" in my first post on this topic...

Setting the owner to nil will leave you with the burden to destroy the menuitem later - normally the owner would do this... But you have the MenuItem to which the new item will be added. Why not use the same owner?

Code: Pascal  [Select][+][-]
  1.       item := TMenuItem.Create(AMenuItem.Owner);

wasp41p

  • New Member
  • *
  • Posts: 12
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #11 on: August 16, 2023, 10:00:37 am »
Thank you wp for your time.
 I mean I can't use self in global:
Code: Pascal  [Select][+][-]
  1. ....
  2. AList := TFPList.Create;
  3. AList.Add(TSynADSP21xxSyn.Create(self));
  4. AList.Add(TSynFortranSyn.Create(self));
  5. AList.Add(TSynGalaxySyn.Create(self));
  6. ......

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #12 on: August 16, 2023, 10:32:01 am »
changes in uglobal
Code: Pascal  [Select][+][-]
  1. var
  2.     GHighlighters : TFPList;
  3. implementation
  4. var
  5.     g: TFPFontCacheList;
Code: Pascal  [Select][+][-]
  1. procedure PrepareHighlighters(AOwner: TComponent; AMenu: TMenuItem; ACombobox: TComboBox; AClickHandler: TNotifyEvent);
  2. var
  3.   i: Integer;
  4.   HL: TSynCustomHighlighter;
  5.   item: TMenuitem;
  6. begin
  7.   if GHighlighters = nil then
  8.     GHighlighters := TFPList.Create;
  9.   GHighlighters.Add(TSynADSP21xxSyn.Create(AOwner));
  10.   GHighlighters.Add(TSynFortranSyn.Create(AOwner));
  11.   GHighlighters.Add(TSynGalaxySyn.Create(AOwner));
  12.   GHighlighters.Add(TSynBaanSyn.Create(AOwner));
  13.   GHighlighters.Add(TSynAWKSyn.Create(AOwner));
  14.   GHighlighters.Add(TSynHaskellSyn.Create(AOwner));
  15.   GHighlighters.Add(TSynCacheSyn.Create(AOwner));
  16.   GHighlighters.Add(TSynModelicaSyn.Create(AOwner));
  17.   GHighlighters.Add(TSynCobolSyn.Create(AOwner));
  18.   GHighlighters.Add(TSynCSSyn.Create(AOwner));
  19.   GHighlighters.Add(TSynDmlSyn.Create(AOwner));
  20.   GHighlighters.Add(TSynProgressSyn.Create(AOwner));
  21.   GHighlighters.Add(TSynEiffelSyn.Create(AOwner));
  22.   GHighlighters.Add(TSynGWScriptSyn.Create(AOwner));
  23.   GHighlighters.Add(TSynHP48Syn.Create(AOwner));
  24.   GHighlighters.Add(TSynVBScriptSyn.Create(AOwner));
  25.   GHighlighters.Add(TSynUnrealSyn.Create(AOwner));
  26.   GHighlighters.Add(TSynVrml97Syn.Create(AOwner));
  27.   GHighlighters.Add(TSynTclTkSyn.Create(AOwner));
  28.   GHighlighters.Add(TSynLDRSyn.Create(AOwner));
  29.   GHighlighters.Add(TSynRubySyn.Create(AOwner));
  30.   GHighlighters.Add(TSynInnoSyn.Create(AOwner));
  31.   GHighlighters.Add(TSynAsmSyn.Create(AOwner));
  32.   GHighlighters.Add(TSynDOTSyn.Create(AOwner));
  33.   GHighlighters.Add(TSynIdlSyn.Create(AOwner));
  34.   GHighlighters.Add(TSynKixSyn.Create(AOwner));
  35.   GHighlighters.Add(TSynSDDSyn.Create(AOwner));
  36.   GHighlighters.Add(TSynSMLSyn.Create(AOwner));
  37.   GHighlighters.Add(TSynFreePascalSyn.Create(AOwner));
  38.   GHighlighters.Add(TSynM3Syn.Create(AOwner));
  39.   GHighlighters.Add(TSynRCSyn.Create(AOwner));
  40.   GHighlighters.Add(TSynPrologSyn.Create(AOwner));
  41.   GHighlighters.Add(TSynLuaSyn.Create(AOwner));
  42.   GHighlighters.Add(TSyn8051Syn.Create(AOwner));
  43.   GHighlighters.Add(TSynCACSyn.Create(AOwner));
  44.   GHighlighters.Add(TSynJavaSyn.Create(AOwner));
  45.   GHighlighters.Add(TSynSQLSyn.Create(AOwner));
  46.   GHighlighters.Add(TSynVHDLSyn.Create(AOwner));
  47.   GHighlighters.Add(TSynVerilogSyn.Create(AOwner));
  48.   GHighlighters.Add(TSynJSONSyn.Create(AOwner));
  49.   GHighlighters.Add(TSynPasSyn.Create(AOwner));
  50.   GHighlighters.Add(TSynXMLSyn.Create(AOwner));
  51.   GHighlighters.Sort(@CompareHighlighters);
  52.  
  53. if (AMenu <> nil) then
  54.   for i:=0 to Pred(GHighlighters.Count) do begin
  55.     HL := TSynCustomHighlighter(GHighlighters[i]);
  56.     if Assigned(HL) then
  57.     begin
  58.       item := TMenuItem.Create(nil);
  59.       AMenu.Add(item);
  60.       item.Caption := Format('%d - %s', [i, GetHighlighterCaption(HL)]);
  61.       item.Tag := i+1;
  62.       item.OnClick := AClickHandler;
  63.       if AComboBox <> nil then AComboBox.Items.Add(item.Caption);
  64.     end;
  65.   end;
  66. end;
  67.  
  68. procedure SelectHighlighter(AIndex: Integer; ASynEdit: TSynEdit; ACaption: TCaption; AVer: String);
  69. begin
  70.   begin
  71.     if AIndex < GHighlighters.Count then
  72.       begin
  73.         ASynEdit.Highlighter := TSynCustomHighlighter(GHighlighters[AIndex]);
  74.         ACaption := Format('%s - %s [%s]', [
  75.           AVer, ASynEdit.Highlighter.LanguageName, ASynEdit.Highlighter.ClassName]);
  76.       end;
  77.   end;
  78. end;
Code: Pascal  [Select][+][-]
  1. initialization
  2.   GHighlighters := nil;
  3. finalization
  4.   GHighlighters.Free;
  5. end.
changes in unit1
Code: Pascal  [Select][+][-]
  1.   PrepareHighlighters(Self, MenuFileHL, ComboBox3, @MenuClick);

I have not tested it fully, at least thats how it compile and run without error
« Last Edit: August 16, 2023, 10:34:18 am by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

wasp41p

  • New Member
  • *
  • Posts: 12
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #13 on: August 21, 2023, 11:53:54 am »
Update: I've managed to get the menu on form1 and the combobox on form2 and it works fine too, but I don't understand if this is normal or I've done something wrong, if I call the procedure in form2 it generates both the menu and the combobox, but if I call it from form1 it goes into access violation at compile time, it would seem that with form1 I can't write into form2. I attach part of the code:
Unit2.pas
Code: Pascal  [Select][+][-]
  1. var
  2.   Form2: TForm2;
  3.  
  4.  
  5. implementation
  6.  
  7. {$R *.lfm}
  8.  
  9. uses
  10.   unit1;
  11.  
  12. { TForm2 }
  13. procedure TForm2.FormCreate(Sender: TObject);
  14. var
  15.    SynEdit: TSynEdit;
  16. begin
  17.   ComboBox3.Items.Clear;
  18.   FFontlist:= TFPFontCacheList.Create;
  19.   ComboBox1.Items.Clear;
  20.   PrepareFontList(FFontlist, ComboBox1);
  21.  
  22.    SynEdit := CurrentSynEdit(Form1.PageControl1);
  23.   if SynEdit <> nil then fillfontmenu(ComboBox1, SpinEdit1, SynEdit);
  24.  
  25.   PrepareHighlighters(Self, ComboBox3, Form1.MenuFileHL, @Form1.MenuClick);
  26.  
  27.     SynEdit := CurrentSynEdit(Form1.PageControl1);
  28.   if SynEdit <> nil then SelectHighlighter(27, SynEdit);
  29.  
  30. end;
But if I do this, I have an access violation:
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.  
  4. implementation
  5.  
  6. {$R *.lfm}
  7.  
  8. uses
  9.   unit2;
  10.  
  11.  
  12. { TForm1 }
  13.  
  14. procedure TForm1.FormCreate(Sender: TObject);
  15. var
  16.   SynEdit: TSynEdit;
  17. begin
  18. .......
  19.  
  20.   PrepareHighlighters(Self, Form2.ComboBox3, MenuFileHL, @MenuClick);
  21.  
  22.   SynEdit := CurrentSynEdit(PageControl1);
  23.   if SynEdit <> nil then SelectHighlighter(27, SynEdit);
  24.  
  25. end;

wasp41p

  • New Member
  • *
  • Posts: 12
Re: Using same procedure to generate menu on Form1 and Combobox on Frame2
« Reply #14 on: August 24, 2023, 05:21:49 pm »
I apologize for bothering you with my silly questions (again) but I´m just starting out with Lazarus and still learning.
I´ve read a lot and I´m still reading, but I can´t figure out why in the statusbar the caret works only on the first tab, if I delete it, doesn´t work anymore even with the new one.  The same thing happens with the markup and other things, It seems that it doesn´t update to the active Synedit/tab. Please I attach the project, if someone wants to have a look could tell me where I am wrong.

 

TinyPortal © 2005-2018