tags:

views:

33

answers:

2

Hi, 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.

A: 

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)

Kamyar
+1  A: 

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.

Hans Passant
Hans, he might be talking about having text boxes scattered on the form in different panels. If the design is not modifiable, what should be done? Recursive iteration into each container panel or something to work on text boxes in it?
Nayan
That reminds me of a traffic sign I saw in Nevada. The practice target for a double-barreled shotgun.
Hans Passant
The controls are not on the same panel.
John
They are easy to move, click and drag. Partition your user interface, avoid making it look like that Nevada traffic sign. Considering creating a *new* one, another form, when the gun gets too big. I don't doubt I'm telling you stuff you already knew. No, using a bazooka doesn't help.
Hans Passant