[英]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.