Example of updating the icons assigned to a Toolbar

Is there an example that illustrates how to update/change the icons for an existing toolbar?

I’m missing something …

What is the problem?

if PrefsToolbar1 = Nil or PrefsToolbar1.ButtonAt(0) = Nil then Return

dim nt as NSToolbarMBS = self.NSToolbarMBS
dim items() as NSToolbarItemMBS = nt.items

for each item as NSToolbarItemMBS in items
  if item.label = PrefsToolbarLocalization.kProxy then
    dim theImage as new NSImageMBS(proxy_outline)
    theImage.isTemplate = true
    item.image = theImage
  ElseIf item.label = PrefsToolbarLocalization.kFonts then
    dim theImage as new NSImageMBS(pen_outline)
    theImage.isTemplate = true
    item.image = theImage
  ElseIf item.label = PrefsToolbarLocalization.kRegistration or item.Label = PrefsToolbarLocalization.kInAppPurchase then
   'and so on
  end if
next

I use the code to switch between the nicer old style and the not so nice new style for macOS.

Thanks, but I need to support all three platforms. I do like that you’re using Christian’s NSImageMBS and the isTemplate feature!

You have to be careful doing it this way, because if you change the enabled state of the toolbaritem with the Xojo framework, it wipes out the icon. There may be other toolbar functions where Xojo resets what was done via declares.

It’s one of those things where once you start, you end up having to use more and more work arounds. Because I targeted the macOS only, I was able to use pushbuttons in the toolbar, which not only gave me the look I wanted, but avoided Xojo deciding it doesn’t like icons that were not set there via their framework.

1 Like

I use a template image in my toolbar (the same for all items), then on status bases I change to what I need:

DesktopToolbarButton(toolbar1.mySpecificButton).Icon=myDefinedPicture

where toolbar1 is the instance of the toolbar defined in the project and there the ToolbarButtons must be public so you can directly identify them

Otherwise you can make an empty toolbar object, add the buttons in the Window opening event giving a name and iterate (buttonAt) to search your button and then you can change the icon, update the enabled property and so on.