首页 > 解决方案 > 当进度条结束并更改背景图像时,删除 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;

            }
        }
    }
}

标签: c#forms

解决方案


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();
            }
        }
    }
}

推荐阅读