从其他形式更改 Picturebox 的图像

匿名
2024-05-15T06:47:00.8766667+00:00

我想从另一个表单按钮更改图片框的图像。

我制作的程序没有显示错误,但在运行时,它显示错误“对象引用未设置为对象的实例”。

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 SCADA_training
{
    public partial class Form1 : Form
    {
       // private Form2 frm2;
        public Form1()
        {
            InitializeComponent();


        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }



        private void button3_Click(object sender, EventArgs e)
        {
            Form2 A = new Form2();
            A.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

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 SCADA_training
{
    public partial class Form2 : Form
    {
        public Form1 frm1;
        public object pictureBox1 { get; private set; }

        public Form2()
        {
            InitializeComponent();

    }


        public void button2_Click(object sender, EventArgs e)
        {

           frm1.pictureBox1.Image = PROJECT.Properties.Resources._1;


        }

        public void button1_Click(object sender, EventArgs e)
        {


            frm1.pictureBox1.Image = PROJECT.Properties.Resources._2;


        }
    }
}

请帮帮我,我是初学者

Note:此问题总结整理于: Change Image of Picturebox from other form

开发人员技术 | C#
开发人员技术 | C#

一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。

0 个注释 无注释

问题作者接受的答案

Hui Liu-MSFT 48,711 信誉分 Microsoft 外部员工
2024-05-15T13:07:49.9366667+00:00

尝试添加一行:

private void button3_Click(object sender, EventArgs e)
{
   Form2 A = new Form2();
   A.frm1 = this; // added line
   A.Show();
}

如果回复有帮助,请点击“接受答案”并点赞。

注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

此答案是否有帮助?

1 个人认为此答案很有帮助。
0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

提问者可以将答案标记为“已接受”,审查方可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。