這可能沒有多大意義,但是如果用戶搜索了 PictureBox 并且它與當前字串中的一個匹配,我正在嘗試將 PictureBox 的影像更改為用戶請求。
"System.ArgumentException: 'Parameter is not valid.'"當我運行它時,我一直收到一個。
陣列中的每個字串都與專案資源中的影像名稱相匹配。
string[] TShirts = new string[] { "New York", "Melbourne", "London", "Sydney", "Los Angeles" };
for (int i = 0; i < TShirts.Length; i )
{
if (txtSearch.Text == TShirts[i])
{
string x = TShirts[i];
ptbItem.Image = new Bitmap(x); // error occurs here (this is what I can't work out)
}
}
uj5u.com熱心網友回復:
您可以使用ResourceManager在運行時按名稱訪問資源。
您可以將字串傳遞給它的GetObject()方法并轉換為資源型別。
例如:
int idx = Array.IndexOf(TShirts, txtSearch.Text);
if (idx >= 0) {
ptbItem.Image?.Dispose();
ptbItem.Image = (Bitmap)Properties.Resources.ResourceManager.GetObject(TShirts[idx]);
}
由于您有一個字串集合,您可以填充 ComboBox 而不是使用 TextBox 作為輸入,因此您的用戶無需猜測名稱并避免輸入錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/450343.html
