从单独的表单更改另一个表单的值(解决方案不起作用) [英] Changing values of another form from a separate form (solution not working)

查看:35
本文介绍了从单独的表单更改另一个表单的值(解决方案不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

peoples. Today I'm attempting to change the background image of a panel from a separate form. I've looked at a few S.O. questions and they have all said to create a new form variable and change it's values. So, I've done this, and I don't get any errors, but I also don't get any change to the background. Here's what I mean: (this is the main form I'm trying to change)

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

这很可能是愚蠢的事情,但我对这个很困惑.

It's most likely something stupid but I'm stumped on this one.

编辑

这里是文件

主要形式

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 method must be defined in non-generic static class 错误发生在主窗体的第 13 行.

The Extension method must be defined in non-generic static class error is occuring on line 13 of the main form.

推荐答案

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

Your "other" form needs an instance of the main form (Form_Main), which you have stored as a variable named frm. With this variable you can call the main form instance, like this:

frm.changeGridSize(newSize);

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

Instead you are trying to call the form like it is a static class by using the syntax:

Form_Main.changeGridSize(newSize);

要获取主窗体的实例到其他"窗体,可以将其传递给另一个窗体的构造函数,如下所示:

To get the instance of the main form to the "other" form, you can pass it to the constructor of the other form, like this:

public class OtherForm : Form
{
    Main_Form _mainForm;

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

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

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

Finally, when you are creating your "other" form, you will need to pass an instance of the main form, like this:

OtherForm theOtherForm = new OtherForm(this);

这篇关于从单独的表单更改另一个表单的值(解决方案不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆