Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将不同控件作为参数发送到方法_C#_Winforms_Rad Controls - Fatal编程技术网

C# 将不同控件作为参数发送到方法

C# 将不同控件作为参数发送到方法,c#,winforms,rad-controls,C#,Winforms,Rad Controls,我使用相同的表单输入新数据和现有记录,例如供应商、库存项目、客户等。在编辑模式下,我将所有文本框设置为只读,一旦用户选择要编辑的记录,我将只读状态重置为false。表单可能包含多个选项卡(或Telerik中的PageViewPages),每个选项卡都包含许多必须设置为只读true/false的可编辑控件 我使用此代码向名为FormControl的类发送容器数组 RadGroupBox [] containerList = { this.pageGeneral, this.pageBankDeta

我使用相同的表单输入新数据和现有记录,例如供应商、库存项目、客户等。在编辑模式下,我将所有文本框设置为只读,一旦用户选择要编辑的记录,我将只读状态重置为false。表单可能包含多个选项卡(或Telerik中的PageViewPages),每个选项卡都包含许多必须设置为只读true/false的可编辑控件

我使用此代码向名为FormControl的类发送容器数组

RadGroupBox [] containerList = { this.pageGeneral, this.pageBankDetail, this.pageContact };
FormControl.ControlsReadOnly(containerList, false);  // /truefalse to set Read-only status
在FormControl类中,我有以下代码来设置只读状态

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
publicstaticvoidcontrolsreadoly(RadGroupBox[]containerList,bool readOnlyStatus)
{
var container=(containerList[0]作为RadGroupBox);
for(int i=0;i
代码运行良好,但明显的缺点是,它仅在容器为RadGroupBox时有效。我希望通过更改调用表单中的控件类型,使用相同的代码来处理表单、组框和页面视图


我怀疑答案会涉及反射,但我无法解决它。我曾尝试在FormControl方法中替换参数列表以控制[]containerList,但我无法再使用var变量。

我不熟悉RadBox(Telerik之类的东西?)但是,沿着继承链向上爬行,以找到最低的公共类——也就是说,您要操作的所有控件是否都继承自同一基类

ReadOnly属性是在哪里定义的?如果它只存在于RadBox上,那么您可能运气不好。但是,如果它存在于RadBox继承的对象上,那么您所要做的就是将方法定义为接受这些类型的集合

例如,如果有一个BaseControl类具有只读属性,并且RadBox继承自该类,则只需传入“BaseControl[]ContainerList”。最终,您始终可以将对象作为其基类型进行传递


这就是你要找的吗?

@ChrisC是正确的,RadGroupBox继承自control类,并且control类有一个属性
Controls

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

}
有一件事我不明白:为什么要看同一个控件的数组长度的次数?你的意思是:

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


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

}
我希望这有帮助

资料来源:


下面是我如何实现它的,它非常有效

 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
publicstaticvoidcontrolsreadoly(Control[]containerList,bool readOnlyStatus)
{
for(int i=0;i

谢谢你的帮助!

看起来你要做这个通用的事情很困难。难道你不需要替换<代码> var容器=(容器列表[0)作为RADGROUPBOX)吗?< /代码>也有其他类型?这可能是一个很难的单一方法。考虑重载,然后从每个超负荷中调用一个通用方法。(接收人).这是我的第一个想法,事实上我现在是如何实现的:每种可能性一个方法。重载可能会起作用,不过,我会检查一下。好的,谢谢,这看起来是一种整洁的方法,肯定会起作用。我现在需要找到公共类,这可能很棘手。for…循环会遍历传入的容器数,foreach()循环遍历每个容器中的控件。关于enabled属性:我绝对不喜欢被禁用的控件所假设的深灰色,所以我更喜欢只读属性。在Todd的帮助下,我找到了答案!我会在回答我自己的问题后尽快发布答案(再过五个小时左右).谢谢托德!很高兴我能提供一些见解。
 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