Using a control as input for a method

时间:2015-09-30 23:18:37

标签: c# methods input

Im trying to create a method that will make all textboxes Enable property false. The method input will be the tab name, but something is not working

public void fechacampos(Control tab)
    {
        foreach (Control control in this.tab.Controls)
        {
            if (control is TextBox)
            {
                TextBox tb = control as TextBox;
                tb.Enabled = false;      
            }
        }
    }

Can someone help? Thanks!

1 个答案:

答案 0 :(得分:0)

可能还有其他控件嵌套在选项卡中(例如Panels),里面有文本框,所以你需要递归调用你的方法:

#include <iostream>
#include <math.h>

using namespace std;

double factorial(double x)
{   
    int i;
    double x_new=1;
    for (i=x; i>=1; i--)
    {
        x_new*=i;
    }

    return x_new;
}

double Pascal(double n, double p){
    double d=n-p;
    double fact_1;
    double fact_2;
    double fact_3;
    fact_1=factorial(n);
    fact_2=factorial(p);
    fact_3=factorial(d);
    return fact_1/(fact_2*fact_3);
}


int main()
{
    int n;
    double  numRows;
    double numCollumns;
    cout << "Enter number of rows: ";
    cin >> numRows;
    n=numRows+1;
    for (numRows=0; numRows<n;numRows++)
    {
        for(numCollumns=0; numCollumns<=numRows;numCollumns++)
        {
            cout << Pascal(numRows, numCollumns) << " ";
        }
        cout << endl;
    }




}  

//this is the output of the function when numRows is set to 6:
//  1  
//  1 1
//  1 2 1
//  1 3 3 1
//  1 4 6 4 1
//  1 5 10 10 5 1

此外,方法的参数不是选项卡名称,它是一个控件。如果您想以标签名称开头,则必须先获取标签:

public void DisableTextBoxes(Control tab)
{
    foreach (Control control in tab.Controls)
    {
        if (control is TextBox)
        {
            TextBox tb = control as TextBox;
            tb.Enabled = false;
        }
        else
        {
            foreach (Control innerControl in control.Controls)
            {
                DisableTextBoxes(control);
            }
        }
    }
}