开发者

How set properties enable on false for very textbox on form with extender provider. {C#, Winforms}

开发者 https://www.devze.com 2023-01-18 07:09 出处:网络
I have 30 textbox and other control on winform, and I want sometimes set their properties (enable, visible) on true/false. I don’t make duplicate code. I am newbie in winforms, and I’d like know wha

I have 30 textbox and other control on winform, and I want sometimes set their properties (enable, visible) on true/false. I don’t make duplicate code. I am newbie in winforms, and I’d like know what is the good solution开发者_如何学编程 for this problem. I’d like use extender provider, but I’d know if it is suitable. Sory for my english. :)

If someone can give me a code example, I will be very grateful.


IExtenderProvider is not a suitable solution to this, it was meant to add new properties to existing controls. The generic approach is quite simple: use a Panel. Put all the controls you want to disable or hide in that panel. And set the panel's Visible or Enabled property to false. This will automatically disable all the child controls of the panel. And when you hide the panel, its children will be hidden too.


It depends on the situations when you want to toggle visibility and enable/disable status.
If you want to change the state, you can do something like:

private void ToggleVisible(TextBox tb)
{
  tb.Visible = !tb.Visible;
}
private void ToggleEnable(TextBox tb)
{
  tb.Enabled= !tb.Enabled;
}  

Also if you want to update the status of multiple textboxes at the same time, you better define a method and do the batch updating for certain groups.
What really matters is to categorize (group) your textboxes properly. (into panels if you can)


Here's how I've done something like that:

In the base form class from which my forms inherit (which, in turn, inherits from Form)....

#region Form Fields Enablers

protected virtual void EnableFormFields(Control ctl)
{
  EnableFormFields(ctl, true);
}

protected virtual void DisableFormFields(Control ctl)
{
  EnableFormFields(ctl, false);
}

protected virtual void EnableFormFields(Control ctl, bool enable)
{
  EnableFormFields(ctl, enable, new string[0]);
}

protected virtual void EnableFormFields(Control ctl, bool enable, params string[] excludeControlName)
{
  bool exclude = false;
  foreach (string excludeCtl in excludeControlName)
  {
    if (string.Compare(ctl.Name, excludeCtl, true) == 0)
    {
      exclude = true;
      break;
    }
  }

  if (!exclude)
  {
    ctl.Enabled = enable;
    foreach (Control childCtl in ctl.Controls)
    {
      EnableFormFields(childCtl, enable, excludeControlName);
    }
  }
}

#endregion  Form Fields Enablers

Then, in the particular form, I call this.EnableFormFields(someContainerControl) when I want to set all the controls within a given container, such as all controls on all tabs (tabcontrol) or all controls on a single tab (tabpage), or even all the controls on the form (passing "this"), etc. I can even exclude some controls by passing their names as a string array, or each such name as a separate trailing parameter ("params" in the method definition's parameter list).

I think this is what John was seeking. The important note for a newbie is the recursiveness of this code, setting the Enable property for a given control, then calling the method to set the Enable property in each of the control's child controls. Each of these, in turn, set the property on their child controls.

Thanks, John


You could consider to subclass the Textbox and add your own properties:

class MyTextbox : Textbox {
    public bool MyEnable {
        get{ return someBool; }
        set {
            if (yourConditionIsMet) {
                someBool = value;
                this.Enabled = value;
            }
        }
    }
}

...then iterate over all your controls in your form:

foreach (Control control in this.Controls) {
    if (control is MyTextbox)
        control.MyEnable = true;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消