Reputation: 1196
I have a SplashScreen
, MainForm
.
On my MainForm_Load
I have a method named Connect();
. This methods makes a verification of the connection of my RFID device with the SerialPort and it takes a few seconds to finish.
While it goes through the Connect()
method, I want to show my SplashScreen
. I tried this:
private void Main_Load(object sender, EventArgs e)
{
Frm_Splash s = new Frm_Splash();
s.Show();
Connect();
}
The Connect();
method, shows a message using MessageBox
.
But when the SplashScreen
is over, it cloeses itself AND closes the MessageBox aswell.
Here's my SplashScreen
form code:
private void timer1_Tick(object sender, EventArgs e)
{
if (pbLoad.Value < 100)
{
pbLoad.Value = pbLoad.Value + 1;
}
else
{
timer1.Enabled = false;
this.Close();
}
}
I know it has something to do with the this.Close();
. I just don't know how to fix it.
Maybe if I use this.Visible = false
, but then the SplashScreen
would not close, it would still be processing, just would be invisible... I think there is a better option.
Upvotes: 0
Views: 223
Reputation: 43300
I imagine that you are opening your messagebox using MessageBox.Show()
...
Instead of this, use MessageBox.Show(this,"message");
I imagine what is happening is your messageboxes parent is set to the splash screen as that is the dialog with focus
Upvotes: 4
Reputation: 11025
Here's my splash screen:
namespace MyNamespace
{
public partial class frmSplashScreen : Form
{
private static frmSplashScreen splashScreen = null;
private static Thread splashThread = null;
private Double opacityInc = .03;
private Double opacityDec = .1;
private const Int32 iTimerInterval = 30;
public frmSplashScreen()
{
InitializeComponent();
Opacity = .0;
timer1.Interval = iTimerInterval;
timer1.Start();
}
private void frmSplashScreen_Load(Object sender, EventArgs e)
{
CenterToScreen();
}
public static void ShowSplashScreen()
{
if (splashScreen != null)
return;
splashThread = new Thread(new ThreadStart(frmSplashScreen.ShowForm));
splashThread.IsBackground = true;
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();
}
private static void ShowForm()
{
splashScreen = new frmSplashScreen();
Application.Run(splashScreen);
}
public static void CloseForm()
{
if (splashScreen != null)
splashScreen.opacityInc = -splashScreen.opacityDec;
splashThread = null;
splashScreen = null;
}
private void timer1_Tick(Object sender, EventArgs e)
{
if (opacityInc > 0)
{
if (Opacity < 1)
Opacity += opacityInc;
}
else
{
if (Opacity > 0)
Opacity += opacityInc;
else
Close();
}
}
}
}
I activate it by calling this in the constructor of the Form where I want it to pop-up from:
frmSplashScreen.ShowSplashScreen();
Then close it in Shown
of the same Form:
frmSplashScreen.CloseForm();
NOTE: I offer this because it employs static
classes, and that helps to alleviate the ownership issues.
Upvotes: 1