简体   繁体   English

如何从另一个窗体更改 PictureBox 图像

[英]How to change a PictureBox image from another Form

I want to open form2 while form1 loads.我想在form1加载时打开form2 Also, I want to change the Image of a PictureBox in form2 when an action is triggered in form1 .另外,我想在form1中触发动作时更改form2中 PictureBox 的图像。

To open form2 while form1 loads I used this code:要在加载form1时打开form2 ,我使用了以下代码:

private void MONITOR3_Load(object sender, EventArgs e)
{
    MONITOR4 mo4 = new MONITOR4(this);
    mo4.Show();
}

To change the Image of the PictureBox in form2 I used this code, which is must to be run after a condition is met:要更改form2中 PictureBox 的图像,我使用了这段代码,它必须在满足条件后运行:

if (textBox1.Text == @"QUEBEC - ALPHA - TANGO - ALPHA - ROMEO - ")
{
    MONITOR4 mo4 = new MONITOR4(this);
    mo4.pictureBox1.Image = Image.FromFile("D:/QResources/images/HIA.jpg");
}

There are two problems with your current code:您当前的代码有两个问题:

  • You don't have to create a new Form instance each time you need to set some of its properties: store a reference to this Form and use this reference to call public properties or methods of the Form.您不必每次需要设置其某些属性时都创建一个新的 Form 实例:存储对此 Form 的引用并使用此引用调用 Form 的公共属性或方法。
  • You are trying to directly access a child Control's properties of another Form.您正在尝试直接访问另一个窗体的子控件的属性。 Event though you can define a child Control public , you shouldn't and it's not necessary.事件虽然您可以定义一个子控件public ,但您不应该也没有必要。 A Form is a class like any other in this aspect: create a public method on the Form that provides means to modify a private property, without directly exposing a Control's property to direct access.在这方面,Form 是一个类似于任何其他类的类:在 Form 上创建一个公共方法,该方法提供修改私有属性的方法,而无需直接将 Control 的属性暴露给直接访问。
    It's simple, safer and more portable : if a Control needs to be modified (the name is changed, the type of Control is changed etc.), you don't need to go on a hunt to find where the old name/properties have been used in other classes.它简单、更安全、更便携:如果需要修改控件(名称更改、控件类型更改等),您无需继续寻找旧名称/属性的位置在其他类中使用过。
    The public method will be the same and it's the only responsible to reference the current names, properties of the affected Control.公共方法将是相同的,它唯一负责引用受影响控件的当前名称和属性。 A single place where the code, eventually, needs to be modified.最终需要修改代码的一个地方。 You could also use a public event or implement INotifyPropertyChanged to notify the subscribers that some properties have changed.您还可以使用公共事件或实施INotifyPropertyChanged来通知订阅者某些属性已更改。

Here, I'm creating a reference to Monitor4 in the Monitor3 Form:在这里,我在Monitor3表单中创建了对Monitor4的引用:

Monitor4 mo4 = null;

This reference will be use to call a public method ( UpdatePictureBox ) of Monitor4 .该引用将用于调用Monitor4的公共方法 ( UpdatePictureBox )。

Monitor3 Form: Monitor3形式:
(I'm using the TextChanged event of a TextBox to select the Image to show in the Monitor4 PictureBox. Of course, it could be the Validate event or anything else that agrees with your design) (我正在使用 TextBox 的TextChanged事件来选择要在Monitor4 PictureBox 中显示的图像。当然,它可以是Validate事件或与您的设计一致的任何其他事件)

public partial class Monitor3 : Form
{
    Monitor4 mo4 = null;

    private void Monitor3_Load(object sender, EventArgs e)
    {
        mo4 = new Monitor4();
        //Show Monitor4 on the right side of this Form
        mo4.Location = new Point(this.Right + 10, this.Top);
        mo4.Show(this);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string newText = textBox1.Text;
        switch (newText) {
            case "[Some text 1]":
                mo4.UpdatePictureBox(@"[Images1 Path]");
                break;
            case "QUEBEC - ALPHA - TANGO - ALPHA - ROMEO - ":
                mo4.UpdatePictureBox(@"[Images2 Path]");
                break;
            case "[Some text 3]":
                mo4.UpdatePictureBox(@"[Images3 Path]");
                break;
        }
    }
}

Monitor4 Form: Monitor4表格:

public partial class Monitor4 : Form
{
    public void UpdatePictureBox(string imagePath)
    {
        if (File.Exists(imagePath)) {
            pictureBox1.Image?.Dispose();
            pictureBox1.Image = Image.FromFile(imagePath, true);
        }
    }
}

Sample result:示例结果:

从另一个类访问表单的控件

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

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