方法: Windows フォーム ProgressBar コントロールによって表示される値を設定する
重要
ToolStripProgressBar コントロールは、ProgressBar コントロールに代わると共に追加の機能を提供します。ただし、ProgressBar コントロールは、下位互換性を保つ目的および将来使用する目的で保持されます。
.NET Framework では、ProgressBar コントロール内に特定の値を表示するための方法が複数用意されています。 どの方法を選択するかは、実行するタスクや、解決しようとしている問題によって変わってきます。 使用できるアプローチを次の表に示します。
アプローチ | 説明 |
---|---|
ProgressBar コントロールの値を直接設定する。 | この方法は、データ ソースからレコードを読み取る場合など、タスクに関連する測定項目の合計値がわかっている場合に役立ちます。 また、値の設定が 1、2 回しか必要ないような場合も、この方法が簡単です。 最後に、進行状況バーに表示される値を減らす必要がある場合も、このプロセスを使用します。 |
ProgressBar の表示を固定値で増やしていく。 | この方法は、最小値と最大値の間を単純にカウントしていく場合に便利です (経過時間を表示する場合や、既知の合計値の中から処理済みのファイル数を表示する場合など)。 |
ProgressBar の表示を、変化する値で増やしていく。 | この方法は、表示される値をさまざまな数量で複数回変えていく必要がある場合に便利です。 たとえば、一連のファイルをディスクに書き込んでいる間に、ハードディスクの使用済み容量を示すような場合です。 |
進行状況バーに表示される値を設定するための最も直接的な方法は、Value プロパティを設定することです。 これは、デザイン時と実行時のどちらでも行うことができます。
ProgressBar の値を直接設定するには
ProgressBar コントロールの Minimum と Maximum の値を設定します。
コードで、コントロールの 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 コントロールの値を増やすメソッドを呼び出すことができます。 これは、タイマーを表示する場合や、進行状況を全体に対する割合として測定しない場合に役立ちます。
進行状況バーを固定値で増やしていくには
ProgressBar コントロールの Minimum と Maximum の値を設定します。
コントロールの Step プロパティを、進行状況バーの表示値の増加量を表す整数に設定します。
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(); } }
最後に、表示値が毎回異なる数量で増加するようにすることもできます。 これは、さまざまなサイズのファイルをハードディスクに書き込む場合や、進行状況を全体に対する割合として測定する場合など、一連の一意の操作を追跡する場合に便利です。
進行状況バーを動的な値で増やしていくには
ProgressBar コントロールの Minimum と Maximum の値を設定します。
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(); } }
関連項目
.NET Desktop feedback
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示