2016-07-18 10 views
-1

私は、Buttonコントロール、ButtonSelectorというコントロールを持つWindowsフォームを持っています。文字列変数colorBgが現在値を格納しています。です。 Properties.Resourcesには、イエローとも呼ばれる画像ファイルのエントリがあります。文字列とProperties.ResourcesからBackgroundImageを設定しますか?

ButtonSelector.BackgroundImageMyApp.Properties.Resources.Yellowに設定するには、変数colorBgの値を使用しますか?私は現在、switch-caseを使用してい

string colorBg = "Yellow"; 
switch (colorBg) 
{ 
    case "Yellow": 
     buttonSelector.BackgroundImage = MyApp.Properties.Resources.yellow; 
     break; 
    case "Blue": 
     buttonSelector.BackgroundImage = MyApp.Properties.Resources.blue; 
     break; 
    case "Green": 
     buttonSelector.BackgroundImage = MyApp.Properties.Resources.green; 
     break; 
} 

感謝。

+1

の可能性の重複をそのキーを使ってリソースファイルから値を取得する方法](http://stackoverflow.com/questions/5946539/how-to-get-a-value-from-resource-file-using-its-key) – Kinetic

答えて

0

rbhatup、

ButtonSelector.BackgroundImage = MyApp.Properties.Resources.GetObject(colorbg)。

すべての星が整列していれば動作します。星であること:

  1. colorbgは、リソースの項目を描画することができる画像であることを、あなたのリソース
  2. に実際にある値が含まれています。製造において

、私はこのような何か使用して終了これとcolorbgの値のより堅牢なテストなど

+0

'プロパティ.Resources'には 'GetObject'というメソッドはありません。 – rbhat

0

の周りにtryキャッチを持っているでしょう:[

ResourceManager rm = Properties.Resources.ResourceManager; 
Bitmap myImage = (Bitmap)rm.GetObject(color); 
buttonSelector.BackgroundImage = myImage; 
関連する問題