简体   繁体   English

C#方法更改“图片框”

[英]C# method changing “picturebox”

trying to change an Image in "picturebox". 试图在“图片框”中更改图片。 But won't work. 但是不会起作用。 no errors,warning. 没有错误,警告。

I have 2 forms, one is a "Message box", and one is a main form. 我有2种形式,一种是“消息框”,一种是主要形式。 If I try to change the Image from another method (for example: Form1_load) It works. 如果我尝试通过其他方法(例如:Form1_load)更改Image,则它可以工作。

Form1: Form1中:

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;
using WindowsFormsApp1;
namespace jatek
{
    public partial class Form1 : Form
         public void Eldontesboss(short adat)
         {
            MessageBox.Show("Number:" + adat); //this is appears,but ...
            box.Image = WindowsFormsApp1.Properties.Resources.alap; //this is not work.
         }
    }
}

Form2: 窗体2:

using jatek;
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
    {
       private void button1_Click(object sender, EventArgs e)
        {
            Form1 foo = new Form1();
            foo.Eldontesboss(1);
            this.Close();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form1 foo = new Form1();
            foo.Eldontesboss(2);
            this.Close();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            Form1 foo = new Form1();
            foo.Eldontesboss(3);
            this.Close();
        }   
    }
}

Change the PictureBox image. 更改PictureBox图像。

You're creating instances of Form1 that are never displayed; 您正在创建从未显示的Form1实例; they are simply invisible, in memory. 它们在内存中只是看不见的。 If you want to change the Image in the PictureBox of the CURRENTLY displayed Form1 , then you need a reference to that particular instance. 如果要更改当前显示的Form1的图片框中的图像,则需要对该特定实例的引用。 It is unclear from your description which form is the "Main" form and which one is the "Message" form, but I think that Form1 is the main, and Form2 is the message. 从您的描述中还不清楚哪种形式是“主”形式,哪种形式是“消息”形式,但是我认为Form1是主要形式,而Form2是消息。 Furthermore, I'll assume that Form1 is creating an instance of Form2. 此外,我假设Form1正在创建Form2的实例。 If that is the case, one way to pass a reference is by setting the Owner property when you call Show() , like this: 在这种情况下,传递引用的一种方法是在调用Show()时设置Owner属性,如下所示:

// ... in Form1, when Form2 is created ...
Form2 f2 = new Form2();
f2.Show(this); // pass reference to Form1, into Form2

Now, over in Form2, you can cast the Owner property back to type Form1 and work with it: 现在,在Form2中,您可以将Owner属性Form1Form1类型并使用它:

// ... in Form2, after being displayed with `Show(this)` back in `Form1` ...
private void button1_Click(object sender, EventArgs e)
{
    Form1 foo = (Form1)this.Owner;
    foo.Eldontesboss(1);
    this.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM