Yabaz Thampi
Yabaz Thampi

Reputation: 82

How to set Background image in master page using windows form application C#?

I set a background image in the master form then I call in the child form it's not working

I set the background image in the properties of the master form. if I put any other tool like buttons panel it working on the child form

Master Form

public partial class MasterForm: Form
{
    public MasterForm()
    {
        InitializeComponent();
    }
}

Child Form

here I call like this

public partial class item: MasterForm
{
}

Upvotes: 1

Views: 339

Answers (2)

Changemyminds
Changemyminds

Reputation: 1377

  1. First create BaseMasterForm.cs and inherit Form. Write your code if you want added something in constructor.
  2. Second create Child Form.cs and inherit BaseMasterForm. You can override Show Background. If you want to show Background you need to set true.

BaseMasterForm .cs

public class BaseMasterForm : Form
{
    // You can override this Show Background. If you want or not to show background.
    public virtual bool ShowBackground { get { return true; } }

    public BaseMasterForm()
    {
        this.Load += (s, e) =>
        {
            // your image
            this.BackgroundImage = (ShowBackground) ? Properties.Resources.LeeQc : null;
            // you can custom
            this.BackgroundImageLayout = (ShowBackground) ? ImageLayout.Stretch : ImageLayout.None;
        };
    }
}

ChildForm.cs

public partial class ChildForm : BaseMasterForm
{
    // set true or false. to show MasterForm background.
    public override bool ShowBackground { get { return true; } }

    public ChildForm()
    {
        InitializeComponent();
    }
}

Upvotes: 3

programmer444
programmer444

Reputation: 156

I think it's not working because every page has it's own properties. You have to set the image in the MasterForm.

public partial class MasterForm: Form
{
    public MasterForm()
    {
        InitializeComponent();
        this.BackgroundImage = Properties.Resources.Image;
    }
}

See here: Set the same background in all windows forms with a single setting

Upvotes: 0

Related Questions