Joe
Joe

Reputation: 447

PictureBox disappears after resize my Form

I have a MainForms.cs with Ribbon, I want to put a transparent PictureBox on the top right of the ribbon (The PictureBox represent my logo).

This is my MainForm with a Logo on the top rigth

This is what I have tried:

Code :

InitializeComponent();
pictureBox1.Parent = ribbon1;

Until here all is working great.

My Problem :

When I resize my Form, the PictureBox disappears.

On the OnPaint fonction i reset all setting like that :

protected override void OnPaint(PaintEventArgs pe)
{
    this.Activate();
    pictureBox1.Visible = true;
    pictureBox1.Show();
    pictureBox1.BringToFront();            
}

But nothing makes the Picturebox appear. Please, can you tell me what i missed.

Upvotes: 2

Views: 1418

Answers (1)

Mark Hall
Mark Hall

Reputation: 54522

I downloaded the DLL that you are using and created a small test example. What I noticed is the Parent property of the PictureBox was set to null. By adding the Parent back to the Picturebox in the OnPaint event I was able to get it working if the size of the Form was growing, but would disappear when the Form size was reduced. When I put the same code in the OnResize EventHandler it works like you would expect.

public partial class Form1 : Form
{
    PictureBox pictureBox1 = new PictureBox();
    public Form1()
    {
        InitializeComponent();

        pictureBox1.Image = Image.FromFile(@"C:\temp\test.jpg");
        pictureBox1.Parent = ribbon1;
        pictureBox1.Location = new Point(this.Width-pictureBox1.Width,10);


    }


    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        if (pictureBox1.Parent == null)
        {
            pictureBox1.Parent = ribbon1;
            pictureBox1.Visible = true;
            pictureBox1.Location = new Point(this.Width - pictureBox1.Width, 10);
        }
    }
}

Upvotes: 2

Related Questions