Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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#_Variables - Fatal编程技术网

C# 在我的代码中创建可多次重用的函数

C# 在我的代码中创建可多次重用的函数,c#,variables,C#,Variables,我希望能够在我的代码中多次调用以下函数,以填充表单中不同组的8个文本框 现在引用正在“tbPlay”中传递,从代码中最初调用它的地方开始 每次调用此函数时,将填充不同的文本框组 我正试图想出一种方法,使用空for循环来创建必要的变量名,以替换case语句中的tbPlay0-7,因此它不仅可用于代码中的一组文本框。我不确定能不能做到 有人能帮忙吗 private void convertBasetoDrawn(string numBase, string reference) {

我希望能够在我的代码中多次调用以下函数,以填充表单中不同组的8个文本框

现在引用正在“tbPlay”中传递,从代码中最初调用它的地方开始

每次调用此函数时,将填充不同的文本框组

我正试图想出一种方法,使用空for循环来创建必要的变量名,以替换case语句中的tbPlay0-7,因此它不仅可用于代码中的一组文本框。我不确定能不能做到

有人能帮忙吗

    private void convertBasetoDrawn(string numBase, string reference)
    {
        string baseNumber = numBase;

        for (int i = 0; i < 8; i++)
        {
            //some code here to create variables to replace the text box names in the
            //following case statement
        }

        switch (baseNumber)
        {
            case "000":
                tbPlay0.Text = "000";
                tbPlay0.ForeColor = Color.Red;
                tbPlay1.Text = "500";
                tbPlay2.Text = "050";
                tbPlay3.Text = "005";
                tbPlay4.Text = "550";
                tbPlay5.Text = "505";
                tbPlay6.Text = "055";
                tbPlay7.Text = "555";
                tbPlay7.ForeColor = Color.Red;
                break;
        }


    }
private void convertBasetoDrawn(字符串numBase,字符串引用)
{
字符串baseNumber=numBase;
对于(int i=0;i<8;i++)
{
//此处的一些代码用于创建变量以替换
//以下案例陈述
}
开关(基本编号)
{
案例“000”:
tbPlay0.Text=“000”;
tbPlay0.ForeColor=颜色。红色;
tbPlay1.Text=“500”;
tbPlay2.Text=“050”;
tbPlay3.Text=“005”;
tbPlay4.Text=“550”;
tbPlay5.Text=“505”;
tbPlay6.Text=“055”;
tbPlay7.Text=“555”;
tbPlay7.ForeColor=颜色。红色;
打破
}
}

不清楚您计划如何识别特定的八人组。但让我们假设你有

然后,如果我在编写这段代码,我会使用
UserControl
来封装重复的模式,将八个
TextBox
控件(或者更确切地说,您想要访问的控件的属性)作为属性公开。例如

class TextBoxGroup : UserControl
{
    public string Text1
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }

    public Color ForeColor1
    {
        get { return textBox1.ForeColor; }
        set { textBox1.ForeColor = value; }
    }

    public string Text2
    {
        get { return textBox2.Text; }
        set { textBox2.Text = value; }
    }

    public Color ForeColor2
    {
        get { return textBox2.ForeColor; }
        set { textBox2.ForeColor = value; }
    }

    // ...

    public string Text8
    {
        get { return textBox8.Text; }
        set { textBox8.Text = value; }
    }

    public Color ForeColor8
    {
        get { return textBox8.ForeColor; }
        set { textBox8.ForeColor = value; }
    }
}
然后,在您的方法中,您只需检索相应的
TextBoxGroup
实例并在
开关中使用它,而不是使用您计划用于计算组的起始索引的任何逻辑,如下所示:

case "000":
    textBoxGroup.Text1 = "000";
    textBoxGroup.ForeColor1 = Color.Red;
    textBoxGroup.Text2 = "500";
    textBoxGroup.Text3 = "050";
    textBoxGroup.Text4 = "005";
    textBoxGroup.Text5 = "550";
    textBoxGroup.Text6 = "505";
    textBoxGroup.Text7 = "055";
    textBoxGroup.Text8 = "555";
    textBoxGroup.ForeColor8 = Color.Red;
    break;
convertBasetoDrawn(list01, "0","5");
上面的一个变体将使用setter方法封装属性,setter方法采用索引。例如

class TextBoxGroup : UserControl
{
    // Initialized in constructor to be the eight TextBoxes
    private TextBox[] _textboxes;

    public void SetText(int i, string text)
    {
        _textboxes[i].Text = text;
    }
}
当然,如果您不想使用
UserControl
,可以在主窗体中初始化类似的数据结构,这样就可以通过索引访问控件。但就个人而言,我更喜欢
UserControl
,因为它更易于重用,并确保
TextBox
控件的所有组的一致性。

为每个组创建一个
列表:

    List<TextBox> list01 = new List<TextBox>() { tbPlay0, tbPlay1, ....};
    List<TextBox> list02 = new List<TextBox>() { ..., ... , ....};
    // ..

}
现在你可以这样称呼它:

case "000":
    textBoxGroup.Text1 = "000";
    textBoxGroup.ForeColor1 = Color.Red;
    textBoxGroup.Text2 = "500";
    textBoxGroup.Text3 = "050";
    textBoxGroup.Text4 = "005";
    textBoxGroup.Text5 = "550";
    textBoxGroup.Text6 = "505";
    textBoxGroup.Text7 = "055";
    textBoxGroup.Text8 = "555";
    textBoxGroup.ForeColor8 = Color.Red;
    break;
convertBasetoDrawn(list01, "0","5");
更新:对于我现在理解的规则,您可以:

string newText = "";
for (int s = 0; s < texts.Length; s++)
{
    newText = "";
    for (int i = 0; i < 3; i++)
    {
        if (texts[s][i] == 'n') newText += numBase[i];
        else  newText +=  (Convert.ToByte(numBase[i].ToString()) + 
                            Convert.ToByte(reference[0].ToString()) ).ToString("0");
    }
    texts[s] = newText;
}


注:此处无结转。。你必须为它定义规则并自己编写。

我做任何Windows窗体的东西都已经很久了,假设这就是它,但是不是有一些
ContainerControl.FindControl
FindChild
Controls.Find
或类似的东西可以让您从
字符串
名称转换为实际的控件吗?你可以这样做。难道你不能使用数组或文本框列表吗?艾哈迈德的建议是:为每个组创建一个
列表
,并将其传递给函数!正如feiyun0112所建议的那样,您可以使用这个名称,但是拥有好名字的组要好得多!(更易读,更灵活,更快…)我喜欢你的替换,因为它可以节省我很多代码,但在我看来,我似乎无法使它工作。我喜欢你的替换,因为它可以节省我很多代码,但在我看来,我似乎无法使它工作。假设numBase=“000”模式为true并正确替换,但如果说numBase=“001”,则字符串数组将为“001”、“501”、“051”、“006”、“551”、“506”、“056”和“556”。如果可以创建一个遮罩来增加图案和底数的底数,那将非常有用。啊,我明白了。那样的话,我们需要做些改变。。输入在什么范围内?输出是否仅为3位数字?numbase可以是999或reference=9吗?输出结果会是什么?请尝试以下内容,而不是替换:`string newText=“”;for(ints=0;s convertBasetoDrawn(list01, "001", "5");
 convertBasetoDrawn(list02, "000", "1");