简体   繁体   中英

C# Enabling/Disabling buttons using an array, Win forms/MVP

i have an array of buttons [42] in a c# winforms desktop application:

private Button[] _tabButton = new Button[]{button00,button01,/*...,*/button41};

Than, when i called the "Initiate" methode i just do:

for(int i=0;i<_tabButton.Length;i++)
      {
       _tabButton[i].enabled = false;
      }

that was pretty simple and logic. Now, am newbie in MVP/MVC, but I did a little work in the code... i create an Interface and implement it,to get:

public bool Button_0 { get => button00.Enabled; set => button00.Enabled = value; }
   //...
   public bool Button_41 { get => button41.Enabled; set => button41.Enabled = value; }

also:

 private bool[] _arrayButton_bool;// = new bool[42];//an array of Button_0,Button_1...

So,my problem is: How to enable/disable the 42 Buttons, using boolean variables "Button_0... Button_41" inside the boolean array ".

If i say:

 `_arrayButton_bool = {Button_0,Button_1,...,Button_41};
 //... inside intiate methode :
  for(int i=0;i<_arrayButton_bool.Length;i++)
      {
       _arrayButton_bool[i].enabled = false;
      }`

that will affect "_arrayButton_bool[0]", not the variable:"Button_0", i know my question is ambiguous, am not so good in english,but please i did my best to have a little help. thanks.

You define setters like:

public bool Button_0 
{
    get => button00.Enabled; 
    set => button00.Enabled = value; 
}

so Burron_0 = true; would enable the button00 button. But the problem is, this is setter, so under the hood it is method, which you cannot simply put in an array. Trying to create array of those properties would only invoke each getter and provide bool value.

So it is impossible what you are asking. Cleanest way I can think of is to define method:

private void SetButtonsEnabledProperty(bool enabled)
{
    Button_0 = enabled;
    // through to
    Button_41 = enabled;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM