0

I posted this earlier, but deleted it cause i thought i could contribute more, but soon realized i could not. Anyways, I need to create tabs, groups and actionitems at runtime.

procedure TfrmMain.Button2Click(Sender: TObject);
var
 Tab: TRibbonTabItem;
 Group: TRibbonGroup;
 //Item: TActionItem;
begin
 Tab:= Ribbon1.Tabs.Add;
 Tab.Caption:= 'Test';
 Group:= TRibbonGroup.Create(Tab.Page);
 //Tab.Page.AddGroup(Group);
 Group.Caption:= 'Test Group';
 //create actionitem on group
end;

Any help would be greatly appreciated. I cant seen to find any good resource showing how to accomplish this at runtime

All the tabs and groups for each tab seem to be created without any issues. I cant seem to get the buttons to create correctly. Below is the code for the buttons to be created on each group. I have a show message that I look at for the number of buttons before it goes into this for loop and the number is always correct. Yet, only one button seems to be displayed on the group.

   //ShowMessage(IntToStr(ButtonNames.Count));
   for K:= 0 to ButtonNames.Count-1 do
   begin
    BarItem := ActionManager1.ActionBars.Add;
    BarItem.ActionBar := RGroup;
    BarAction := BarItem.Items.Add;
    BarAction.Caption := ButtonNames[K];
   end; //for K:= 0 to ButtonNames.Count-1 do

Below is the entire source code for the creation of the entire set of tabs, groups, and buttons

procedure TfrmMain.Button1Click(Sender: TObject);
var
 I, J, K: Integer;
 JSONData, TabNames, GroupNames, ButtonNames: TStringList;
 NavData: TJSONObject;
 Tabs, Groups, Buttons, Tab, Group, Button: TJSONValue;
 TabItem: TRibbonTabItem;
 RGroup: TRibbonGroup;
 BarItem: TActionBarItem;
 BarAction: TActionClientItem;
begin
 JSONData:= TStringList.Create;
 if openDialog1.Execute then
 begin
  JSONData.LoadFromFile(openDialog1.FileName);
  try
    NavData:= getJSONObj(JSONData.Text);

    if NavData <> nil then
    begin
     {Ribbon1:= TRibbon.Create(self);
     Ribbon1.Parent:= Self;

     ActionManager1:= TActionManager.Create(self);
     Ribbon1.ActionManager:= ActionManager1;}

     Tabs:= getTabs(NavData);
     TabNames:= TStringList.Create;
     getTabNames(Tabs, TabNames);
     for I:= 0 to TabNames.Count-1 do
     begin
      TabItem := Ribbon1.Tabs.Add;
      TabItem.Caption := TabNames[I];
      Tab:= getTabByName(Tabs, TabNames[I]);
      Groups:= getGroups(TJSonObject(Tab));
      GroupNames:= TStringList.Create;
      getGroupNames(Groups, GroupNames);
      for J:= 0 to GroupNames.Count-1 do
      begin
       RGroup := TRibbonGroup.Create(Ribbon1);
       RGroup.Parent := TabItem.Page;
       RGroup.Caption := GroupNames[J];

       Group:= getGroupByName(Groups, GroupNames[J]);
       Buttons:= getButtons(TJSonObject(Group));
       ButtonNames:= TStringList.Create;
       getButtonNames(Buttons, ButtonNames);
       //ShowMessage(IntToStr(ButtonNames.Count));
       for K:= 0 to ButtonNames.Count-1 do
       begin
        BarItem := ActionManager1.ActionBars.Add;
        BarItem.ActionBar := RGroup;
        BarAction := BarItem.Items.Add;
        BarAction.Caption := ButtonNames[K];
       end; //for K:= 0 to ButtonNames.Count-1 do
      ButtonNames.Free;
      end; //for J:= 0 to GroupNames.Count-1 do
      GroupNames.Free;
     end;
     TabNames.Free;
    end;
  finally
    NavData.Free;
  end;
 end;
 JSONData.free;
end;
LuvRAD
  • 118
  • 1
  • 4
  • 13
  • Buttons, icons, what ever goes on a group. I saw a post on the internet that mentioned actionItems. Ill be happy if i can just get the tabs with groups showing, but it would be nice to display things on the groups as well. I need clickable items. – LuvRAD Jan 16 '14 at 00:46
  • Having said that, I think there are larger issues than what i am trying to do now and I am going to post a secondary question – LuvRAD Jan 16 '14 at 00:47
  • You've [`posted one`](http://stackoverflow.com/q/21148878/960757) where I've replied you with a link to a simple code showing how to create tab with a group and a button in that group. I thought you have seen it before you've deleted the post... – TLama Jan 16 '14 at 00:51
  • I deleted it, cause there was no answer, and cause i thought i solved it myself, and felt kinda silly posting such an easy question. It turns out, its not so easy as i thought. It also turns out there are bigger issues than i knew. I posted a second question – LuvRAD Jan 16 '14 at 00:54
  • 2
    Sometimes you need to wait for the answer. Sometimes you may read comments... Never mind, assuming you mean actions from an action manager, you can use a code [`like this`](http://pastebin.com/RjbX1Gzf). – TLama Jan 16 '14 at 01:03
  • That looks like it might work, let me try that out – LuvRAD Jan 16 '14 at 01:05
  • That works great! If you want to post that as an answer, i will accept. Also, check out o=my other question: http://stackoverflow.com/questions/21151426/issues-with-tribbon-in-delphi-xe-5 - seems to be the same issue as this one back in 2011 (surely they must have worked on this) - http://stackoverflow.com/questions/8421205/tribbon-does-not-show-tabs – LuvRAD Jan 16 '14 at 01:19
  • @TLama - can you look at my edit please, and if you have a solution / answer to what i am doing wrong, post a solution - please – LuvRAD Jan 22 '14 at 18:35
  • 1
    You've made from the original post a different question, so I'll answer you again just in comment :-) You cannot add more than one action bar into a group. And since you're trying to add an action bar into the same group with each iteration of your loop, the previously created action bar is "overwritten" by the new one. You certainly meant to write it [`like this`](http://pastebin.com/chsnEV97). – TLama Jan 22 '14 at 21:44
  • Not sure why you keep making a big deal about the original post, i told you it was a mistake. Is this your way of gaining "ups"? I just dont get it. Anyways, thanks for the answer. Try and relax a little. – LuvRAD Jan 22 '14 at 22:51
  • I don't care about reputation on StackOverflow anymore because of "random" upvoters voting for posts just because they are from certain users and because I've reached what I wanted. So sorry if it looks that I don't want to answer just your question. I'm doing this quite often these days. – TLama Jan 22 '14 at 23:03

1 Answers1

0

I found the answer posted by TLama was good, and is outside this site.

This question should be answered properly, so I add the answer, if TLama want to add it has his answer, please do and ill remove this one.

To add on runtime actions to the ribbon menu, you need to have a group. The group needs to connect to a single barItem(and the bar item connect to the group). For each new button on the menu you add BarAction.

procedure TForm1.Button1Click(Sender: TObject);
var
  Group: TRibbonGroup;
  TabItem: TRibbonTabItem;
  BarItem: TActionBarItem;
  BarAction: TActionClientItem;
begin
  TabItem := Ribbon1.Tabs.Add;
  TabItem.Caption := 'Tab caption';

  Group := TRibbonGroup.Create(Ribbon1);
  Group.Parent := TabItem.Page;
  Group.Caption := 'Group caption';

  BarItem := ActionManager1.ActionBars.Add;

  BarAction := BarItem.Items.Add;
  BarAction.Action := Action1;
  BarAction.Caption := 'Action 1 caption';

  BarAction := BarItem.Items.Add;
  BarAction.Action := Action2;
  BarAction.Caption := 'Action 2 caption';

  BarItem.ActionBar := Group;
end;
none
  • 4,669
  • 14
  • 62
  • 102