我制作了 4 个单选按钮,并将它们的标签设置为木头、钻石、粘土和砖块。我想找到选中的,并根据选定的单选按钮设置 Bitmap 属性。忽略它应该分成更多方法的事实,这将在我重构时完成。Map 和 Cell 中的属性 cellBgImage 为空。
void setCellBgImage()
{
string cellBgImage = "";
foreach (Control c in myMap.myForm.Controls)
{
if (c is RadioButton)
{
RadioButton radio = c as RadioButton;
if (radio.Checked)
{
cellBgImage = radio.Tag.ToString();
}
}
}
switch (cellBgImage)
{
case "wood":
myMap.cellBgImage = (Bitmap)Bitmap.FromFile("Images/wood.png");
break;
case "diamond":
myMap.cellBgImage = (Bitmap)Bitmap.FromFile("Images/diamond.png");
break;
case "clay":
myMap.cellBgImage = (Bitmap)Bitmap.FromFile("Images/clay.png");
break;
case "brick":
myMap.cellBgImage = (Bitmap)Bitmap.FromFile("Images/brick.png");
break;
}
foreach (Cell cell in myMap.myCells)
{
cell.myBgImage = myMap.cellBgImage;
}
}