0

I want to change a picture box from form1 with form2 but keep getting this error ,couldnt understand how to fix it even with all the ppl asking about this error, if anyone could help please. photo of the error

I have tried making it all static but it didnt work.

Form 1

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string solution = "ashe";

        public void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == solution || pictureBox1.Image == Image.FromFile(@"C:\Users\User\Desktop\lol\azir.png"))
            {
                pictureBox1.Image = Image.FromFile(@"C:\Users\User\Desktop\lol\azir.png");

            }

            if (textBox1.Text == solution || pictureBox1.Image == Image.FromFile(@"C:\Users\User\Desktop\lol\sej.png"))
            {
                pictureBox1.Image = Image.FromFile(@"C:\Users\User\Desktop\lol\sej.png");

                var form2 = new Form2();
                form2.Show();
                this.Hide();

            }

            else
            {
                pictureBox1.Image = Image.FromFile(@"C:\Users\User\Desktop\lol\x.jpg");

            }
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(@"C:\Users\User\Desktop\lol\azir_black.png");
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
             

        }
    }

}

Form 2

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 WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        int count = 0;
        
        public void button2_Click(object sender, EventArgs e)
        {
            
            count++;
            switch (count)
            {
                case 1:
                    

                    Form1.pictureBox1.Image = Image.FromFile(@"C:\Users\Ucenik\Desktop\lol\sej_black.png");
                    Form1.solution = "sejuani";

                    var form1 = new Form1();
                    form1.Show();
                    this.Hide();

                    break;


                default:
                    break;
            }
        }
    }
}

.........................................................

Jelena
  • 1
  • 1
  • You need to use an instance of the class and keep form open (and hide). See my two form project : https://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net?force_isolation=true – jdweng Sep 27 '22 at 17:03
  • @jdweng I picked another duplicate (with similar code) because your answer is attached to a completely unrelated question - WebForms are not WinForms and what you provided as answer should not in any way helped OP or resolve the problem ... but they accepted your answer - very confusing Q&A. – Alexei Levenkov Sep 27 '22 at 17:13
  • My sample code in the link is WinForm. The question title says ASPN – jdweng Sep 27 '22 at 17:19
  • The following may be helpful: https://stackoverflow.com/questions/69711574/send-data-within-child-forms/69743297 – Tu deschizi eu inchid Sep 27 '22 at 20:40

0 Answers0