How do we maintain or pass the form controls in class file c#

Gani_tpt 1,506 Reputation points
2021-04-02T05:50:25.63+00:00

I am developing the win c# applications and i am using dropdown, textbox,listbox and progress bar controls.

Now, i want to move or transfer all the control values to the class file including progress bar.

Because, i want to maintain all the business logic in the class file. i have repeated task to maintain in the form.

so i decided to move all the control and its values to the class file and doing the process over there.

How to do this with minimal impact..?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2021-04-02T11:50:17.437+00:00

    One method is to

    Setup a class which has properties to represent information on your form which implements INotifyPropertyChanged interface. In the following is an example with a single property, for each property needed repeat what has been done with ProductName.

    public class Product : INotifyPropertyChanged  
    {  
        private string _productName;  
      
        public string ProductName  
        {  
            get => _productName;  
            set  
            {  
                _productName = value;  
                OnPropertyChanged();  
            }  
        }  
      
        public override string ToString() => ProductName;  
      
        public event PropertyChangedEventHandler PropertyChanged;  
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)  
        {  
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
        }  
      
          
    }  
    

    Next, have a class responsible for data operations that has methods such as get all data, get data by id, delete by id, find by id, save by id etc.

    In your form request data from the data operation class, assign to a BindingList which becomes the data source of a BIndingSource which can be used to bind properties to controls while the ProgressBar you can remember via something like this and in the set set the property value for the current item.

    public partial class Form1 : Form, INotifyPropertyChanged  
    {  
        private int _percentDone = 50;  
        public Form1()  
        {  
            InitializeComponent();  
              
            progressBar1.Maximum = 100;  
      
            progressBar1.DataBindings.Add("Value", this, "PercentDone");  
        }  
        public int PercentDone  
        {  
            get => _percentDone;  
            set  
            {  
                _percentDone = value;  
                OnPropertyChanged();  
            }  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)  
        {  
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
        }  
    }  
    

    In some cases you may need to subscribe to the ListChanged event of the BindingList.

    The above may appear like a lot of work and complex but the truth is, it's work and only complex until you take time to understand everything.

    0 comments No comments

0 additional answers

Sort by: Most helpful