Toolbar button Icon

How do I change the icon of a button in code that has been created in the IDE?

Also while in the IDE, or at runtime?
And what specific type of button?

At Runtime so I can show alternate themed pushbutton icons

With MBS plugin:

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

for each item as NSToolbarItemMBS in items
  
  if Color.IsDarkMode then
    
    if item.label = PrefsToolbarLocalization.kProxy then
      dim theImage as new NSImageMBS(proxy_dark)
      theImage.isTemplate = False
      item.image = theImage
    ElseIf item.label = PrefsToolbarLocalization.kFonts then
      dim theImage as new NSImageMBS(pen_dark)
      theImage.isTemplate = False
      item.image = theImage

and so on. I have one set of ugly outline icons and one set of nicely colored icons.

1 Like

If the button has an icon property, and you created it in the IDE, it has a name

eg button1

so just issue button1.icon = nameoficon
and button1.invalidate

if you clone the button at runtime, then it is part of a control array.
You would need to do something like button1(0).icon = nameoficon

Standard buttons do not have icons, however.
BevelButtons do- but naturally, they have been deprecated.

Just checking, you don’t use the IDE to create the initial Toolbar and that you create it all in code?

I don’t seem to be able to reference the icon property of the Toolbar button I create in the IDE

What does this mean?

Toolbar button

Thank you. Not a pushbutton, then. :slight_smile:
Beatrix’s solution should work in this case.
Do you have the MBS plugins?

1 Like

Screen Shot 2021-12-07 at 12.05.18 am

.Icon is not an option??

Have you tried it anyway (Jeff’s code, I mean)? If so what does the compiler have to say?

Try

ToolbarButton(TBNew).Icon = MyPicture
1 Like

I create the toolbar in the IDE, but I have 3 sets of icons (light, dark, outline). Also I can change individual icons.

1 Like

Ok I have both Beatrix and Martin T solutions working MBS took me a bit more to work out and Martin’s was probably what I was asking in the beginning but could wrangle with the correct syntax.
DesktopToolbarButton(Toolbar12.TBNew).Icon = New32_OS11
DesktopToolbarButton(ToolbarInstance.ButtonName).Icon = NewImage
Thanks :slight_smile:

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.