标题:请问如何在子窗体中修改主窗体的pictureBox的图片?
只看楼主
wen045
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2021-2-27
结帖率:100%
已结贴  问题点数:20 回复次数:2 
请问如何在子窗体中修改主窗体的pictureBox的图片?
刚学C#,想在想弹出的子窗体中修改主窗体中图片框中的图片,pictureBox1的Modifiers属性也改为public.
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "JPG图片|*.jpg|PNG图片|*.png|BMP图片|*.bmp";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                F_Main f_Main = new F_Main();
                f_Main.pictureBox1.Image = System.Drawing.Image.FromFile(fd.FileName);
              }

就是不知为什么总是不成功。
度娘也查,找不到合格的答案,请各位大侠请赐教,有什么方式可以修改!谢谢!
搜索更多相关主题的帖子: 窗体 修改 pictureBox 图片 子窗体 
2021-08-22 10:46
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:57
专家分:267
注 册:2021-4-14
得分:20 
省事写法
Form1.cs
程序代码:
using System.Drawing;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form1 : Form
    {
        public static PictureBox pb;
        public Form1()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;

            pb = new PictureBox()
            {
                Size = new Size(250, 250),
                Location = new Point(10, 10),
                Image = Image.FromFile("3.png"),
            };
            this.Controls.Add(pb);

            Load += (s, e) => {
                new Form2().Show();
            };
        }
    }
}


Form2.cs
程序代码:
using System.Drawing;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(0, 0);

            Button btn = new Button()
            {
                Text = "change!",
                Location = new Point(10, 10)
            };
            btn.Click += (s, e) =>
            {
                Form1.pb.Image = Image.FromFile("11.gif");
            };
            this.Controls.Add(btn);
        }
    }
}
2021-08-23 08:30
wen045
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2021-2-27
得分:0 
回复 2楼 qq2889577966
谢谢,确实可以!
2021-08-24 09:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506609-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.159958 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved