从单独的表单更改另一个表单的值(解决方案不起作用)

时间:2013-10-10 18:55:12

标签: c# winforms

人民。 今天我试图从单独的表单中更改面板的背景图像。我看了几个S.O.问题,他们都说要创建一个新的表单变量并改变它的值。所以,我已经做到了,我没有得到任何错误,但我也没有 对背景的任何改变。这就是我的意思:(这是我试图改变的主要形式)

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;
            }
        }
    }
}

扩展方法必须在非泛型静态类中定义,错误发生在主窗体的第13行。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);