如何从资源中获取特定图像? [英] How do I get specific image from resources?
问题描述
所以我有一个名为Portugal,United States,Spain的按钮。
我有一张名为Portugal.png的美国国旗图片,美国.png,Spain.png在我的资源中。
我希望每当我点击我的表格按钮(例如葡萄牙按钮)时,Picturebox1.Image变成Portugal.png和每当我点击美国按钮时,PIcturebox1.image变成United States.png
伪代码:
Private Sub ButtonClick
Picturebox1.image = My.Resources(button1.text&.png)
End Sub
我尝试过:
My.Resources(Button1.Text&.png)。
*不起作用*
So I have a button named "Portugal", "United States", "Spain".
I have a flag image named Portugal.png, United States.png, Spain.png in my resources.
I want whenever I click the buttons my form (e.g Portugal button) the Picturebox1.Image turns into Portugal.png and whenever I click the United States button the PIcturebox1.image turns into United States.png
Pseudo code:
Private Sub ButtonClick
Picturebox1.image = My.Resources(button1.text & ".png")
End Sub
What I have tried:
My.Resources(Button1.Text & ".png").
*Doesn't work*
推荐答案
VB.NET的我的.Resources
提供对项目资源的强类型访问。它具有嵌入在资源中的每个文件的属性,但它不提供索引器来访问具有动态名称的文件。
但是,它 提供名为ResourceManager
的属性,它允许您按名称访问资源:
使用ResourceManager类检索资源 [ ^ ]
VB.NET's "My.Resources
" provides strongly-typed access to the resources in your project. It has a property for each file embedded in the resources, but it doesn't provide an indexer to access the files with a dynamic name.
However, it does provide a property calledResourceManager
, which will let you access the resources by name:
Retrieving Resources with the ResourceManager Class[^]
Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim button As Button = DirectCast(sender, Button)
Dim resource As Object = My.Resources.ResourceManager.GetObject(button.Text)
Picturebox1.Image = DirectCast(resource, Image)
End Sub
注意:您只需要资源的名称,而不是文件扩展名。
NB: You only need the name of the resource, not the file extension.
这篇关于如何从资源中获取特定图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!