Dela via


Så här gör du för att ange värdet som visas av Windows Forms-kontrollen ProgressBar

Viktigt!

Den ToolStripProgressBar kontrollen ersätter och lägger till funktioner i ProgressBar-kontrollen. Den ProgressBar kontrollen behålls dock för både bakåtkompatibilitet och framtida användning, om du väljer det.

.NET Framework ger dig flera olika sätt att visa ett angivet värde i ProgressBar kontroll. Vilken metod du väljer beror på vilken uppgift du har eller vilket problem du löser. I följande tabell visas de metoder som du kan välja.

Tillvägagångssätt Beskrivning
Ange värdet för ProgressBar-kontrollen direkt. Den här metoden är användbar för uppgifter där du känner till den totala mängden av objektet som mäts och som kommer att ingå, till exempel vid läsning av poster från en datakälla. Dessutom, om du bara behöver ange värdet en eller två gånger, är detta ett enkelt sätt att göra det. Använd slutligen den här processen om du behöver minska värdet som visas i förloppsindikatorn.
Öka skärmen ProgressBar med ett fast värde. Den här metoden är användbar när du visar en enkel räkning mellan minimi och maximi, till exempel förfluten tid eller antalet filer som har bearbetats av ett känt totalt antal.
Öka ProgressBar-skärmens visning med ett värde som ändras. Den här metoden är användbar när du behöver ändra det visade värdet ett antal gånger i olika mängder. Ett exempel skulle vara att visa hur mycket hårddiskutrymme som förbrukas när du skriver en serie filer till disken.

Det mest direkta sättet att ange värdet som visas av en förloppsindikator är att ange egenskapen Value. Detta kan göras antingen vid designstadiet eller vid körtid.

Så här ställer du in ProgressBar-värdet direkt

  1. Ange ProgressBar-kontrollens värden för Minimum och Maximum.

  2. I kod anger du kontrollens egenskap Value till ett heltalsvärde mellan de lägsta och högsta värden som du har upprättat.

    Anmärkning

    Om du anger egenskapen Value utanför de gränser som fastställs av egenskaperna Minimum och Maximum, utlöser kontrollen ett ArgumentException undantag.

    I följande kodexempel visas hur du anger ProgressBar-värdet direkt. Koden läser poster från en datakälla och uppdaterar förloppsindikatorn och etiketten varje gång en datapost läss. Det här exemplet kräver att formuläret har en Label kontroll, en ProgressBar kontroll och en datatabell med en rad med namnet CustomerRow med fälten FirstName och 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();
    }
    

    Om du visar förlopp som fortsätter med ett fast intervall kan du ange värdet och sedan anropa en metod som ökar ProgressBar kontrollvärdet med det intervallet. Detta är användbart för timers och andra scenarier där du inte mäter förloppet som en procentandel av helheten.

Öka förloppsindikatorn med ett fast värde

  1. Ange ProgressBar-kontrollens värden för Minimum och Maximum.

  2. Ange kontrollens Step egenskap till ett heltal som representerar beloppet för att öka förloppsindikatorns visade värde.

  3. Anropa metoden PerformStep för att ändra värdet som visas med det belopp som anges i egenskapen Step.

    I följande kodexempel visas hur en förloppsindikator kan upprätthålla antalet filer i en kopieringsåtgärd.

    I följande exempel, när varje fil läss in i minnet, uppdateras förloppsindikatorn och etiketten för att återspegla det totala antalet lästa filer. Det här exemplet kräver att formuläret har en Label kontroll och en ProgressBar kontroll.

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

    Slutligen kan du öka värdet som visas i ett förloppsfält så att varje ökning är en unik mängd. Detta är användbart när du håller reda på en serie unika åtgärder, till exempel att skriva filer av olika storlekar till en hårddisk eller mäta förloppet som en procentandel av helheten.

Öka förloppsindikatorn med ett dynamiskt värde

  1. Ange ProgressBar-kontrollens värden för Minimum och Maximum.

  2. Anropa metoden Increment för att ändra värdet som visas av ett heltal som du anger.

    Följande kodexempel visar hur ett förloppsfält kan beräkna hur mycket diskutrymme som har använts under en kopieringsåtgärd.

    I följande exempel, när varje fil skrivs till hårddisken, uppdateras förloppsindikatorn och etiketten för att återspegla mängden tillgängligt hårddiskutrymme. Det här exemplet kräver att formuläret har en Label kontroll och en ProgressBar kontroll.

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

Se även