TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WPF ProgressBar: Value and Maximum

This WPF example uses the ProgressBar control. It sets the Value and accesses the Maximum.

ProgressBar. A ProgressBar graphically displays progress.

As it is updated, by setting its Value property, it displays a colored bar. The bar can become shorter or longer. We set its Foreground, and use its Value and Maximum properties.

Example. To begin, please create a WPF project and drag a ProgressBar to your Window. Now, adjust some of the attributes on the ProgressBar in the XAML. Change the Foreground—we use a fish color ("Salmon") here.

Name: I added the Name attribute to the ProgressBar. I chose the short (but not descriptive) Name of "B".

Name

Next, I added a separate Button control by dragging it to the Window. I specified the "Click" attribute and pressed tab to have Visual Studio create the C# method Button_Click. This method is where the ProgressBar is changed.

Based on:

.NET 4.5

Example markup: XAML

<Window x:Class="WpfApplication27.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="MainWindow" Height="350" Width="525">
    <Grid>
	<ProgressBar HorizontalAlignment="Left"
		     Height="10"
		     Margin="10,10,0,0"
		     VerticalAlignment="Top"
		     Width="100"
		     Name="B"
		     Foreground="Salmon"/>
	<Button Content="Add"
		HorizontalAlignment="Left"
		Margin="115,10,0,0"
		VerticalAlignment="Top"
		Width="75"
		Click="Button_Click"/>
    </Grid>
</Window>

Example code: C#

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication27
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
	public MainWindow()
	{
	    InitializeComponent();
	}

	private void Button_Click(object sender, RoutedEventArgs e)
	{
	    // ... Add 1/5 to the ProgressBar.
	    B.Value += (B.Maximum / 5);

	    // ... See if ProgressBar has reached its max.
	    if (B.Value == B.Maximum)
	    {
		// ... Change button Content.
		Button button = sender as Button;
		button.Content = "DONE";
	    }
	}
    }
}

Changing the ProgressBar. In Button_Click, I increase the Value property of the ProgressBar by one-fifth of its Maximum. So if you click on the Button five times, the ProgressBar is at 100%.

And: When the Value equals the maximum of the ProgressBar with name "B", the Button is changed to display "DONE".

ValueChanged. One useful event handler on the ProgressBar is ValueChanged. This is not shown in the example, but it functions in a similar way to ValueChanged on the Slider control. Please check out that article for an example.

Slider

Summary. In many programs, a ProgressBar is meant to update as a program completes a long-running task. Occasionally, a ProgressBar is used to indicate progress in a sign-up form. This control indicates a linear progression.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf