C # in the use of the Progress class

xiaoxiao2021-03-06  44

Visual Studio

Set the value displayed by the Windows Form ProgressBar control

.NET Framework allows you to display a given value in a PROGRESSBAR control using a variety of different methods. The choice will depend on the task you want to perform or the problem to be resolved:

Set the value of the ProgressBar control directly. If you know the total number of measurements involved in the task, this method is very useful, such as reading records from the data source. Also, if you only need to set this value once or twice, this will be a simple way to implement this. Finally, if you need to reduce the value displayed by the schedule, use this method. The display value of ProgressBar is incremented by fixed value. This method is very useful if you need to display a simple count between the minimum and the maximum, such as the number of files that have been processed in the total number of runtime or known files. Make the display value of ProgressBar by variable value. This method is very useful if you need to change the display value multiple times. For example, display the amount of hard disk occupied when writing a series of files to disk.

Set the ProgressBAR value directly

The most direct way to set the value displayed by the progress bar is to set the value attribute. This can be done when designing or runtime.

Set the ProgressBAR value directly

Set the minimum and maximum values ​​of the ProgressBar control. In the code, set the value of the control to the integer value between the set minimum and the maximum value.

Note if

The value of the Value property setting is exceeded

Minimum and

The Maximum property determines the boundary, the control will be triggered

ARGUMENTEXCEPTION is abnormal.

The following example explains how to set the ProgressBAR value directly. This code reads the record from the data source and updates the progress bar and label at each read data record. This presentation assumes that your form has a Label control, a ProgressBar control, and a data table that is named Customerrow with the firstname and the Last Name field. 'Visual Basic

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)

'Increas the value displayed by the program bar.

ProgressBar1.Value = 1

'Updates the lael to show what a replaord was read.

Label1.text = "Records Read =" & progressbar1.value.tostring ()

End Sub

// C #

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 Melely

// Illustrates How The Progress Bar Would Be Used.

Customerrow Anyrow = DataSetName.existingTingTable.NewRow ();

Anyrow.firstname = "stephen";

AnyRow.lastname = "james";

EXInshipTable.Rows.Add (AnyRow);

// increas the value displayed by the program bar.

ProgressBar1.Value = 1;

// Updates the lael to show That A Record Was Read.

Label1.text = "Records Read =" ProgressBar1.Value.toString ();

}

Make the ProgressBAR value to increase the fixed time interval

If you want to display the progress of the fixed time interval, you can set this value, then call the method so that the value of the ProgressBar control is incremented by this time interval. This is very useful for timers and other scenarios that you cannot measure the progress of the overall percentage.

Increment the progress bar by fixed value

Set the minimum and maximum values ​​of the ProgressBar control. Set the STEP property of the control to an integer that represents the number of display values ​​in the progress bar. Call the PerformStep method so that the display value is changed in the number of STEP properties. The following example explains how the progress bar maintains the file count in the replication operation. In the following example, when each file is read into memory, the progress bar and tag will be updated to reflect the total number of files read. This example assumes that your form has a Label control and a ProgressBar control. 'Visual Basic

Public Sub loadingFiles ()

'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 stepproperty to one ity.

'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 throughte the Operations to be' Completed. In this case, Five Files Are to be copied into memory,

'So the loop will execute 5 time.

For i = 0 TO 4

'INSERT CODE to COPY A FILE

Progressbar1.performstep ()

'Update the lael to show what a file.

Label1.text = "# of files read =" & progressbar1.value.tostring

Next I

End Sub

// C #

Public void loadingfiles ()

{

// 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 stepproperty to amount to increase with each ity.

// 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 lael to show what a file.

Label1.text = "# of files read =" progressbar1.value.toString ();

}

}

Make the progressBAR value to increment by variable number

Finally, you can make the number of display values ​​in the progress bar each time the amount is unique. This is very useful when you record a series of unique operations, such as writing different sizes of files to a hard drive, or measurement as a percentage of overall.

Make the progress bar to increment according to the dynamic value

Set the minimum and maximum values ​​of the ProgressBar control. Call the Increment method so that the display value changes according to the specified integer. The following example explains how the progress bar calculates the amount of disk space that has been used during replication operation. For more information on determining the currently available disk space, see Code: Find available disk space (Visual Basic). In the following example, when each file is written to a hard drive, the progress bar and tag will be updated to reflect the amount of hard disk available. This example assumes that your form has a Label control and a ProgressBar control. 'Visual BasicPublic Sub ReadFiles ()

'Sets the progress bar's minimum value to a number

'Repesenting 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

'Repesenting 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 TOET A File INTO MEMORY AND UPDATE FILE SIZE.

'Increas the progress bar's value based on the size of

'The File Currently Being Written.

Progressbar1.increment (filesis)

'Updates the lael to show available drive.

Label1.text = "Current Disk Space Used =" & _

ProgressBar1.Value.toString ()

Next I

End Sub

// C #

Public void readfiles ()

{

// sets the progress Bar's Minimum Value to a Number

// Repesenting The Hard Disk Space Before The Files Are Read In.// You Will Most Likey 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 Likey 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 docute the loop 5 Times.

For (INT i = 1; i <= 5; i )

{

// INSERT CODE TO READ A File INTO Memory and Update File Size.

// increas The Progress Bar's Value Based on The size of

// The file currently beingwritten.

Progressbar1.increment (filesis);

// Updates the lael to show available driving.

Label1.text = "current disk space used =" progressbar1.value.tostring ();

}

}

See

转载请注明原文地址:https://www.9cbs.com/read-73811.html

New Post(0)