Skip to main content
Email Password   helpLost your password?
Sample Image - TicoResourceWriter.jpg

Introduction

Editing RESX files in Visual Studio is not very convenient if you want to add content like image streams, icons and things like that. I work a lot with resource files because I don't like to add a form to a library just to be able to add an image stream as a resource.

I know that there are already pretty good open source RESX editors. But they have one thing in common that I don't like: If I want to add support for a new resource type, I have to change the code of the library. But what I want is a library that supports latebinding, so that I can provide my userdefined libraries with definitions of different resources.

The Library

The TicoResourceWriter library basically contains the following classes:

Using the Library

To open a Resource file, just do the following:

ResourceManager mgr = new ResourceManager("C:\\blah.resx");
IResourceHolder itm = mgr.CreateHolderInstance(0);
mgr.Add(itm);
mgr.Edit(itm);
mgr.Save()

This creates an RESX file on c:\Blah.resx and adds one resource Item on it.

If you open a Resource File containing a Resource that has no appropriate IResourceHolder/Editor, ResourceManager will not let you save it. That's because the unknown Resources would be lost if you would save it.

So, if you want to have a complete Resource editor, you have to provide some classes by yourself. Just create a new ClassLibrary project and add TicoResourceWriter as reference and create two classes like this:

[ResourceProperties(typeof(fooEditor),"Foo Resource",typeof(fooType))]
public class fooHolder:DefaultResourceHolder
{
    public fooHolder()
    {
    }
    protected override string resourceTypeName
    {
        get
        {
            return "foo Resource";
        }
    }
    protected override object checkResource(object Value)
    {
        object RetVal = null;
        if (Value is fooType)
        {
            RetVal = base.checkResource(Value);
        }
        else
        {
            InvalidCastException up = new InvalidCastException("Value is not foo");
            throw up;
        }
        return RetVal;
    }
}
public class fooEditor:DefaultResourceEditor
{
    public fooEditor()
    {
    }
    protected override IResourceHolder checkTarget(IResourceHolder Value)
    {
        IResourceHolder RetVal = null;
        if (Value is fooHolder)
        {
            RetVal = base.checkTarget(Value);
        }
        else
        {
            InvalidCastException up = 
                    new InvalidCastException("Value is not a fooHolder");
            throw up;
        }
        return RetVal;
    }
    protected override void showEditor()
    {
        frmfooEditor tmp = new frmfooEditor();
        tmp.Value = Target.Resource as fooType;
        if (tmp.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Target.Resource = tmp.Value;
        }
    }
}

Now, just add the frmfooEditor form to the project. Design it the way you want it and you have your new Resource Library.

If you want to use your library with the demo project, you create an XML file:

<?xml version="1.0" ?>
<Config>
<LoadLib>myFooLib.dll</LoadLib>
<LoadPath>myLibPath</LoadPath>
</Config> 

You have the choice to add only a single library by using LoadLib, or to add all DLLs in a certain directory by using LoadPath.

The XML file must be located in the directory where the executable lies and must be named REConfig.xml.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralGreat work! Pin
P.Sandgren
23:33 21 Sep '09  
GeneralExcelent work... Pin
Narayanan Dayalan
2:05 16 Feb '07  


Last Updated 22 Dec 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009