[英]load form2 after couple of seconds and close form1
我想知道幾秒鍾后如何關閉form1。 它本質上是帶有一些數據的加載形式。 我希望顯示Form1半分鍾,然后我需要關閉Form1並在使用Visual C#打開Windows Form應用程序的地方打開form2。
任何人的任何編碼幫助!!! 請
您可以使用Timer
等待指定的時間。
嘗試這個:
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=30000;
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
this.Hide();
new Form2().Show();
}
您可以使用ApplicationContext類來完成此工作:
public class CustomApplicationContext : ApplicationContext
{
Form mainForm = null;
Timer timer = new Timer();
public CustomApplicationContext(Form mainForm,Form timed):base(timed)
{
this.mainForm = mainForm;
timer.Tick += new EventHandler(SplashTimeUp);
timer.Interval = 30000;
timer.Enabled = true;
}
private void SplashTimeUp(object sender, EventArgs e)
{
timer.Enabled = false;
timer.Dispose();
base.MainForm.Close();
}
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (sender is Form1)
{
base.MainForm = this.mainForm;
base.MainForm.Show();
}
else if (sender is Form2)
{
base.OnMainFormClosed(sender, e);
}
}
}
然后在您的Program.cs中的Main()中:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
CustomApplicationContext app = new CustomApplicationContext(new Form2(),new Form1());
Application.Run(app);
}
這樣,您就不需要Form1中的計時器,只需在指定的時間量內擁有所需的功能,然后它將關閉,然后顯示Form2。
將以下代碼用於啟動屏幕並調用form1。
Form2 _f2;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
_f2 = new Form2();
}
private void Form1_Load(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
_f2.Show();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
}
private void btnStart_Click(object sender, EventArgs e)
{
}
private void btnCancle_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
backgroundWorker1.CancelAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 100; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_f2.Close();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.