Jeffrey W
Jeffrey W

Reputation: 89

C# - Adding an image file from resources in constructor

I made a custom button class in Visual Studio, and I would like to add an image to the constructor. The images are in the Properties/Resources of my project. Normally, I would add an image to my button like this:

btnBack.Image = Properties.Resources.back57;

In this scenario, back57 is the name of the image file.

This is the current constructor of my button class:

public MenuButton()
    {
        this.Font = new System.Drawing.Font("TrsNo__", 18);
        this.Width = 160;
        this.Height = 40;
        this.FlatStyle = FlatStyle.Popup;
        this.BackColor = System.Drawing.Color.DarkOrange;
        this.ImageAlign = System.Drawing.ContentAlignment.TopLeft;
        this.MouseEnter += new System.EventHandler(mouseEnterCustom);
        this.MouseLeave += new System.EventHandler(mouseLeaveCustom);
        this.SetStyle(ControlStyles.Selectable, false);
    }

How to edit the constructor so I can add image files from resources?

Edit 1: I assume it will look something like this.

public MenuButton(??????)
    {
    this.Image = ?????????
    }

Upvotes: 0

Views: 357

Answers (1)

iceberg
iceberg

Reputation: 596

add new cunstructor:

public MenuButton(Bitmap buttonImage)
   : base()
{
   this.Image = buttonImage
}

and create your button:

MenuButton menuBttn = new MenuButton(Properties.Resources.back57);

Upvotes: 2

Related Questions