1

人们。今天我试图从一个单独的表单中更改面板的背景图像。我看过一些 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 行出现错误。

4

2 回答 2

0

您的“其他”表单需要一个主表单 ( Form_Main) 的实例,您已将其存储为名为 的变量frm。使用此变量,您可以调用主表单实例,如下所示:

frm.changeGridSize(newSize);

相反,您尝试使用以下语法调用表单,就像它是静态类一样:

Form_Main.changeGridSize(newSize);

要将主表单的实例传递给“其他”表单,您可以将其传递给其他表单的构造函数,如下所示:

public class OtherForm : Form
{
    Main_Form _mainForm;

    // Constructor
    public OtherForm(Main_Form theMainForm)
    {
        _mainForm = theMainForm;
    }

    public static void changeGridSize(int newSize)
    {
        _mainForm.changeGridSize(newSize);
    }
}

最后,当您创建“其他”表单时,您需要传递主表单的一个实例,如下所示:

OtherForm theOtherForm = new OtherForm(this);
于 2013-10-10T18:58:24.117 回答
0

您必须更改要更改的表单的特定实例中的变量。我建议将表单添加为方法的参数或将静态方法转换为扩展方法。

范围:

public static void changeGridSize(Form_Main frm, int newSize)
{
    switch (newSize) 

扩展方法:

public static void changeGridSize(this Form_Main frm, int newSize)
{
    switch (newSize)

扩展方法的使用将来自调用表单:

myFormMain.changeGridSize(newSize);
于 2013-10-10T19:00:01.657 回答