Home > Software engineering >  WFA - Button Enable
WFA - Button Enable

Time:07-19

in Windows Forms App project, I'm trying to set the enable property of the button control with a custom class - ButtonEnable. The class is static and has one static void method. I am using the method in Form Load event where I try to set the buttons according to my needs, although none of the buttons get changed. Any idea why?

public static class ButtonEnable {

    public static void ButtonSet(bool addButton, bool ediButton, bool saveButton, bool 
    deleteButton, bool cancelButton)
    {
        Form1 form1 = new Form1();

        form1.add_Button.Enabled = addButton;
        form1.edit_Button.Enabled = ediButton;
        form1.save_Button.Enabled = saveButton;
        form1.delete_Button.Enabled = deleteButton;
        form1.cancel_Button.Enabled = cancelButton;
    }

}

This is the form load event where I try to the static method

private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'test4DB.phone_book_table' table. You can move, or remove it, as needed.
        this.phone_book_TableAdapter.Fill(this.test4DB.phone_book_table);

        CustomClasses.ButtonEnable.ButtonSet(true, false, false, false, false);

    }

Thanks in advance.

CodePudding user response:

You create a new instance of Form1 in your method, this is destroyed when the method ends and has no effect on the other instance where Form1_Load belongs to.

You can pass the object into the method:

public static void ButtonSet(Form1 form1, bool addButton, bool ediButton, bool saveButton, bool 
    deleteButton, bool cancelButton)
    {
        form1.add_Button.Enabled = addButton;
        form1.edit_Button.Enabled = ediButton;
        ...
    

and call it like this in Form1_Load

CustomClasses.ButtonEnable.ButtonSet(this, true, false, false, false, false);

CodePudding user response:

There are 2 approaches you can implement:

  1. If it is a simple project with only one form, don't use static class and method. Instead define a normal method in your Form1 class that manipulates the properties of its buttons.
public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        this.phone_book_TableAdapter.Fill(this.test4DB.phone_book_table);

        this.ButtonSet(true, false, false, false, false);
    }

    public void ButtonSet(bool addButton, bool ediButton, bool saveButton, bool 
    deleteButton, bool cancelButton)
    {
        this.add_Button.Enabled = addButton;
        this.edit_Button.Enabled = ediButton;
        this.save_Button.Enabled = saveButton;
        this.delete_Button.Enabled = deleteButton;
        this.cancel_Button.Enabled = cancelButton;
    }
}

  1. However if it is mandatory to use a static class and method, you can change the static method signature to have another argument of Form1 type. This way you can change the properties of that object inside the method.
public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        this.phone_book_TableAdapter.Fill(this.test4DB.phone_book_table);

        ButtonEnable.ButtonSet(this, true, false, false, false, false);
    }
}
public static class ButtonEnable
{
    public static void ButtonSet(Form1 myForm, bool addButton, bool ediButton, bool saveButton, bool deleteButton, bool cancelButton)
    {
        myForm.add_Button.Enabled = addButton;
        myForm.edit_Button.Enabled = ediButton;
        myForm.save_Button.Enabled = saveButton;
        myForm.delete_Button.Enabled = deleteButton;
        myForm.cancel_Button.Enabled = cancelButton;
    }
}
  •  Tags:  
  • c#
  • Related