Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:51:16+00:00 2026-05-24T03:51:16+00:00

I have a class with INotifyPropertyChanged interface. There is a property with the name

  • 0

I have a class with INotifyPropertyChanged interface. There is a property with the name Total Progress.

I have a Form with Progress Bar on it. I want to send the TotalProgress property changed notifications to this Progress Bar and set it’s value.

Do I need to catch the PropertyChangedEvent in the Form also?

Edit: WPF Form Code

using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System;
using System.Windows.Threading;

namespace SUpdater
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BackgroundWorker bw = new BackgroundWorker();
        DownloadFile FileDownloadClass = new DownloadFile();

        public MainWindow()
        {
            InitializeComponent();

            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

            progressBar1.SetBinding(System.Windows.Controls.ProgressBar.ValueProperty, new Binding("TotalPercentCompleted"));
            progressBar1.DataContext = FileDownloadClass;

            FileDownloadClass.PropertyChanged +=new PropertyChangedEventHandler(FileDownloadClass_PropertyChanged);
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            FileDownloadClass.DownloadFiles();

            if ((bw.CancellationPending == true))
                e.Cancel = true;
            else
            {
                bw.ReportProgress(FileDownloadClass.TotalPercentCompleted);
            }

        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                this.lblConnectionStatus.Content = " Download Canceled!";
            }

            else if (!(e.Error == null))
            {
                this.lblConnectionStatus.Content = ("Error: " + e.Error.Message);
            }

            else
            {
                this.lblConnectionStatus.Content = "Done!";
            }
        }

        private void FileDownloadClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }

        private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            lblKbCompleted.Content = e.ProgressPercentage.ToString();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bw.RunWorkerAsync();
        }

    }
}

Edit: DownloadFile Class Code

    sealed class DownloadFile:INotifyPropertyChanged
    {

        #region Private Fields
            // These fields hold the values for the public properties.
            private int progressBarValue = 0;
            private int totalKbCompleted = 0;
            private int totalBytesReceived = 0;
            private int remoteFileSize = 0;

            private string fileName = String.Empty;
            private string statusMessage = String.Empty;
        #endregion

            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }

            #region Public Properties
            public int TotalKbCompleted
            {
                get { return this.totalKbCompleted; }

                set
                {
                    if (value != this.totalKbCompleted)
                    {
                        this.totalKbCompleted = value/1024;
                        NotifyPropertyChanged("TotalKbCompleted");
                    }
                }                
            }

            public int TotalBytesReceived
            {
                get { return this.totalBytesReceived; }

                set
                {
                    if (value != this.totalBytesReceived)
                    {
                        this.totalBytesReceived = value;
                        NotifyPropertyChanged("TotalBytesReceived");
                    }
                }
            }

            public int RemoteFileSize
            {
                get { return this.remoteFileSize; }

                set
                {
                    if (value != this.remoteFileSize)
                    {
                        this.remoteFileSize = value;
                        NotifyPropertyChanged("RemoteFileSize");
                    }
                }
            }

            public string CurrentFileName
            {
                get { return this.fileName; }

                set
                {
                    if (value != this.fileName)
                    {
                        this.fileName = value;
                        NotifyPropertyChanged("CurrentFileName");
                    }
                }
            }

            public string StatusMessage
            {
                get { return this.statusMessage; }

                set
                {
                    if (value != this.statusMessage)
                    {
                        this.statusMessage = value;
                        NotifyPropertyChanged("StatusMessage");
                    }
                }
            }
            #endregion

        public Int16 DownloadFiles()
        {
            try
            {

                statusMessage = "Attempting Connection with Server";

                DoEvents();
                // create a new ftpclient object with the host and port number to use
                FtpClient ftp = new FtpClient("mySite", 21);

                // registered an event hook for the transfer complete event so we get an update when the transfer is over
                //ftp.TransferComplete += new EventHandler<TransferCompleteEventArgs>(ftp_TransferComplete);

                // open a connection to the ftp server with a username and password
                statusMessage = "Connected. Authenticating ....";
                ftp.Open("User Name", "Password");

                // Determine File Size of the compressed file to download
                statusMessage = "Getting File Details";
                RemoteFileSize = Convert.ToInt32(ftp.GetFileSize("myFile.exe"));

                ftp.TransferProgress += new EventHandler<TransferProgressEventArgs>(ftp_TransferProgress);
                statusMessage = "Download from Server";
                ftp.GetFile("myFile.exe", "E:\\Test\\myFile.exe", FileAction.Create);

                // close the ftp connection
                ftp.Close();
                statusMessage = "Download Complete";
                return 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return 0;
            }

        }


        private void ftp_TransferProgress(object sender, TransferProgressEventArgs e)
        {
            totalBytesReceived = Convert.ToInt32(e.BytesTransferred.ToString());
            totalKbCompleted = Convert.ToInt32(totalKbCompleted + Convert.ToInt32(totalBytesReceived));
            progressBarValue = totalKbCompleted;
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:51:18+00:00Added an answer on May 24, 2026 at 3:51 am

    You can use control binding:

    Windows Forms:

    progressBar1.DataBindings.Add("Value", dataSource, dataMember, true,
        DataSourceUpdateMode.OnPropertyChanged);
    

    where the dataSource is your class. and the dataMember is the property name in that class “TotalProgress“.

    Edit: For WPF

    progressBar1.SetBinding(ProgressBar.ValueProperty, new Binding("ProgressTotal"));
    progressBar1.DataContext = the instance of the class you want to bind to its property;
    

    For more information about wpf data binding check this and this.

    Edit2: Here is an full example:

    Foo _foo = new Foo();
    
    DispatcherTimer _dispatcherTimer = new DispatcherTimer();
    
    public MainWindow()
    {
        InitializeComponent();
    
        _dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
        _dispatcherTimer.Tick += _dispatcherTimer_Tick;
        _dispatcherTimer.Start();
    
        progressBar1.SetBinding(ProgressBar.ValueProperty, new Binding("ProgressTotal"));
        progressBar1.DataContext = _foo;
    }
    
    private void _dispatcherTimer_Tick(object sender, EventArgs e)
    {
        _foo.ProgressTotal = (_foo.ProgressTotal + 10) % progressBar1.Maximum;
    }
    
    
    public class Foo : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private double _progressTotal = 0;
    
        public double ProgressTotal
        {
            get { return _progressTotal; }
            set 
            {
                if (value != _progressTotal)
                {
                    _progressTotal = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("ProgressTotal"));
                    }
                }
            }
    
        }
    }
    

    Edit: Add a timer to preview the effect.

    Edit: After you uploading your code, the problem appears in two positions:

    1. The name of the variable is TotalKbCompleted no TotalPercentCompleted. so change the binding line to:

      progressBar1.SetBinding(System.Windows.Controls.ProgressBar.ValueProperty, new Binding("TotalKbCompleted"));
      
    2. You are updating the totalKbCompleted instead of TotalKbCompleted so the property changed will not trigger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Bar that implements INotifyPropertyChanged. When setting the property CurrentFoo, I
I have a class that implements the INotifyPropertyChanged interface. Some of the properties of
I have this class: public class UploadFile : INotifyPropertyChanged { private string name; public
I have a need to use the INotifyPropertyChanged interface on a class. This is
I have a ClassA with an ObservableCollection property, that implements the INotifyPropertyChanged interface on
I have a ViewModelBase class where I define RaisePropertyChanged method for the INotifyPropertyChanged interface.
Let's say I have a base class that implements the INotifyPropertyChanged interface and I
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.