簡體   English   中英

VB.Net:從 My.Resources 中動態選擇圖像

[英]VB.Net: Dynamically Select Image from My.Resources

我的 My.Resources 中有一組圖像。 我想選擇要在運行時動態顯示的圖像。 我該怎么做呢?

'Static (Compile time) Assignment
UltraPictureBox1.Image = my.Resources.zoo_picture_1

'Dynamic (Runtime) Assignment
UltraPictureBox1.Image = ???

找到解決方案:

UltraPictureBox1.Image = _
    My.Resources.ResourceManager.GetObject(object_name_as_string)

這在運行時也適用於我:

UltraPictureBox1.Image = My.Resources.MyPicture

不涉及字符串,如果我更改名稱,它會通過重構自動更新。

確保您不包括資源的擴展名,也不包括它的路徑。 它只是資源文件名。

PictureBoxName.Image = My.Resources.ResourceManager.GetObject("object_name") 
Dim resources As Object = My.Resources.ResourceManager
PictureBoxName.Image = resources.GetObject("Company_Logo")

有時您必須更改名稱(或檢查以從編譯器自動獲取它)。

例子:

文件名 = amp2-rot.png

它不工作:

PictureBoxName.Image = resources.GetObject("amp2-rot.png")

它有效,就像我的amp2_rot一樣:

 PictureBox_L1.Image = My.Resources.Resource.amp2_rot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM