Help with Access Violation on TadxRibbonbutton.OnClick

Add-in Express™ Support Service
That's what is more important than anything else

Help with Access Violation on TadxRibbonbutton.OnClick
 
Francesco Faleschini




Posts: 26
Joined: 2014-05-15
As anticipated in https://www.add-in-express.com/forum/read.php?a&FID=1&TID=14549 i try to explain my problem so that i can get a solution faster without implementing EurekaLog.

A customer using my addin has an Access Violation when clicking on a TadxRibbonbutton.
This occurs on Win7 and WIn10 machines equipped with Office H&B 2013 or 2016.
In my system I haev Win7 and Office2013_64bit and i cannot reproduce the problem.
The customer moreover reports that on other 63 bit machines (Win7 and Win10) but with Office32 the problem is not there.

The TadxRibbonbutton that gives a problem is a runtime created button.

I create some menuitems under a design-time-created "Recent" item (basically i retrieve recent items from db and create one menuitem per Recent item).

This is the code I use to create the menuitems:

procedure TAddInModule.CreateRibbonRecentButtons;
var
   pil1,pil2 : TadxRibbonMenu;
   i : Integer;
   newButton: TadxRibbonbutton;
begin
  pil1 := RCM.Controls[0].AsRibbonMenu;
  pil2 := pil1.Controls[2].AsRibbonMenu;
  for i :=  0 to  pil2.Controls.Count -1  do
    pil2.Controls.Delete(0);
  dmdAddin.SetRecentItemList; // here i query the db and i populate dmdAddin.RecentList
  for i := Low(dmdAddin.RecentList) to high(dmdAddin.RecentList) do
  begin
    // Button
    newButton := pil2.Controls.Add(rctButton).AsRibbonButton;
    newButton.Id := 'uniqueStringRecent'+IntToStr(i);
    newButton.Caption := dmdAddin.RecentList[i].Desc;
    newButton.Tag := dmdAddin.RecentList[i].ID;
    newButton.OnClick := DoOnRRecentClick;
  end;
end;

I tried to put many MsgBox calls inside the DoOnRecentClick event handler but when customer runs this it gets Access Violation even before the first MsgBox so i suspect the problem is out of the DoOnRRecentClick method.

Do you see some flaw in the code above? THe strange thing is that the Addin works fine on most systems.

Please suggest. Thanks.
Posted 26 May, 2017 10:04:02 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Francesco,

Try specifying newButton.Ribbons. Does it work?


Andrei Smolin
Add-in Express Team Leader
Posted 29 May, 2017 06:16:24 Top
Francesco Faleschini




Posts: 26
Joined: 2014-05-15
Hi Andrei.

Thanks for the reply.

Could you please restate the solution?

Where should I use newButton.Ribbon in my code?

Thanks.
Posted 01 Jun, 2017 02:34:44 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Francesco,

Every Ribbon component provides the Ribbons (not Ribbon) property. This property specifies the Ribbon(s), in which the corresponding Ribbon control will be created. You should also set it on the button you create.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Jun, 2017 02:50:33 Top
Francesco Faleschini




Posts: 26
Joined: 2014-05-15
You mean like this (Check added line): Thanks!
procedure TAddInModule.CreateRibbonRecentButtons; 
var 
   pil1,pil2 : TadxRibbonMenu; 
   i : Integer; 
   newButton: TadxRibbonbutton; 
begin 
  pil1 := RCM.Controls[0].AsRibbonMenu; 
  pil2 := pil1.Controls[2].AsRibbonMenu; 
  for i :=  0 to  pil2.Controls.Count -1  do 
    pil2.Controls.Delete(0); 
  dmdAddin.SetRecentItemList; // here i query the db and i populate dmdAddin.RecentList 
  for i := Low(dmdAddin.RecentList) to high(dmdAddin.RecentList) do 
  begin 
    // Button
    newButton.Ribbons := pil1.Ribbons; // THIS LINE ADDED
    newButton := pil2.Controls.Add(rctButton).AsRibbonButton
    newButton.Id := 'uniqueStringRecent'+IntToStr(i); 
    newButton.Caption := dmdAddin.RecentList[i].Desc; 
    newButton.Tag := dmdAddin.RecentList[i].ID; 
    newButton.OnClick := DoOnRRecentClick; 
  end; 
end; 
Posted 01 Jun, 2017 03:10:12 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Yes. But the line should be moved down in order to work correctly.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Jun, 2017 03:19:14 Top
Francesco Faleschini




Posts: 26
Joined: 2014-05-15
Please tell me where.

After
newButton.OnClick := DoOnRRecentClick ?

Thanks for the prompt reply.
Posted 01 Jun, 2017 03:36:02 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Anywhere after newButton := pil2.Controls.Add(rctButton).AsRibbonButton. Otherwise you'll get an exception.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Jun, 2017 03:41:17 Top
Francesco Faleschini




Posts: 26
Joined: 2014-05-15
Thanks. I will try.
Posted 01 Jun, 2017 03:43:47 Top