c# - 当进度条结束并更改背景图像时,删除 C# 表单对象并显示新对象
问题描述
我正在制作一个程序,一旦进度条过期,它就会更改背景图像,销毁当前表单对象(如进度条)并显示新的表单对象。非常感谢对代码的解释。我正在使用 Visual Studio 2017。代码如下所示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < 100)
{
this.progressBar1.Increment(1);
}
else {
timer1.Stop();
Image myImage = new Bitmap(@"C:\Users\user\source\repos\Form Application\Form Application\res\img1.png");
this.BackgroundImage = myImage;
}
}
}
}
解决方案
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form_Application
{
public partial class Form1 : Form
{
//create new form object
Form1 f = null;
//This is the constructor of the form
public Form1()
{
//intializecomponent is used to construct the design of a form
InitializeComponent();
//ser form1 object to this
f=this;
//it the method to start the timer
this.timer1.Start();
}
//this is the event handler which is called when time of the timer is increased
private void timer1_Tick(object sender, EventArgs e)
{
//check if progress bar value is less than 100
if (progressBar1.Value < 100)
{
//increase the value of progress bar by 1
this.progressBar1.Increment(1);
}
else {
//stop the timer when progress bar value is 100
timer1.Stop();
//get the image from the path
Image myImage = new Bitmap(@"C:\Users\user\source\repos\Form Application\Form Application\res\img1.png");
//set the image to the forms background here this represents the object of the form
this.BackgroundImage = myImage;
//create new form
Form1 formobj = new Form1();
//show new form
formobj.show();
//close current form
f.close();
}
}
}
}
推荐阅读
- kubernetes - 我应该在 OpenShift 中使用 certmanager.k8s.io 或 certmanager.io 哪个?
- javascript - 鼠标单击不适用于 google.maps.places.Autocomplete
- css - 媒体查询上的 CSS 网格布局更新
- .net - EF中如何拦截延迟加载相关实体
- docker - 不再支持 Google Cloud Run UDP 端口?
- c++-cli - 与类型同名的属性 - 我可以摆脱显式命名空间吗?
- regex - 正则表达式,在分组时排除单词之间的一个
- c# - 如何防止异步方法可重入?
- python - Python - if str in df.column 输入 if 语句
- gd - Area2D 函数调用错误随机发生