简体   繁体   English

C# 使用数组启用/禁用按钮,Win 表单/MVP

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

i have an array of buttons [42] in a c# winforms desktop application:我在 c# winforms 桌面应用程序中有一组按钮 [42]:

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:现在,我是 MVP/MVC 的新手,但我在代码中做了一些工作......我创建了一个接口并实现它,以获得:

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 ".所以,我的问题是:如何启用/禁用 42 个按钮,使用 boolean 数组中的 boolean 变量“Button_0 ... Button_41”。

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.这将影响“_arrayButton_bool[0]”,而不是变量:“Button_0”,我知道我的问题模棱两可,我的英语不太好,但请我尽力提供一点帮助。 thanks.谢谢。

You define setters like:您定义 setter 如下:

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

so Burron_0 = true;所以Burron_0 = true; would enable the button00 button.将启用button00按钮。 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.尝试创建这些属性的数组只会调用每个 getter 并提供 bool 值。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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