人们。今天我试图从一个单独的表单中更改面板的背景图像。我看过一些 SO 问题,他们都说要创建一个新的表单变量并更改它的值。所以,我已经这样做了,我没有收到任何错误,但我也没有对背景进行任何更改。这就是我的意思:(这是我试图改变的主要形式)
public static void changeGridSize(int newSize)
{
Form_Main frm = new Form_Main();
switch (newSize)
{
case 16:
frm.panelBoard.BackgroundImage = Properties.Resources.grid_16;
break;
case 32:
frm.panelBoard.BackgroundImage = Properties.Resources.grid_32;
break;
case 64:
frm.panelBoard.BackgroundImage = Properties.Resources.grid_64;
break;
case 128:
frm.panelBoard.BackgroundImage = Properties.Resources.grid_128;
break;
}
}
这是另一种形式:
int newSize = 16;
Form_Main.changeGridSize(newSize);
这很可能是一些愚蠢的事情,但我对此感到困惑。
编辑
这里的文件
主要形式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LWJSGL_Level_Editor
{
public partial class Form_Main : Form
{
public static int gridSize = 32;
public Image pbg
{
get { return this.panelBoard.BackgroundImage; }
set { this.panelBoard.BackgroundImage = value; }
}
public String curTile = "";
public Form_Main()
{
InitializeComponent();
}
private void Form_Main_Load(object sender, EventArgs e)
{
}
private void buttonTile_Click(object sender, EventArgs e)
{
//show file dialog
openFileDialogTile.ShowDialog();
}
private void openFileDialogTile_FileOk(object sender, CancelEventArgs e)
{
curTile = openFileDialogTile.FileName;
Msg(curTile);
}
public void Msg(String str)
{
MessageBox.Show(str);
}
private void buttonSettings_Click(object sender, EventArgs e)
{
Form_Settings fs = new Form_Settings();
fs.ShowDialog();
}
public void changeGridSize(int newSize)
{
switch (newSize)
{
case 16:
this.panelBoard.BackgroundImage = Properties.Resources.grid_16;
break;
case 32:
this.panelBoard.BackgroundImage = Properties.Resources.grid_32;
break;
case 64:
this.panelBoard.BackgroundImage = Properties.Resources.grid_64;
break;
case 128:
this.panelBoard.BackgroundImage = Properties.Resources.grid_16;
break;
}
}
}
}
其他形式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LWJSGL_Level_Editor
{
public partial class Form_Settings : Form
{
int newSize;
public Form_Settings()
{
InitializeComponent();
}
private void buttonSaveSettings_Click(object sender, EventArgs e)
{
//save settings
Form_Main.gridSize = newSize;
Form_Main frm = new Form_Main();
frm.changeGridSize(newSize);
this.Close();
}
private void radioButtonGridSize16_CheckedChanged(object sender, EventArgs e)
{
newSize = 16;
}
private void radioButtonGridSize32_CheckedChanged(object sender, EventArgs e)
{
newSize = 32;
}
private void radioButtonGridSize64_CheckedChanged(object sender, EventArgs e)
{
newSize = 64;
}
private void radioButtonGridSize128_CheckedChanged(object sender, EventArgs e)
{
newSize = 128;
}
private void Form_Settings_Load(object sender, EventArgs e)
{
switch (Form_Main.gridSize)
{
case 16:
this.radioButtonGridSize16.Checked = true;
break;
case 32:
this.radioButtonGridSize32.Checked = true;
break;
case 64:
this.radioButtonGridSize64.Checked = true;
break;
case 128:
this.radioButtonGridSize128.Checked = true;
break;
}
}
}
}
Extension 方法必须在非泛型静态类中定义 主窗体的第 13 行出现错误。