I have setup a custom control that acts as a loading overlay, the overlay is a form with a picturebox on it to show the image.
When the overlay shows it goes into position and is infront of the main form calling it, this looks great. However when the user moves the form or resizes it the form then goes behind to main form.
The overlay form moves and resizes without problems when the main form is moving or resizing, however after it has finished resizing or moving the form goes behind the main form. How can I bring the overlay form back to the top without using TopMost?
I call the below code from the main form using
LoadingControl p = new LoadingControl(dataGridView1, this);
p.Show();
And the overlay form:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class LoadingControl : Form
{
private Color BackgroundColour = Color.Black;
private double BackgroundOpacity = 0.50;
private Image LoadingImage = APPNAME.Properties.Resources.loading_120x128;
private Form Mainform;
private Control MainControl;
public LoadingControl(Control parent, Form frm)
{
MainControl = parent;
Mainform = frm;
SetupForm();
Size = parent.ClientSize;
Location = parent.PointToScreen(Point.Empty);
Mainform.Move += AdjustPosition;
MainControl.SizeChanged += AdjustPosition;
}
private void SetupForm()
{
FormBorderStyle = FormBorderStyle.None;
BackColor = BackgroundColour;
Opacity = BackgroundOpacity;//0.50;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
PictureBox pbox = new PictureBox {Image = LoadingImage, Parent = this};
pbox.Width = pbox.Image.Width;
pbox.Height = pbox.Image.Height;
pbox.Left = (Width/2) - (pbox.Width/2);
pbox.Top = (Height/2) - (pbox.Height/2)-10;
pbox.Anchor = AnchorStyles.None;
Controls.Add(pbox);
}
public void SetLoadingImage(Image img)
{
LoadingImage = img;
}
public void SetBackgroundColour(Color col)
{
BackgroundColour = col;
}
public void SetOpacity(double Opa)
{
BackgroundOpacity = Opa;
}
private void AdjustPosition(object sender, EventArgs e)
{
//TopMost = true;
BringToFront();
ClientSize = MainControl.ClientSize;
Location = MainControl.PointToScreen(Point.Empty);
//Mainform.Activate();
//TopMost = false;
BringToFront();
Focus();
//MakeTopMost(this);
}
}