I want to call a string type resource inside a .resx by its name which is stored in a variable.
Let's say that I have a DataGridView1
with these columns:
| Kiwi | Apple | Grape | 'these are the columns name
I have a Core.resx
to store my resources:
| Name | Value | Comment | 'note how the name of the resource
| DGV_Kiwi | Hairy Kiwi | | 'is "DGV_" & column.Name
| DGV_Apple | Red Apple | |
| DGV_Grape | White Grape | |
When loading DataGridView1
I want to change the header to the value of the resource file. Of course, I can do it with something like:
DataGridView1.Columns(0).HeaderText = My.Resources.Core.DGV_Kiwi
DataGridView1.Columns(1).HeaderText = My.Resources.Core.DGV_Apple
DataGridView1.Columns(2).HeaderText = My.Resources.Core.DGV_Grape
But I'm lazy and I have a lot more columns in my real project. So I want to do this with a loop.
This is the first thing I've tried :
For Each Col As DataGridViewColumn In Me.DataGridView1.Columns
Col.HeaderText = My.Resources.Core.Col.Name
Next
Of course, this does not work:
Col is not a member of Core
I have also tried this:
For Each Col As DataGridViewColumn In Me.DGVComps.Columns
Col.HeaderText = My.Resources.ResourceManager.GetObject("Core.DGV_" & Col.Name)
Next
But My.Resources.ResourceManager.GetObject("Core.DGV_" & Col.Name)
returns Nothing
.
So is this thing possible? Or do I have to call my resources by name?
PS: I have given this example in VB.NET but I can make it in C# as well.
Use
Col.HeaderText = My.Resources.Core.ResourceManager.GetString("DGV_" & Col.Name)
"Core" is not part of the name of the resource item, but the name of the resource bundle itself.
When using My.Resources.ResourceManager
, you access the default resource bundle of the project. You can access this in Visual Studio in the properties dialog of the project.