• Greg (unregistered)

    I have no experience with Windows.Forms, but I know that when using the Qt framework, there is a subtle difference between disabling a widget vs disabling all its children: in the first case the (parent) widget no longer receives keyboard event while in the latter it does.

  • Jaloopa (unregistered)
    private bool ToDisable(Control control)
    {
        return control is TextBox
            || control is ComboBox
            || control is CheckBox
            || control is DateTimePicker
            || control is NumericUpDown;
    }
    
    ...
    
    foreach(var C in pnlPrincipal.Controls.Union(grpProveedor.Controls).Union(grpDescuentoGeneral.Controls)
        .Where(ToDisable))
    {
        C.Enabled = bolBloquear;
    }
    
  • 516052 (unregistered)

    Honestly, I prefer the second scenario. I really do. One is a lazy screwup by an idiot. The other is a masterfully crafted mystery of misery mangled magnificently by a malevolent mangler for the future misfortune of miserable maintainers.

  • some guy (unregistered)

    I'll add: if your variables are not in English or something that can be construed as such, you have another big problem. And that's from somebody whose first language is not English.

  • (nodebb)

    What makes me think it might be the second scenario is that the code doesn't check if the control is a button, so this might be how the programmer disables everything except buttons. That would be a reasonable UI - if you have a bunch of input fields and then Start and Stop buttons, you would want the Stop button to stay enabled.

    Everything else about it is a huge WTF, of course.

  • Rob (unregistered) in reply to Dragnslcr

    Surely then it'd be eazier to do if (C.GetType() != typeof(System.Windows.Forms.Button)).

  • PedanticRobot (unregistered)

    I've done something similar to this once or twice, written more efficiently and more clearly of course, but the point was to disable all the input controls while leaving informational controls (labels, images, and a textbox) enabled. Generally it's better to split such controls off into their own panels or groupboxes, but occasionally that wasn't practical and I have to jump in an do it manually.

  • Fik of the borg (unregistered) in reply to some guy

    Former chief on IT here. ounterpoint: One of my coding rules was naming the variables in spanish or recognizable spanish abbreviations, precisely to make it obvious that they were our applications variables and not language keywords, usually in english. No Ñs or accents allowed, though.

  • (nodebb)

    "Bloquear" in Spanish means "to block" and you can translate it as "to disable" so the semantic of the bolBloquear variable is inverted. It should have been called bolPermitir or bolHabilitar or something like that. And why did they bother giving the iterators different names C, C1 and C2? That implies they either copy-pasted the block and then renamed the variable or they just wrote every block from scratch. Hard to tell at this point which is worse.

  • (nodebb)

    Careful, there's a key difference between GetType()== and is, that difference being the behavior with derived classes. Now, without more data to know which behavior is desirable...

  • (nodebb)

    In order to do this, it checks the type of the control for some reason.

    Most likely because there are some controls in the panel that shouldn't be disabled (static labels, the Cancel button, etc.) so you mustn't just blindly disable all controls. (Yes, that's already been said, but @Dragnslcr forgot non-button other things that you might not want to disable.)

  • Tim (unregistered)

    Isn't there some mechanism where you can group your controls inside a container and when you disable that container it disables all the controls? I'm sure I remember that from my vb6 days (or <fieldset> for web folks)

  • 516052 (unregistered) in reply to Medinoc

    No behavior is desirable in this context. The proper way to do this is to reference each control by its name so as to ensure any change in value is done on a deliberately chosen element. Yes, it's more code to write but it is much, much more maintainable.

  • Old Timer (unregistered)

    I too have enabled / disabled controls by type. It was messy, but real life often is messy, and code reflects that.

    We strive for clarity and clean lines of separation, but If real life wasn't so messy, we'd have far fewer big projects coming in 3 years late, and at triple the expected cost.

  • (nodebb) in reply to Rob

    Surely then it'd be eazier to do if (C.GetType() != typeof(System.Windows.Forms.Button)).

    Yes. Like I said, it's still a huge WTF.

  • (nodebb)

    I think there is a misunderstanding how WinForms work:

    Disabling the parent control is NOT the same as disabling all children.

    The reason is simple, the disable state themes the control differently and for group boxes you actually don't won't the visual change, because it looks ugly.

    Now the implementation is sloppy and the copy&past makes it obvious that there should be instead be a filter method and then you end up with one foreach per group with controls filtered a simple boolean condition to change Enabled. But Jaloopa already pointed that out.

  • (nodebb)

    is keyword would not be the same since is allows for matching on a subclass while comparing Type objects for equality does not (you'd want Type.IsAssignableFrom to match the behavior of is).

Leave a comment on “Blocked Up”

Log In or post as a guest

Replying to comment #692477:

« Return to Article