Solved

Visual Basic Resource Files

Posted on 2014-10-13
10
186 Views
Last Modified: 2014-10-13
I want to move all of my English strings into a Resource file (an resx).

I've been able to create the resx and add it into my Project, but I am struggling about the code as to how to refer to it. I believe I should use GetString to get the strings, but it's the code getting to the resource file in the first place that's the problem.

Looking online, people have been talking about Assembly and ResourceManager etc., but I can't get any of it to work. Maybe I am missing some "Imports" at the beginning.

So, is anyone able to give some code for accessing a single string in a resource file (i.e. with all of the necessary Imports that I need).

I am using MS Visual Studio 2010.

Many thanks,
0
Comment
Question by:Phillip Burton
  • 7
  • 3
10 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40377034
The simplest way would be to use the My namespace.

e.g.

Dim aValue As String = My.Resources.KeyName

Open in new window


The "longer" way would be to create a ResourceManager:

Imports System.Reflection
Imports System.Resources

...

Dim manager As New ResourceManager("YourAssemblyName.Resources", Assembly.GetExecutingAssembly())

Dim aValue As String = manager.GetString("KeyName")

Open in new window

0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377057
I'm looking at the ResourceManager solution, because I want to use localised strings (which I don't think you can do with the My.Resources - though if I am wrong, please let me know).

I have got a file called FP.resx with a Key called TestString (see attached).

I am using this code:

        Dim manager As New ResourceManager("FP.resx", Assembly.GetExecutingAssembly())
        MsgBox(manager.GetString("TestString"))

Open in new window


It fails on the second line.

If it helps, I've got two projects, FP1 and FP2. The code starts in FP1, goes to FP2 (a .vb file), and I am calling it from FP2. Does the Assembly bit need changing?
Resource.png
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377060
I note that your call says "YourAssemblyName.resources" - I am using a resx file.

Does that make a difference? If it does, how do I use a .resources file?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40377108
Honestly, it's been a while since I've looked at resources, but from what I recall, there is nothing you should have to do in terms of selecting a specific file. .NET/Windows takes care of selecting the appropriate file/library based on what the end-user's system is set up as. If the end user is set up for German, then you would have a resource file that is named "SomeName.de.resx". I think it may be a bit more involved in ASP.NET, but not much.
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377118
Thankfully, I'm using Visual Studio 2010 instead of ASP.NET, but still don't get why the above code isn't working.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377134
Manager.ResourceSets = 0.
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377169
If it helps any:

MsgBox(My.Resources.FP.TestString)

does work.

However, I also have two resource files called "FP.resx" and "FP.fr-FR.resx", and the following still gives me the English version:

        My.Application.ChangeCulture("fr-FR")
        MsgBox(My.Resources.FP.TestString)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40377199
I think I might have to defer this to someone with more experience with multi-culture applications. If you don't get a decent response before tonight, then I'll take another stab at it when I get home. Sorry for not being more helpful at this time.
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377263
You were very helpful.

I think I've found the solution re the simplest way. Instead of:

 My.Application.ChangeCulture("fr-FR")

I have to use

 My.Application.ChangeUICulture("fr-FR")

So, problem solved. Thank you very much.
0
 
LVL 24

Author Closing Comment

by:Phillip Burton
ID: 40377265
Thank you very much - very helpful.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

13 Experts available now in Live!

Get 1:1 Help Now