locked
Recursively and Conditionally disabling Form Controls RRS feed

  • Question

  •  I have a situation here. I am Trying to recursively disable my controls but based on who is logged in, I would like to keep certain areas enabled or disabled.

     

    I have 3 Panels --- A, B, C. When A is logged on, I want to disable all controls in A and C, but leave A and its contents enabled. If B is logged on, I want to disable all controls in A and C, but leave B and its contents enabled.

     

    I also noticed something. When I add a look up and do a RETURN when the current control is A, or B, or C, What happens is the loop starts over from the FORM CONTROL on my page, when I would rather it continue to the next control in the loop.

     

    Any idea how I can best implement this.

     

    Thanks in Advance.




    None
    Monday, July 6, 2009 7:50 PM

Answers

  • If you want it to continue to the next control in the loop, try using "continue" instead of "return". 
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Proposed as answer by Figo Fei Friday, July 10, 2009 7:27 AM
    • Marked as answer by Figo Fei Monday, July 13, 2009 2:36 AM
    Monday, July 6, 2009 8:11 PM
  • Also, you should not need to loop through the controls in the panel to disable them. If you disable the panel, all of the controls on the panel should automatically be disabled.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Figo Fei Friday, July 10, 2009 7:27 AM
    • Marked as answer by Figo Fei Monday, July 13, 2009 2:36 AM
    Monday, July 6, 2009 8:15 PM
  • The code below does the Trick. I removed the returns as suggested but turns out I was missing an ELSE in my condition


    public void SecureControls(Control control, Boolean status)

    {

    foreach (Control co in control.Controls)

    {

    if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshcomarea") && logger == "C")

    {;

    }

    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshamarea") && logger == "A")

    { ;

    }

    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshpmarea") && (logger == "P"))

    {;

    }

    else if (co.GetType().GetProperty("ReadOnly") != null)

    {

    co.GetType().GetProperty("ReadOnly").SetValue(co, (!status), null);

    }

    else if (co.GetType().GetProperty("Enabled") != null)

    {

    if ((co.GetType() != typeof(Label)) && (co.GetType() != typeof(Button) && co.ID != "closepage"))

    {

    co.GetType().GetProperty("Enabled").SetValue(co, status, null);

    }

    }

    else

    {

    SecureControls(co, status);

    }

    }

    }

     

     


    None
    • Proposed as answer by Figo Fei Friday, July 10, 2009 7:27 AM
    • Marked as answer by Figo Fei Monday, July 13, 2009 2:36 AM
    Monday, July 6, 2009 8:38 PM

All replies

  • Perhaps you could post your existing code.  This would help us narrow your issue down.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Monday, July 6, 2009 8:00 PM
  • public void SecureControls(Control control, Boolean status)
    {
    foreach (Control co in control.Controls)
    {
    if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshcomarea") && logger == "C")
    {
    return;
    }
    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshamarea") && logger == "A")
    {
    return;
    }
    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshpmarea") && (logger == "P"))
    {
    return;
    }
    if (co.GetType().GetProperty("ReadOnly") != null)
    {
    co.GetType().GetProperty("ReadOnly").SetValue(co, (!status), null);
    }
    else
    if (co.GetType().GetProperty("Enabled") != null)
    {
    if ((co.GetType() != typeof(Label)) && (co.GetType() != typeof(Button) && co.ID != "closepage"))
    {
    co.GetType().GetProperty("Enabled").SetValue(co, status, null);
    }
    }
    else
    {
    SecureControls(co, status);
    }
    }
    }
    
    

    None
    Monday, July 6, 2009 8:07 PM
  • If you want it to continue to the next control in the loop, try using "continue" instead of "return". 
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Proposed as answer by Figo Fei Friday, July 10, 2009 7:27 AM
    • Marked as answer by Figo Fei Monday, July 13, 2009 2:36 AM
    Monday, July 6, 2009 8:11 PM
  • Also, you should not need to loop through the controls in the panel to disable them. If you disable the panel, all of the controls on the panel should automatically be disabled.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Figo Fei Friday, July 10, 2009 7:27 AM
    • Marked as answer by Figo Fei Monday, July 13, 2009 2:36 AM
    Monday, July 6, 2009 8:15 PM

  •         public void SecureControls(Control control, Boolean status)
            {
                foreach (Control co in control.Controls)
                {
                    if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshcomarea") && logger == "C")
                    {
                        ;
                    }
                    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshamarea") && logger == "A")
                    {
                        ;
                    }
                    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshpmarea") && (logger == "P"))
                    {
                        ;
                    }
                    if (co.GetType().GetProperty("ReadOnly") != null)
                    {
                        co.GetType().GetProperty("ReadOnly").SetValue(co, (!status), null);
                    }
                    else
                        if (co.GetType().GetProperty("Enabled") != null)
                        {
                            if ((co.GetType() != typeof(Label)) && (co.GetType() != typeof(Button) && co.ID != "closepage"))
                            {
                                co.GetType().GetProperty("Enabled").SetValue(co, status, null);
                            }
                        }
                        else
                        {
                            SecureControls(co, status);
                        }
                }
                return;
            }

    Or just get rid of them altogether.  They are not needed, unless you are only trying to set certain controls.  This where nested IF blocks get crazy, and why I avoid them like the plague.  Sometimes the logic gets so convoluted you forget where you began.

    Swtich is easier to read, but can get just as bad when just as abused.

    SOLUTION:
    Stop trying to do so much within one method.  Create a separate method for each A, B, or C.  Create another, a fourth method, that calls the first 3 in the proper order or not all.  Name this fourth method with the name you have posted above.

    Rudedog  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Figo Fei Friday, July 10, 2009 7:27 AM
    Monday, July 6, 2009 8:23 PM
  • The code below does the Trick. I removed the returns as suggested but turns out I was missing an ELSE in my condition


    public void SecureControls(Control control, Boolean status)

    {

    foreach (Control co in control.Controls)

    {

    if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshcomarea") && logger == "C")

    {;

    }

    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshamarea") && logger == "A")

    { ;

    }

    else if ((co.GetType() == typeof(Panel)) && (co.ID == "pnlshpmarea") && (logger == "P"))

    {;

    }

    else if (co.GetType().GetProperty("ReadOnly") != null)

    {

    co.GetType().GetProperty("ReadOnly").SetValue(co, (!status), null);

    }

    else if (co.GetType().GetProperty("Enabled") != null)

    {

    if ((co.GetType() != typeof(Label)) && (co.GetType() != typeof(Button) && co.ID != "closepage"))

    {

    co.GetType().GetProperty("Enabled").SetValue(co, status, null);

    }

    }

    else

    {

    SecureControls(co, status);

    }

    }

    }

     

     


    None
    • Proposed as answer by Figo Fei Friday, July 10, 2009 7:27 AM
    • Marked as answer by Figo Fei Monday, July 13, 2009 2:36 AM
    Monday, July 6, 2009 8:38 PM