How to make the class methods more generic?

eshesh michael 60 Reputation points
2023-12-20T02:00:41.4866667+00:00

what I want to do is instead in each method to use the same variables controls over again to make something more generic and easier to use.

for example this method I'm using the success flag and change all the variables to be enabled true or false.

public void SetControlsAfterProcessing(bool success)
        {
            // Set controls' states after processing (whether it was successful or canceled)
            btnVideoFileFolder.Enabled = success;
            btnSaveLightnings.Enabled = success;
            btnBrowseLightningsFolder.Enabled = success;
            checkBoxSaveLogger.Enabled = success;
            comboBoxLightnings.Enabled = success;
            lightningsSelectionTrackBar.Enabled = success;
            imagesSelectionTrackBar.Enabled = success;
            // ... set other controls
    
            // Update processing state
            IsProcessing = false;
            UpdateButtonText();
        }

but I'm also looking for something to make that I will be also able to set each variable individual.

for example this will be true:

btnVideoFileFolder.Enabled = true;

and this will be false:

btnSaveLightnings.Enabled = false;

but instead typing all the variables over and over again in each method to do something more general and generic.

using Test;
using System.Threading.Tasks;
using System.Windows.Forms;
using XComponent.SliderBar;

public class ControlsStateManager
{
    private readonly Button btnStartProcessing;
    private readonly Button btnViewLogger;
    private readonly CheckBox checkBoxSaveLogger;
    private readonly Button btnVideoFileFolder;
    private readonly Button btnSaveLightnings;
    private readonly Button btnBrowseLightningsFolder;
    private readonly ComboBox comboBoxLightnings;
    private readonly MACTrackBar lightningsSelectionTrackBar;
    private readonly MACTrackBar imagesSelectionTrackBar;
    private readonly CustomProgressBar customProgressBar;
    private readonly PictureBox lightningImagePictureBox;
    // ... add other controls

    // Introduce a processing state variable
    public bool IsProcessing { get; private set; } = false;

    private readonly ProcessingManager processingManager;

    public ControlsStateManager(Button btnStartProcessing,
        Button btnViewLogger,
        CheckBox checkBoxSaveLogger,
        Button btnVideoFileFolder,
        Button btnSaveLightnings,
        Button btnBrowseLightningsFolder,
        ProcessingManager processingManager, ComboBox comboBoxLightnings,
        MACTrackBar lightningsSelectionTrackBar,
        MACTrackBar imagesSelectionTrackBar,
        PictureBox lightningImagePictureBox,
        CustomProgressBar customProgressBar)
    {
        this.btnStartProcessing = btnStartProcessing;
        this.btnViewLogger = btnViewLogger;
        this.checkBoxSaveLogger = checkBoxSaveLogger;
        this.btnVideoFileFolder = btnVideoFileFolder;
        this.btnSaveLightnings = btnSaveLightnings;
        this.btnBrowseLightningsFolder = btnBrowseLightningsFolder;
        this.processingManager = processingManager;
        this.comboBoxLightnings = comboBoxLightnings;
        this.lightningsSelectionTrackBar = lightningsSelectionTrackBar;
        this.imagesSelectionTrackBar = imagesSelectionTrackBar;
        this.lightningImagePictureBox = lightningImagePictureBox;
        this.customProgressBar = customProgressBar;
        // ... initialize other controls
    }

    public void InitializeControls(bool loggerFileExists)
    {
        // Initialize controls' states in the constructor or load event
        btnViewLogger.Enabled = loggerFileExists;
        // ... initialize other controls
    }

    public void SetControlsForProcessing()
    {
        // Set controls' states during processing
        btnVideoFileFolder.Enabled = false;
        btnSaveLightnings.Enabled = false;
        btnBrowseLightningsFolder.Enabled = false;
        checkBoxSaveLogger.Enabled = false;
        comboBoxLightnings.Enabled = false;
        lightningsSelectionTrackBar.Enabled = false;
        imagesSelectionTrackBar.Enabled = false;
        // ... set other controls

        // Update processing state
        IsProcessing = true;
        UpdateButtonText();
    }

    public void SetControlsAfterProcessing(bool success)
    {
        // Set controls' states after processing (whether it was successful or canceled)
        btnVideoFileFolder.Enabled = success;
        btnSaveLightnings.Enabled = success;
        btnBrowseLightningsFolder.Enabled = success;
        checkBoxSaveLogger.Enabled = success;
        comboBoxLightnings.Enabled = success;
        lightningsSelectionTrackBar.Enabled = success;
        imagesSelectionTrackBar.Enabled = success;
        // ... set other controls

        // Update processing state
        IsProcessing = false;
        UpdateButtonText();
    }

    // Add other methods as needed

    // For example, if you want to add a method to reset all controls, you can do:
    public void ResetAllControls()
    {
        customProgressBar.UpdateProgress(0);
        lightningImagePictureBox.Image = null;

        // ... reset other controls
    }

    // Helper method to update button text based on processing state
    private void UpdateButtonText()
    {
        btnStartProcessing.Text = IsProcessing ? "STOP PROCESSING" : "START PROCESSING";
    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,927 questions
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.
11,474 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-12-20T02:50:31.54+00:00

    Hi @eshesh michael , Welcome to Microsoft Q&A,

    Are you referring to creating a general generic method?

    For example:

    // Generic method to set the enabled state of controls
        private void SetControlsEnabledState(bool enabled)
        {
            btnVideoFileFolder.Enabled = enabled;
            btnSaveLightnings.Enabled = enabled;
            btnBrowseLightningsFolder.Enabled = enabled;
            checkBoxSaveLogger.Enabled = enabled;
            comboBoxLightnings.Enabled = enabled;
            lightningsSelectionTrackBar.Enabled = enabled;
            imagesSelectionTrackBar.Enabled = enabled;
            // ... set other controls
        }
    

    When you need to use it, you can call this method directly.

    public void SetControlsForProcessing()
    {
        // Set controls' states during processing
        SetControlsEnabledState(false);
    
        // Update processing state
        IsProcessing = true;
        UpdateButtonText();
    }
    
    public void SetControlsAfterProcessing(bool success)
    {
        // Set controls' states after processing (whether it was successful or canceled)
        SetControlsEnabledState(success);
    
        // Update processing state
        IsProcessing = false;
        UpdateButtonText();
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.