question

GaniTPT avatar image
0 Votes"
GaniTPT asked GaniTPT commented

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

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..?

dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe you can define a User Control? It is like a panel that contains your child controls, is designed individually having a separate class for your code and can be used multiple times in different forms. User Controls can be easily crated in Visual Studio and then appear in Toolbox like other controls.

0 Votes 0 ·

Thanks for your reply.

But, i already used controls in the win forms. i just want to separate the forms based on my business logic.

0 Votes 0 ·

If you simply want to move some functions because the code of the form becomes too big, then you can create a new class file (.cs), call it ProgressBarFunctions.cs, for example, and use the right namespace and the name of the form:

 // File ‘ProgressBarFunctions.cs’:
    
 namespace MyNamespace1
 {
    public partial class MyForm1
    {
       . . . the data and functions related to progress bar control . . .
    }
 }

Then move here the functions that are related to your progress-bar control. No other changes are required.

But if you want to use the same functions in different forms, then consider other approaches.

1 Vote 1 ·

Sorry for the late response. Thanks for your suggestions.

As per your suggestion, i am planning to use user control. especially for progress bar.
remaining all the controls, i am passing as variable. Not required to maintain all the controls in class file.

So, i decided to use user control (only for progress bar) and pass progress bar user control in class file.

Can you pls. suggest or provide some sample step to create user control in forms and pass it into class file..?

0 Votes 0 ·

IMHO, I don't think what you are doing is an optimal approach.

You could look into using Model View Presenter, which is seperation of duty, and layered architecture implementing separation of concerns.

https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

https://en.wikipedia.org/wiki/Separation_of_concerns

I think what you have stated as an architectural style for the UI and business logic wouldn't be approved in a professional environment.

https://www.codeproject.com/Articles/36847/Three-Layer-Architecture-in-C-NET-2


0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.