我想通过存储在变量中的.resx名称来调用字符串类型的资源。
假设我在这些列中有一个DataGridView1
:
| Kiwi | Apple | Grape | 'these are the columns name
我有一个Core.resx
来存储我的资源:
| 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 | |
在加载DataGridView1
时,我想将标题更改为资源文件的值。当然,我可以用类似的方法做到这一点:
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
但是我很懒,在我的真实项目中还有很多专栏文章。所以我想循环执行此操作。
这是我尝试过的第一件事:
For Each Col As Datagridviewcolumn In Me.DataGridView1.Columns
Col.HeaderText = My.Resources.Core.Col.Name
Next
当然,这不起作用:
Col不是Core的成员
我也尝试过这个:
For Each Col As Datagridviewcolumn In Me.DGVComps.Columns
Col.HeaderText = My.Resources.ResourceManager.GetObject("Core.DGV_" & Col.Name)
Next
但是My.Resources.ResourceManager.GetObject("Core.DGV_" & Col.Name)
返回Nothing
。
那么这可能吗?还是我必须按名称调用资源?
PS:我已经在VB.NET中给出了这个示例,但是我也可以在C#中实现它。