How to read resource file

Posted on 2006-03-22
Last Modified: 2008-01-09
Hi everyone,
I'm using VS2005.

I've added a pictrure to the resource file.
And show it on the button

The default way to set the image is:
dynpSplitButton.Image = global::Health_Proc.Properties.Resources.AO;

However, I want to set the name AO dynamiccaly,

I mean, I need a function which takes a vairable and returns me the image/object

dim img as string = "AO"
dynpSplitButton.Image = global::Health_Proc.Properties.Resources.getResourceByName(img) <= This function is imaginary.

Question by:tolgaong
    LVL 16

    Expert Comment

    by:Swapnil Piparia
    Hi tolgaong,

    you can get string from resource (.resx) file by

    dim resourceCulture as System.Globalization.CultureInfo
    dim str as string = System.Resources.ResourceManager.GetString("EventLogEntryHeaderTemplate", resourceCulture)

    here "EventLogEntryHeaderTemplate" means your key from this you can get msg.

    LVL 6

    Expert Comment

    LVL 9

    Author Comment

    I think the above solutions are not suitable for me.
    I have some other urgent jobs for today.

    I'll try to rephare my question again tomorrow afternoon.
    Thanks for your participation.
    LVL 9

    Author Comment

    I'm back again. Sorry for the so late return.

    Let's do it step by step
    1) Create a new VB.NET project
    2) Click "Show All files"
    3) Expand My Project
    4) Select Resources.resx
    5) Right click and select "Open"
    6) Select Images from the toolbar (Left upper drop down button)
    7) Select Add Existing File from Add Resource (Next to the previous button) [I've added the zapotec file from c:\windows]
    8) Select and double click to form1 (to create form_load event)
    9) Write: PictureBox1.Image = Global.ResourceFile.My.Resources.Zapotec
    10) Run. It works.

    What I want is to get the file name as a string
    For example:
    11) Put a textbox and a button to the form.
    12) comment the page_load event
    13) Suppose that the user entered "Zapotec" to the textbox and entered the button.

    I want to load the file if it is in the resources.

    I really need this.
    To netswap:
    You wrote:
    dim resourceCulture as System.Globalization.CultureInfo
    dim str as string = System.Resources.ResourceManager.GetString("EventLogEntryHeaderTemplate", resourceCulture)

    When I past it to the designer, I couldn't see the GetString method. Actually, I need a getImage function ;)

    LVL 16

    Expert Comment

    by:Swapnil Piparia
    Hi tolgaong,

    see following code (taken from hatem72's link, it works)

     make a ResourceManager, as an assembly-ignorant ResourceManager through the
    ' CreateFileBasedResourceManager method. Simply specify the name of the resources
    ' file, and the location. The single "." indicates that the current directory should
    ' be searched the null indicates to use the default resourceset
    Dim rm As ResourceManager = CreateFileBasedResourceManager( "Greeting", ".", null)

    ' having got a reference to the resources, retrieve specific strings...
    ' Note that the CASE MATTERS
    rm.GetStream("Greeting") <-- for image kind of thing
    rm.GetString("PasswordException") <-- for text

    LVL 9

    Author Comment

    I'm sorry but I really cannot achive to work it. :(
    LVL 1

    Accepted Solution

    I added the following to the the .vb file that was automatically created for my resource files:

    Public Shared ReadOnly Property Image(ByVal ImageName As String) As System.Drawing.Bitmap
                    Dim obj As Object = ResourceManager.GetObject(ImageName, resourceCulture)
                    Return CType(obj, System.Drawing.Bitmap)
                End Get
    End Property

    You can then access the images with the following:

    YadaYada.Image = my.Resources.NameOfYourResources.Image("NameOfImage")

    Good luck, Steve
    LVL 9

    Author Comment

    That's what I'm looking for.

    Thanks very much.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now