次の方法で共有


方法: Windows フォームの ProgressBar コントロールによって表示される値を設定する

重要

ToolStripProgressBar コントロールは、ProgressBar コントロールに代わって機能を追加します。ただし、ProgressBar コントロールは、下位互換性と将来の使用の両方で保持されます (選択した場合)。

.NET Framework には、 ProgressBar コントロール内に特定の値を表示するさまざまな方法が用意されています。 選択するアプローチは、手元のタスクまたは解決している問題によって異なります。 次の表に、選択できる方法を示します。

方法 説明
ProgressBar コントロールの値を直接設定します。 この方法は、データ ソースからのレコードの読み取りなど、関連する測定項目の合計を把握しているタスクに役立ちます。 また、値を 1 回または 2 回だけ設定する必要がある場合は、簡単に設定できます。 最後に、進行状況バーに表示される値を小さくする必要がある場合は、このプロセスを使用します。
ProgressBar表示を固定値で増やします。 この方法は、経過時間や既知の合計から処理されたファイルの数など、最小値と最大値の単純なカウントを表示する場合に便利です。
変化する値で ProgressBar 表示を増やします。 この方法は、表示される値を異なる量で何度も変更する必要がある場合に便利です。 たとえば、ディスクに一連のファイルを書き込むときに使用されているハード ディスク領域の量を示します。

進行状況バーに表示される値を設定する最も直接的な方法は、 Value プロパティを設定することです。 これは、デザイン時または実行時に実行できます。

ProgressBar 値を直接設定するには

  1. ProgressBar コントロールのMinimumMaximumの値を設定します。

  2. コードでは、コントロールの Value プロパティを、確立した最小値と最大値の間の整数値に設定します。

    Value プロパティを、Minimum プロパティとMaximum プロパティによって確立された境界の外側に設定すると、ArgumentException例外がスローされます。

    次のコード例は、 ProgressBar 値を直接設定する方法を示しています。 コードは、データ ソースからレコードを読み取り、データ レコードが読み取されるたびに進行状況バーとラベルを更新します。 この例では、フォームにLabel コントロール、ProgressBar コントロール、CustomerRowフィールドとFirstName フィールドを含む LastName という行を含むデータ テーブルが必要です。

    Public Sub CreateNewRecords()
       ' Sets the progress bar's Maximum property to
       ' the total number of records to be created.
       ProgressBar1.Maximum = 20
    
       ' Creates a new record in the dataset.
       ' NOTE: The code below will not compile, it merely
       ' illustrates how the progress bar would be used.
       Dim anyRow As CustomerRow = DatasetName.ExistingTable.NewRow
       anyRow.FirstName = "Stephen"
       anyRow.LastName = "James"
       ExistingTable.Rows.Add(anyRow)
    
       ' Increases the value displayed by the progress bar.
       ProgressBar1.Value += 1
       ' Updates the label to show that a record was read.
       Label1.Text = "Records Read = " & ProgressBar1.Value.ToString()
    End Sub
    
    public void createNewRecords()
    {
       // Sets the progress bar's Maximum property to
       // the total number of records to be created.
       progressBar1.Maximum = 20;
    
       // Creates a new record in the dataset.
       // NOTE: The code below will not compile, it merely
       // illustrates how the progress bar would be used.
       CustomerRow anyRow = DatasetName.ExistingTable.NewRow();
       anyRow.FirstName = "Stephen";
       anyRow.LastName = "James";
       ExistingTable.Rows.Add(anyRow);
    
       // Increases the value displayed by the progress bar.
       progressBar1.Value += 1;
       // Updates the label to show that a record was read.
       label1.Text = "Records Read = " + progressBar1.Value.ToString();
    }
    

    一定の間隔で進行する進行状況を表示する場合は、値を設定し、その間隔で ProgressBar コントロールの値を増やすメソッドを呼び出すことができます。 これは、タイマーやその他のシナリオで、進行状況を全体に対する割合として測定しない場合に役立ちます。

進行状況バーを固定値で大きくするには

  1. ProgressBar コントロールのMinimumMaximumの値を設定します。

  2. コントロールの Step プロパティを、進行状況バーの表示値を増やす量を表す整数に設定します。

  3. PerformStep メソッドを呼び出して、Step プロパティに設定された量で表示される値を変更します。

    次のコード例は、プログレス バーがコピー操作でファイルの数を維持する方法を示しています。

    次の例では、各ファイルがメモリに読み込まれると、進行状況バーとラベルが更新され、読み取られたファイルの合計数が反映されます。 この例では、フォームに Label コントロールと ProgressBar コントロールが必要です。

    Public Sub LoadFiles()
       ' Sets the progress bar's minimum value to a number representing
       ' no operations complete -- in this case, no files read.
       ProgressBar1.Minimum = 0
       ' Sets the progress bar's maximum value to a number representing
       ' all operations complete -- in this case, all five files read.
       ProgressBar1.Maximum = 5
       ' Sets the Step property to amount to increase with each iteration.
       ' In this case, it will increase by one with every file read.
       ProgressBar1.Step = 1
    
       ' Dimensions a counter variable.
       Dim i As Integer
       ' Uses a For...Next loop to iterate through the operations to be
       ' completed. In this case, five files are to be copied into memory,
       ' so the loop will execute 5 times.
       For i = 0 To 4
          ' Insert code to copy a file
          ProgressBar1.PerformStep()
          ' Update the label to show that a file was read.
          Label1.Text = "# of Files Read = " & ProgressBar1.Value.ToString
       Next i
    End Sub
    
    public void loadFiles()
    {
       // Sets the progress bar's minimum value to a number representing
       // no operations complete -- in this case, no files read.
       progressBar1.Minimum = 0;
       // Sets the progress bar's maximum value to a number representing
       // all operations complete -- in this case, all five files read.
       progressBar1.Maximum = 5;
       // Sets the Step property to amount to increase with each iteration.
       // In this case, it will increase by one with every file read.
       progressBar1.Step = 1;
    
       // Uses a for loop to iterate through the operations to be
       // completed. In this case, five files are to be copied into memory,
       // so the loop will execute 5 times.
       for (int i = 0; i <= 4; i++)
       {
          // Inserts code to copy a file
          progressBar1.PerformStep();
          // Updates the label to show that a file was read.
          label1.Text = "# of Files Read = " + progressBar1.Value.ToString();
       }
    }
    

    最後に、進行状況バーに表示される値を増やして、各増加が一意の量になるようにすることができます。 これは、さまざまなサイズのファイルをハード ディスクに書き込んだり、進行状況を全体の割合として測定したりするなど、一連の固有の操作を追跡する場合に便利です。

進捗バーを動的な値で増加させるには

  1. ProgressBar コントロールのMinimumMaximumの値を設定します。

  2. Increment メソッドを呼び出して、指定した整数で表示される値を変更します。

    次のコード例は、コピー操作中に使用されたディスク領域の量を進行状況バーで計算する方法を示しています。

    次の例では、各ファイルがハード ディスクに書き込まれると、使用可能なハード ディスク領域の量を反映するように進行状況バーとラベルが更新されます。 この例では、フォームに Label コントロールと ProgressBar コントロールが必要です。

    Public Sub ReadFiles()
       ' Sets the progress bar's minimum value to a number
       ' representing the hard disk space before the files are read in.
       ' You will most likely have to set this using a system call.
       ' NOTE: The code below is meant to be an example and
       ' will not compile.
       ProgressBar1.Minimum = AvailableDiskSpace()
       ' Sets the progress bar's maximum value to a number
       ' representing the total hard disk space.
       ' You will most likely have to set this using a system call.
       ' NOTE: The code below is meant to be an example
       ' and will not compile.
       ProgressBar1.Maximum = TotalDiskSpace()
    
       ' Dimension a counter variable.
       Dim i As Integer
       ' Uses a For...Next loop to iterate through the operations to be
       ' completed. In this case, five files are to be written to the disk,
       ' so it will execute the loop 5 times.
       For i = 1 To 5
          ' Insert code to read a file into memory and update file size.
          ' Increases the progress bar's value based on the size of
          ' the file currently being written.
          ProgressBar1.Increment(FileSize)
          ' Updates the label to show available drive space.
          Label1.Text = "Current Disk Space Used = " &_
          ProgressBar1.Value.ToString()
       Next i
    End Sub
    
    public void readFiles()
    {
       // Sets the progress bar's minimum value to a number
       // representing the hard disk space before the files are read in.
       // You will most likely have to set this using a system call.
       // NOTE: The code below is meant to be an example and
       // will not compile.
       progressBar1.Minimum = AvailableDiskSpace();
       // Sets the progress bar's maximum value to a number
       // representing the total hard disk space.
       // You will most likely have to set this using a system call.
       // NOTE: The code below is meant to be an example
       // and will not compile.
       progressBar1.Maximum = TotalDiskSpace();
    
       // Uses a for loop to iterate through the operations to be
       // completed. In this case, five files are to be written
       // to the disk, so it will execute the loop 5 times.
       for (int i = 1; i<= 5; i++)
       {
          // Insert code to read a file into memory and update file size.
          // Increases the progress bar's value based on the size of
          // the file currently being written.
          progressBar1.Increment(FileSize);
          // Updates the label to show available drive space.
          label1.Text = "Current Disk Space Used = " + progressBar1.Value.ToString();
       }
    }
    

こちらも参照ください