Question

I use the same form to enter new data and enter existing records, e.g. for suppliers, stock items, customers, etc. In Edit mode I set all text boxes to Read-Only, and once the user has selected the record to edit, I reset the read-only status to false. A form may contain several tabs (or PageViewPages in Telerik), each containing a number of editable controls that have to be set read-only true/false.

I use this code to send an array of the containers to a class named FormControl.

RadGroupBox [] containerList = { this.pageGeneral, this.pageBankDetail, this.pageContact };
FormControl.ControlsReadOnly(containerList, false);  // /truefalse to set Read-only status

In the FormControl class I have the following code to set the Read-Only status.

public static void ControlsReadOnly(RadGroupBox [] containerList, bool readOnlyStatus)
    {
       var container = (containerList[0] as RadGroupBox);

        for (int i = 0; i < containerList.Length; i++)
        {
            foreach (var control in container.Controls)
            {
                RadTextBox textEdit = control as RadTextBox;
                if (textEdit != null)
                {
                    textEdit.ReadOnly = readOnlyStatus;
                    continue;
                }

                RadMaskedEditBox textMasked = control as RadMaskedEditBox;
                if (textMasked != null)
                {
                    textMasked.ReadOnly = readOnlyStatus;
                    continue;
                }

                // rest of the code

The code works well, but the obvious shortcoming is that it it only works if the container is a RadGroupBox. I want to use the same code to deal with forms, group boxes and PageViews by changing the control type in the calling form.

I suspect the answer is going to involve Reflection, but I cannot solve it. I have tried substituting the parameter list in the FormControl method to Control [] containerList, but then I cannot use the var variable anymore.

Was it helpful?

Solution 2

@ChrisC is correct, The RadGroupBox inherits from the control class, and the control class has a property Controls.

public static void ControlsReadOnly(Control[] containerList, bool readOnlyStatus)
{

}

One thing I don't understand: Why do you look at the same control the number of times as the length of your array? Did you mean:

public static void ControlsReadOnly(Control[] containerList, bool readOnlyStatus)
{


    for (int i = 0; i < containerList.Length; i++)
    {
          var control = containerList[i];
          //... omitted code for brevity 
    }

}

Finally, I don't know if this helps, but any Control can be Enabled. This might help keep the access consistent, such that you might be able to do something like this:

public static void ControlsReadOnly(Control[] containerList, bool readOnlyStatus)
{
     foreach (var control in container.Controls)
     {
          foreach (var control in container.Controls)
          {
              if((control as RadTextBox) != null)
              {
                 control.Enabled = !readOnlyStatus; 
              }
          }
     }
}

I hope this helps!

Sources:

http://www.telerik.com/help/winforms/t_telerik_wincontrols_ui_radgroupbox.html http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx

OTHER TIPS

I'm not familaiar with the RadBox (Telerik stuff?) but crawl up the chain of inheritance to find the lowest common class - that is, do all of your controls that you want to manipulate inherit from the same base class?

Where is the ReadOnly property defined? If it exists only on the RadBox, then you might be out of luck. However, if it exists on something that RadBox inherits from, then all you have to do is define your method as accepting a collection of those types instead.

For instance, if there was a BaseControl class that had the Readonly property on it, and RadBox inherited from there, just pass in "BaseControl [] ContainerList". Ultimately, you can always pass an object as it's base type.

Is this what yo uare looking for?

Here is how I implemented it, and it works great.

 public static void ControlsReadOnly(Control [] containerList, bool readOnlyStatus)
    {
        for (int i = 0; i < containerList.Length; i++)
        {
            foreach (var control in containerList[i].Controls)
            {
                // ignore labels
                if ((control as RadLabel) != null)  
                {
                    continue;
                }

                // other editable controls
                else if ((control as RadTextBox) != null)
                {
                    (control as RadTextBox).ReadOnly = readOnlyStatus;
                    continue;
                }

                else if ((control as RadMaskedEditBox) != null)
                {
                    (control as RadMaskedEditBox).ReadOnly = readOnlyStatus;
                    continue;
                }

                // rest of code

Thanks for your help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top