繁体   English   中英

C#方法更改“图片框”

[英]C# method changing “picturebox”

试图在“图片框”中更改图片。 但是不会起作用。 没有错误,警告。

我有2种形式,一种是“消息框”,一种是主要形式。 如果我尝试通过其他方法(例如:Form1_load)更改Image,则它可以工作。

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

窗体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();
        }   
    }
}

更改PictureBox图像。

您正在创建从未显示的Form1实例; 它们在内存中只是看不见的。 如果要更改当前显示的Form1的图片框中的图像,则需要对该特定实例的引用。 从您的描述中还不清楚哪种形式是“主”形式,哪种形式是“消息”形式,但是我认为Form1是主要形式,而Form2是消息。 此外,我假设Form1正在创建Form2的实例。 在这种情况下,传递引用的一种方法是在调用Show()时设置Owner属性,如下所示:

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

现在,在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