【问题标题】:Winforms c# Changing background Image of Parent form from popup formWinforms c#从弹出表单更改父表单的背景图像
【发布时间】:2015-11-05 12:35:29
【问题描述】:

我有两种形式 a 和 b。表单 a 是主表单,在其中一个按钮单击事件表单 b 上显示为一个小窗口。在弹出窗口(表单 b)上,我有一个选择图像的选项。我需要做的是,当我点击弹出(表单 b)的保存按钮时,表单(主表单)的背景图像应设置为通过弹出表单(b)选择的图像。

我尝试了以下代码,但 this.Parent、this.Owner 都为弹出表单 (b) 返回 null。我已将表格 a 指定为 MDI。

this.Owner.BackgroundImage = pictureBoxBackground.Image;
this.Parent.BackgroundImage = pictureBoxBackground.Image;

【问题讨论】:

    标签: c# winforms popup background-image mdiparent


    【解决方案1】:

    我要做的是在表单 b 中拥有一个像这样的公共 Image 属性:

    private Image image;
    public Image SelectedImage
    {
        get
        {
            return image;
        }
    }
    

    然后我会添加一个 button_Click 事件(或任何你用来确认选择的事件)。此事件将关闭表单并设置返回图像。

    private void Button_Click(object sender, EventArgs e)
    {
        image = [Whatever Image variable that you want to return];
        Close();
    }
    

    使 FormB 看起来像这样。

    public class FormB : Form
    {
        //[...]Stuff
        private Image image;
        public Image SelectedImage
        {
            get
            {
                return image;
            }
        }
        private void Button_Click(object sender, EventArgs e)
        {
            image = [Whatever Image variable that you want to return];
            Close();
        }
    }
    

    最后,将其用于 FormA 的背景图像。只需执行以下步骤。

    public void ChangeBackground()
    {
        FormB b = new FormB();
        b.ShowDialog();
        this.BackgroundImage = b.SelectedImage;
    }
    

    【讨论】:

    • 不错,但OP没有具体说明表单B作为模式对话框打开,并且按钮应该关闭表单。即使是这种情况,良好的做法也需要在执行一些发布操作之前检查 ShowDialog 结果是否确定。
    • 我知道,我只是认为我会留下模棱两可的空间,因为这个人没有留下太多关于他们的具体代码和使用的细节。
    【解决方案2】:

    如果您希望弹出窗口作为对话框弹出并返回图像,您可以将图像放置在属性中。示例:

    public class Parent : Form
    {
         var popup = new Popup();
         var result = popup.ShowDialog();
         if(result == DialogResult.OK)
         {
            this.BackgroundImage = popup.Image;
         }
    }
    
    public class Popup : Form
    {
        private void SelectImage()
        {
            Image = pictureBoxBackground.Image;
        }
    
        public string Image {get;set;}
    }
    

    如果您希望弹出窗口作为常规窗口打开并能够同时使用父窗口,则需要使用事件,更多信息here

    public class Parent : Form
    {
         var popup = new Popup();
         popup.BackgroundImageChanged += (sender, args) => this.BackgroundImage = args.Image;
         //...
    }
    
    public class Popup : Form
    {
        public event EventHandler<ImageChangedEventArgs> BackgroundImageChanged;
    
        private void SelectImage()
        {
            // ...
            OnBackgroundImageChanged(pictureBoxBackground.Image);
        }
    
        private void OnBackgroundImageChanged(string image)
        {
            if(BackgroundImageChanged != null)
            {
                var e = new ImageChangedEventArgs(image);
                BackgroundImageChanged(this, e);
            }
        }
    }
    
    public class ImageChangedEventArgs : EventArgs
    {
        public ImageChangedEventArgs(string image)   
        {
            Image = image;
        }
        public string Image { get; private set; }
    }
    

    【讨论】:

    • 和我的疑似相似,嗯。
    • 哈哈,是的,假设表单是模态的,这是显而易见的方法:) 不够快
    • 谢谢@Rariolu & frich,这成功了。我只能将一个标记为答案,所以标记最先出现的那个:)
    猜你喜欢
    • 2021-02-25
    • 2020-11-13
    • 1970-01-01
    • 2016-03-16
    • 2015-06-10
    • 2010-11-08
    • 2012-05-25
    • 2021-11-05
    • 2014-09-06
    相关资源
    最近更新 更多