本文介绍了当我的进度条完全加载时打开一个新表单(窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要多种形式(窗口),即......我的表格中有进度条,还有定时间隔为100的定时器和两个按钮[button1 - > start 和button2 - > 停止],当我点击开始按钮进度条开始加载,,,完全加载后,它必须导航到第二个表单..我曾尝试过一些代码但引发异常[]。 我的代码是什么...... 私人 void timer1_Tick( object sender,EventArgs e) { progressBar1.Maximum = 100 ; progressBar1.PerformStep(); if (progressBar1.Maximum == 100 ) { f2.Show(); --->这里 抛出异常的语句 this .Hide(); } } private void button1_Click( object sender,EventArgs e) { timer1.Enabled = true ; } private void button2_Click( object sender,EventArgs e) { timer1.Enabled = false ; } 所以..任何人都可以引导我走向正确的代码以满足我的要求.... 解决方案 这里有几个问题,首先是这一行: if (progressBar1.Maximum == 100 ) 进度条前进时最大值不会改变,最大值是您设置的静态值,用于确定进度条的最大值(条形图100%满时的值) 。所以这可能永远都是真的。你可能想要: if (progressBar1.Value == 100 ) 接下来是你的表格,我假设这是有效的一次,表单显示,然后单击关闭,它会再次尝试执行此操作。它第二次出现这个错误。因为当你第一次关闭表格时,它确实关闭了。你需要这样的东西: private void timer1_Tick( object sender,EventArgs e) { progressBar1.Maximum = 100 ; progressBar1.PerformStep(); if (progressBar1.Value == 100 ) { timer1.Enabled = false ; // 添加此行 f2 = new Form2(); // 添加此行 f2.Show(); this .Hide(); } } 创建Form2的新实例然后显示它。您可能还需要将timer.Enabled设置为false,以便每次计时器滴答时都不会尝试打开表单(因为在达到100%已满后,每个刻度线上的进度条都会填满)。 / blockquote> 试试这个 在Ron解决方案之上,您也可以试试这个。 Ron解释清晰而整洁...... br $> 私人 void timer1_Tick( object sender,EventArgs e) { progressBar1.PerformStep(); // 或progressBar1.Value ++; if (progressBar1.Value == 100 ) // 检查值 { new Form2()。Show(); this .Hide(); } } private void button1_Click( object sender,EventArgs e) { progressBar1.Maximum = 100 ; progressBar1.Minimum = 0 ; progressBar1.Step = 1 ; timer1.Start(); // 启动计时器 } private void button2_Click( object sender,EventArgs e) { timer1.Stop(); // 停止时间 } I have a requirement of multiple forms(windows) ie...i have progress bar in one of my form and also a timer with timing interval of 100 and two buttons [button1-->start and button2-->stop],when i click the start button progress bar starts loading,,, after completely loading ,,it must navigate to the second form..here i had tried some code but which throws an exception of[].what my code is... private void timer1_Tick(object sender, EventArgs e) { progressBar1.Maximum = 100; progressBar1.PerformStep(); if (progressBar1.Maximum==100) { f2.Show();--->here is the statement which throws an exception this.Hide(); } } private void button1_Click(object sender, EventArgs e) { timer1.Enabled = true; } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; } so.. could any one please guide me towards right code to satisfy my requirement.... 解决方案 You have a couple problems here, first is this line:if (progressBar1.Maximum==100)Maximum doesn't change when the progress bar advances, maximum is a static value you set to determine what the maximum value of the progress bar is (value when the bar is 100% full). So this will probably always be true. You probably want:if (progressBar1.Value == 100)Next is your form, I'm assuming that this works once, the form shows and you click close and it tries to do it again. Its on the second time you get this error. Its because when you closed the form the first time, it really did close. You need something like this:private void timer1_Tick(object sender, EventArgs e){ progressBar1.Maximum = 100; progressBar1.PerformStep(); if (progressBar1.Value == 100) { timer1.Enabled = false; //Add this line f2 = new Form2(); //Add this line f2.Show(); this.Hide(); }}Which creates a new instance of Form2 then shows it. You will probably also want to set your timer.Enabled to false so that it doesn't try to open the form each time the timer ticks (because your progress bar will be full on each tick after it gets to 100% full).Try thisOn Top of Ron Solution, you can try this too.Ron explanation is clear and neat...private void timer1_Tick(object sender, EventArgs e) { progressBar1.PerformStep(); // or progressBar1.Value++; if (progressBar1.Value == 100) // check with the value { new Form2().Show(); this.Hide(); } } private void button1_Click(object sender, EventArgs e) { progressBar1.Maximum = 100; progressBar1.Minimum = 0; progressBar1.Step = 1; timer1.Start(); // start the timer } private void button2_Click(object sender, EventArgs e) { timer1.Stop(); // stop the time } 这篇关于当我的进度条完全加载时打开一个新表单(窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 22:09