準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
サンプルコード (C#)
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
button1.Text = i.ToString();
button1.Update();
System.Threading.Thread.Sleep(50);
}
}
}
}
解説
ボタンのテキストに数字をカウントアップしています。カウントアップごとに数字を確実に表示するには、ボタンコントロールを強制的に再描画します。もし、再描画しないと、ループ処理が終了した時点の最後の数字しか表示されず、途中経過が表示されません。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.05.03 Visual Studio 2015 Professional (C# 6.0)
