Preventing controls enable property from changing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pakmarshal
    New Member
    • Aug 2009
    • 17

    Preventing controls enable property from changing

    Hi Everyone,
    I m implementing control based security in my application. where i m saving controlname (per form) in database, which are to be restricted.

    So far I have implemented the security and restricted control get disabled.Now what i want to do is that i have to prevent that for those controls the "control.Enable " property should not change. how can i restrict that.

    I have achieved the functionality by canceling enable=true property in "Invalidate d" event of the control. but this is not some generic way. I have to map this event for all controls in all forms :(.

    What i really want is that as all my froms are derived from same parent which implements the security. so the parent should some how restrict enable property from changing.

    So,HOW CAN I PREVENT CONTROL enable property from changing in their parent class?

    Regards
    Haroon.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Someplace you are setting a bool that means when the user has permission, right?

    myControl.Enabl ed = mySecurityBool;

    If they have permission then mySecurityBool is true and thus enabled is true.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      You may also want to just keep them from running Task Manager.
      You can limit the programs a user is allowed to run.

      Comment

      • Pakmarshal
        New Member
        • Aug 2009
        • 17

        #4
        Thanks a lot tlhintoq for your respose,
        But I think I could not convey my problem properly.
        let me try again.

        for example a "frmEnroll" (is some enrollment form) has a button "btnOk". The button "btnOK" is not allowed to a user "ABC".

        What I have already achieved is when frmEnroll loads "btnOk" for user disabled as desired.

        Now the functionality in frmEnroll is such that as soon as user enters some text in a textbox, the btnOK gets enable if the button is allowed to the user (IN MY CASE the button is not allowed for "ABC").

        now how can i make sure when the coder writes "btnOk.enable=T RUE" in his code. The buttons gets enable if the user is allowed to use the button but in case the user is restricted like "ABC", the button should not change its enabled property.

        In the above example I considered just one buttton but in real senerios there are many and i dont want to implement check for every button on every form. Rather I m looking for the mechanism so that the button.Enable event triggers (before actaully enabling the control) to some parent routine where parents check if the user is authenticated to use the button it allows the button to enable other wise if user is not authenticated the event simply get cancelled so that the button wont be enable for restricted users.

        Mostly buttons and menus items are getting restricted in my case.

        Thanks and Regards,
        Haroon.

        Comment

        • phommy
          New Member
          • Mar 2010
          • 4

          #5
          you mean the controls' Enabled property is a result of more than one variant, is it?
          I'm afraid you need to set the Enabled value every time the input changes, or code want to change enable to true. i don't know any shortcut

          or you may change the logic slightly. Allow coder to change enabled property. and when endusers click the button with a input 'ABC', show a alert and return

          Comment

          • Pakmarshal
            New Member
            • Aug 2009
            • 17

            #6
            no no
            Its simple
            I want enabling event to stop triggering.
            so that even if their some some statement like button.Enable= TRUE
            even then the event get blocked and wont enable the button.

            But I dont want this thing in buttonEnabled_E VENT, where i will be reverting the change.

            Thanks and Regards,
            Haroon.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              You aren't going to block the events. You can stop thinking along those lines right now.
              now how can i make sure when the coder writes "btnOk.enable=T RUE" in his code.
              What you can do is write an override for the .Enabled property and write your own method for checking all your security parameters. If they are good, then continue to enable it. If they fail, then don't. But you would have to do that for every control and it seems like a big pain.

              How about if you you instead subscribe all the controls to a single .EnabledChanged event handler. That method then validates the entire form enabling and disabling all the controls of the form in accordance with your security rules?

              Comment

              • phommy
                New Member
                • Mar 2010
                • 4

                #8
                try this: set Enabled=false after it is set to true with an input 'ABC'

                Code:
                    public partial class Form1 : Form
                    {
                        Button btn;
                        TextBox txt;
                        public Form1()
                        {
                            InitializeComponent();
                
                            //a textbox
                            txt = new TextBox();
                            Controls.Add(txt);
                
                            //a button to change Enabled of btn
                            Button ctrl = new Button();
                            ctrl.Top = 40;
                            Controls.Add(ctrl);
                            ctrl.Click += new EventHandler(ctrl_Click);
                
                            //another button whose enabled is changed
                            btn = new Button();
                            btn.Top = 80;
                            Controls.Add(btn);
                            btn.EnabledChanged += new EventHandler(btn_EnabledChanged);
                        }
                
                        void ctrl_Click(object sender, EventArgs e)
                        {
                            btn.Enabled ^= true;
                        }
                
                        void btn_EnabledChanged(object sender, EventArgs e)
                        {
                            (sender as Button).Enabled &= txt.Text != "ABC";
                        }
                
                        void b1_Click(object sender, EventArgs e)
                        {
                            btn.Enabled = !btn.Enabled;
                        }
                    }

                Comment

                • Pakmarshal
                  New Member
                  • Aug 2009
                  • 17

                  #9
                  :) thanks tlhintoq and phommy for your nice and helpful advices.

                  Thanks.

                  Comment

                  Working...