Reputation: 1886
I've been trying to set a button's image from code in a GTK (Mono) application i'm building in Xamarin studio, but so far i've had no luck. I can't seem to get any of the methods that work for C#.Net to work in Mono.Netm and I have no idea where else to turn since I've searched the web for anything I could think of, and the only results I got were for C#.Net.
My question is simple, and I hope I've overlooked something and that the answer will be simple too:
What would be the way to set a Gtk.Button image from an embedded resource by code?
Things I've tried:
* Reflection
* ResourceManager
* Image.FromStream (Doesn't exist in Gtk.Image)
* Whatever I could think of...
Chances are I did something wrong along the line but if so I have no idea what, so please help me out, even if it's by pointing me in the right direction on google...
EDIT:
For some reason @Jester's solution didn't seem to work for me because if you change a button's image, and right after change it's label, the button will only display the label and remove the image. But that's a poblem for another thead.
Upvotes: 1
Views: 2480
Reputation: 58762
Not sure how you managed to miss the Image.LoadFromResource method or the associated constructor :)
That is you can simply do:
btn.Image = Image.LoadFromResource("name-of-resource");
Update: works fine for me with this sample code:
using Gtk;
using System;
public class ButtonApp
{
public static int Main (string[] args)
{
Application.Init ();
Window win = new Window ("Button Tester");
win.SetDefaultSize (200, 150);
win.DeleteEvent += new DeleteEventHandler (Window_Delete);
Button btn = new Button ("Click Me");
btn.Image = Image.LoadFromResource("pic");
win.Add (btn);
win.ShowAll ();
Application.Run ();
return 0;
}
static void Window_Delete (object obj, DeleteEventArgs args)
{
Application.Quit ();
args.RetVal = true;
}
}
Compile using: mcs -res:foo.png,pic -pkg:gtk-sharp-2.0 ButtonApp.cs
Upvotes: 4