我想知道如何在几秒钟之后关闭我的form1。它本质上是一个包含一些数据的加载形式我希望显示form1半分钟,然后我需要关闭form1并打开form2,在那里我使用Visual C#获取我的Windows窗体应用程序。
任何人的任何编码帮助! 请
答案 0 :(得分:0)
您可以使用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();
}
答案 1 :(得分:0)
您可以使用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);
}
}
}
然后在Main()中的Program.cs中:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
CustomApplicationContext app = new CustomApplicationContext(new Form2(),new Form1());
Application.Run(app);
}
有了这个,你不需要Form1中的计时器,只需要你想要的特定时间的功能,并关闭后显示Form2。
答案 2 :(得分:0)
使用以下代码进行初始屏幕,并使用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;
}
}