3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C#】プログレスバーのサンプル(とりあえずメモとして)

Last updated at Posted at 2018-08-03

1.Windowsフォーム Form1.cs、Form2.csを追加する
2.Form1.cs上に適当にbutton1を追加する
3.Form2.cs上にいい感じにprogressBar1を追加する
Form2.png
4.Form2.Designer.csを開いて、private System.Windows.Forms.ProgressBar progressBar1;をpublicに変更する
5.あとは4.も含めてコードを見てちょ


Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ProgressBarTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BackGroundClass1 BGC1 = new BackGroundClass1(this);
            BGC1.start();
        }
    }
}

Form2.Designer.cs

namespace ProgressBarTest
{
    partial class Form2
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        
        #region Windows フォーム デザイナーで生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
        //省略
        }

        #endregion

        //↓privateからpublicに変更する
        //private System.Windows.Forms.ProgressBar progressBar1;
        public System.Windows.Forms.ProgressBar progressBar1;
        

    }
}

BackGroundClass1.cs

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

//using System.Windows.Forms;

namespace ProgressBarTest
{
    class BackGroundClass1
    {
        private Form1 f1;
        private Form2 f2;

        public BackGroundClass1(Form1 f1)
        {
            this.f1 = f1;
            this.f2 = new Form2();
        }

        public void start()
        {
            Thread t = new Thread(new ThreadStart(LongTask));
            t.IsBackground = true;
            t.Start();
            f2.ShowDialog(f1);
            f2.Dispose();
        }

        private void LongTask()
        {
            //ここにすっげー時間かかる処理を書く想定
            for (int i = 0; i <= 100; i++)
            {
                Thread.Sleep(100);
                f2.BeginInvoke(new UpdateProgressBarDelegate(UpdateProgressBar), new object[] { i });
            }
        }

        private delegate void UpdateProgressBarDelegate(int val);
        private void UpdateProgressBar(int val)
        {
            f2.progressBar1.Value = val;
            if (val >= 100)
            {
                f2.Close();
            }
        }
    

    }
}

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?