Forum > LCL

Using same procedure to generate menu on Form1 and Combobox on Frame2

(1/3) > >>

wasp41p:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    procedure TForm1.FormCreate(Sender: TObject);      procedure SetDefaultColors(const vHighLighter:TSynCustomHighlighter);       begin         if Assigned(vHighLighter.CommentAttribute)    then vHighLighter.CommentAttribute.Foreground    := clSilver;         if Assigned(vHighLighter.IdentifierAttribute) then vHighLighter.IdentifierAttribute.Foreground := clNone;         if Assigned(vHighLighter.CommentAttribute)    then vHighLighter.CommentAttribute.Foreground    := $00A2A2A2;         if Assigned(vHighLighter.KeywordAttribute)    then vHighLighter.KeywordAttribute.Foreground    := clNavy;         if Assigned(vHighLighter.StringAttribute)     then vHighLighter.StringAttribute.ForeGround     := $003FB306;         if Assigned(vHighLighter.SymbolAttribute)     then vHighLighter.SymbolAttribute.ForeGround     := $00A25151;       end;    var      i: Integer;      item: TMenuItem;      HL: TSynCustomHighlighter;      SynEdit: TSynEdit;    begin      CreateNewTab;      ComboBox3.Items.Clear;      Caption := FORM_CAPTION;      CurrentSynEdit.OnStatusChange:= @OnEditorStatusChange;      //CurrentSynEdit.OnChange:= @OnSynChange;           FHighlighters := TFPList.Create;      FHighlighters.Add(TSynADSP21xxSyn.Create(self));      FHighlighters.Add(TSynFortranSyn.Create(self));      FHighlighters.Add(TSynFoxproSyn.Create(self));      FHighlighters.Add(TSynGalaxySyn.Create(self));      FHighlighters.Add(TSynBaanSyn.Create(self));      FHighlighters.Add(TSynAWKSyn.Create(self));      FHighlighters.Add(TSynHaskellSyn.Create(self));      FHighlighters.Add(TSynCacheSyn.Create(self));      FHighlighters.Add(TSynModelicaSyn.Create(self));      FHighlighters.Add(TSynCobolSyn.Create(self));      FHighlighters.Add(TSynCSSyn.Create(self));      FHighlighters.Add(TSynDmlSyn.Create(self));      FHighlighters.Add(TSynProgressSyn.Create(self));      FHighlighters.Add(TSynEiffelSyn.Create(self));      FHighlighters.Add(TSynGWScriptSyn.Create(self));      FHighlighters.Add(TSynHP48Syn.Create(self));      FHighlighters.Add(TSynVBScriptSyn.Create(self));      FHighlighters.Add(TSynUnrealSyn.Create(self));      FHighlighters.Add(TSynVrml97Syn.Create(self));      FHighlighters.Add(TSynTclTkSyn.Create(self));      FHighlighters.Add(TSynLDRSyn.Create(self));      FHighlighters.Add(TSynRubySyn.Create(self));      FHighlighters.Add(TSynInnoSyn.Create(self));      FHighlighters.Add(TSynAsmSyn.Create(self));      FHighlighters.Add(TSynDOTSyn.Create(self));      FHighlighters.Add(TSynIdlSyn.Create(self));      FHighlighters.Add(TSynKixSyn.Create(self));      FHighlighters.Add(TSynSDDSyn.Create(self));      FHighlighters.Add(TSynSMLSyn.Create(self));      FHighlighters.Add(TSynFreePascalSyn.Create(self));      FHighlighters.Add(TSynM3Syn.Create(self));      FHighlighters.Add(TSynRCSyn.Create(self));      FHighlighters.Add(TSynPrologSyn.Create(Self));      FHighlighters.Add(TSynLuaSyn.Create(Self));      FHighlighters.Add(TSyn8051Syn.Create(Self));      FHighlighters.Add(TSynCACSyn.Create(Self));      FHighlighters.Add(TSynJavaSyn.Create(self));      FHighlighters.Add(TSynSQLSyn.Create(self));      FHighlighters.Add(TSynVHDLSyn.Create(Self));      FHighlighters.Add(TSynVerilogSyn.Create(Self));      FHighlighters.Add(TSynJSONSyn.Create(Self));      FHighlighters.Add(TSynPasSyn.Create(self));      FHighlighters.Add(TSynXMLSyn.Create(self));      FHighlighters.Sort(@CompareHighlighters);      for i:=0 to FHighlighters.Count-1 do begin        HL := TSynCustomHighlighter(FHighlighters[i]);        if Assigned(HL) then        begin          item := TMenuItem.Create(self);          MenuFileHL.Add(item);          try            item.Caption := Format('%d - %s', [i, GetHighlighterCaption(HL)]);          except            on E : Exception  do              ShowMessage(E.Message+LineEnding+' at index '+inttostr(i));          end;          item.Tag := i+1;          ComboBox3.Items.Add(item.Caption);          item.OnClick := @MenuClick;          end;      end;      SelectHighlighter(28);           ComboBox1.Items.Clear;      //load fonts                      g := TFPFontCacheList.Create;      g.SearchPath.Add('C:\Windows\Fonts');      g.BuildFontCache;      for i := 0 to g.Count - 1 do      begin        if ComboBox1.Items.IndexOf(g.Items[i].FamilyName) < 0 then          ComboBox1.Items.Add(g.Items[i].FamilyName);      end;      SynEdit := CurrentSynEdit;      if SynEdit <> nil then      begin        ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(SynEdit.Font.Name);        SpinEdit1.Value := SynEdit.Font.Size;      end;         end;

wp:
Move the shared code into a separate unit which can be "used" wherever needed.

For example (quick and dirty, and untested):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit uCommon; interface uses  ... (whatever needed); procedure PrepareHighlighters(AList: TFPList; AMenu: TMenuItem; ACombobox: TComboBox; AClickHandler: TNotifyEvent); implementation function GetHighligherCaption(vHighlighter: TSynCustomHighlighter): String;begin  ...end; procedure PrepareHighlighters(AList: TFPList; AMenu: TMenuItem; ACombobox: TComboBox; AClickHandler: TNotifyEvent);var  i: Integer;  HL: TSynCustomHighlighter;  item: TMenuitem;begin  AList.Add(TSynADSP21xxSyn.Create(self));  AList.Add(TSynFortranSyn.Create(self));  AList.Add(TSynFoxproSyn.Create(self));  AList.Add(TSynGalaxySyn.Create(self));  AList.Add(TSynBaanSyn.Create(self));  AList.Add(TSynAWKSyn.Create(self));  AList.Add(TSynHaskellSyn.Create(self));  // etc.   if (AMenu <> nil) then    for i:=0 to AList.Count-1 do begin      HL := TSynCustomHighlighter(AList[i]);      if Assigned(HL) then      begin        item := TMenuItem.Create(self);        AMenu.Add(item);        item.Caption := Format('%d - %s', [i, GetHighlighterCaption(HL)]);        item.Tag := i+1;        item.OnClick := AClickHandler;        if AComboBox <> nil then AComboBox.Items.Add(item.Caption);      end;    end;  end;end; end. //--------------------------- unit Unit1; interface uses  ... type  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);    ...  private    FHighlighers: TFPList;  end; implementation procedure TForm1.Formcreate(Sender: TObject);begin  FHighlighters := TFPList.Create;  PrepareHighlighters(FHighlighters, MenuFileHL, Combobox3, @MenuClick);  ...end; 

RayoGlauco:
Maybe you can create a procedure that receives the target form as a parameter:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SetCombos(aForm:TForm);var  aCombo: TComboBox;begin  aCombo:= TComboBox(aForm.FindComponent('ComboBox1'));  if aCombo <> nil then   aCombo.Items.Clear;  // etc... end; 
Same as previous answer, quick and untested!

wasp41p:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function CurrentSynEdit(APageControl1: TPageControl): TSynEdit;begin  if APageControl1.ActivePage <> nil then    Result := TSynEdit(APageControl1.ActivePage.Tag)  else    Result := nil;end;                                  
I'm stuck at this :'(

KodeZwerg:
I have a feeling you do things wrong, attach a demo again that shows current progress.

Navigation

[0] Message Index

[#] Next page

Go to full version