Images in Resource Files

381 views
Skip to first unread message

Andy @ Corero

unread,
Dec 5, 2013, 5:23:59 AM12/5/13
to migrated-...@googlegroups.com
Hi,
 
We have loaded images into Properties/Resources.resx and now we want to show them on a form.
 
If we want to display them all the time, this is simple -
add a System.Windows.Form.PictureBox on the form, and from the Image property you can select from a list of the images in the resource.resx file.
 
Now, if we want the image to apply conditionally - ie it's on a tab or we want to condition visibility, we have a problem.
The System.Windows.Form.PictureBox doesn't have the BoundTo or BindVisible options, so we can't use that.
The Firefly.Box.UI.PictureBox has these, but doesn't have the Image property to select an image from the .resx, so we can't use that either....
 
Is there a way around this?
 
Thanks
 
Andy

Zohar

unread,
Dec 5, 2013, 6:04:14 AM12/5/13
to
Hi Andy,
Add the following code to the PictureBox class in your ENV.UI or Theme.UI:
Image _image;
public Image Image
{
    get { return _image; }
    set
    {
        _image = value;
        if (string.IsNullOrEmpty(ImageLocation))
            ImageLocation = "z"; // becuase at the current version, images are not loaded if there is no location
        }
    }
protected override Image GetImage(string imageLocation)
{
    if (Image != null)
        return Image;
    return base.GetImage(imageLocation);
}

This will add the ability to choose a resx resource to the PictureBox, like the System.Windows.Forms.PictureBox.

Let me know if that helped you,
Zohar

Andy @ Corero

unread,
Dec 5, 2013, 5:51:28 AM12/5/13
to migrated-...@googlegroups.com
Hi Zohar,
 
Many thanks - that's sorted it.
 
I'd just worked out a workaround by changing the visibility property of the image on an on change event of the tab; but yours is a much neater (and generic) solution.
 
Thanks again
 
Andy

Mike Read

unread,
May 13, 2015, 10:46:21 AM5/13/15
to migrated-...@googlegroups.com
Hi

Following up from this, having changed the master for a couple of the buttons, I find programs already written are not showing the amended image as we're holding local copies of the images.

I've found that if I add a line button.Image = ModelsAndTables.Properties.Resources.imagename to end of the constructor in filename.UI.cs I can dynamically point it to it our 'master' resource file.
Is there any way to point to this from the Image property in the designer? Currently the only options are "Local Resource" or "Project Resource". There's no option to point to a resource in a different project in the solution.
It would be good to see the correct image in the designer as well as the runtime.

Mike Read

Zohar

unread,
May 14, 2015, 1:45:14 AM5/14/15
to migrated-...@googlegroups.com
Hi Mike,
We had this problem too (assuming I understood the problem correctly).

Our application has one main namespace shared between all projects and all of our projects included a Resource collection.
This caused a problem when calling the namespace.Properties.Resources actually refered to an isolated scope of the resources collection for each project, although it's supposed to be one big namespace resource collection.

The solution was actually pretty straight forward - just deleted all project's resource file except the one in the base project.
After doing that, all calls to namespace.Properties.Resources refered to the base Resource collection so we could save all our resource images in the base project.

We did, however, find that sometimes the visual studio form designer fails to load the images and claims that the resource collection does not contain the image (although the project builds just fine), I guess the designer looks for the images in the project's resource collection.

A second thing we did is to add a resource manager in the base project that holds a property for each image in the resource so that we can access them anywhere in our solution without having to use the Properties.Resource collection directly and then just set the buttons image in the UI.Form constructor after the InitializeComponents method is called. this approach has the disadvantage that the designer does not show the image on design time, but it helps keeping just one instance of the image.
Something like this:

public abstract class ResourceProvider
{
    public static Image Attachments
    {
        get { return Properties.Resources.Attachment; }
    }
}

I guess there are some other, more common practices out there, but this works for us for the mean time.
Hope this helps...
Zohar
Reply all
Reply to author
Forward
0 new messages