【问题标题】:Creating a function to be reusable multiple times in my code在我的代码中创建一个可多次重用的函数
【发布时间】:2014-11-14 06:06:58
【问题描述】:

我希望能够在我的代码中多次调用以下函数来填充表单中不同的 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;
        }


    }

【问题讨论】:

  • 自从我做任何 Windows 窗体的东西以来,我已经很久没有做过任何 Windows 窗体的东西了,假设就是这样,但没有一些 ContainerControl.FindControlFindChildControls.Find 或类似的东西可以让你从string 名称变为实际控制?你可以这样做。
  • 你不能使用数组或文本框列表吗?
  • Ahmad 的建议是要走的路:为每个组创建一个List&lt;TextBox&gt; 并将其传递给函数!正如 feiyun0112 所建议的那样,您可以使用名称,但是拥有好命名的组要好得多! (更易读、更灵活、更快……)

标签: c# variables


【解决方案1】:

为每个组创建一个List&lt;TextBox&gt;

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

}

并将这样一个组传递给函数:

private void convertBasetoDrawn(List<TextBox> list, string numBase, string reference)
{
    string[] texts = new string[8] 
                 { "000", "500", "050", "005", "550", "505", "055", "555" };

    for (int t = 0; t < list.Count; t++)  list[t].Text = texts[t];
    list[0].ForeColor = Color.Red;
    list[7].ForeColor = Color.Red;

}

假设文本总是这样。如果它们依赖,也许numbase 你也可以动态构造它们,只要你知道规则。也许即使是简单的替换也可以完成这项工作?

你没有使用reference,顺便说一句..

现在,我只是在这里猜测,但也许这就是你的文本的模式......:

 string[] texts = new string[8] 
                  { "nnn", "dnn", "ndn", "nnd", "ddn", "dnd", "ndd", "ddd" };
 for (int s = 0; s < texts.Length; s++) 
      texts[s] = texts[s].Replace("d", numBase).Replace("n", reference);

现在你可以这样称呼它:

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;
}

然后这样称呼它:

 convertBasetoDrawn(list01, "001", "5");

 convertBasetoDrawn(list02, "000", "1");

注意:这里没有结转..您必须为此定义规则并自己编写代码..

【讨论】:

  • 我喜欢你的替换,因为它可以为我节省大量代码,但在我看来,我似乎无法让它工作。
  • 我喜欢你的替换,因为它可以为我节省大量代码,但在我看来,我似乎无法让它工作。说 numBase = "000" 模式为真并正确替换,但说 numBase = "001" 那么字符串数组将是 "001"、"501"、"051"、"006"、"551"、"506 ”、“056”和“556”。如果可以创建一个掩码来增加模式和基数的基数,那将非常有帮助。
  • 啊,我明白了。在这种情况下,我们需要进行一些更改。输入将位于什么范围内?输出只有 3 位数字吗? numbase 可以是 999 还是 reference = 9 ?输出会是什么??
  • 试试这个而不是替换:` string newText = ""; for (int s = 0; s
  • numBase 000 - 999 的范围。引用仅用于传入文本框的基本名称,现在已在声明中消失。现在是 private void convertBaseToDrawn(string numBase, List list)
【解决方案2】:

目前尚不清楚您打算如何确定具体的八人组。但是让我们假设你有某种方式。

然后,如果我正在编写此代码,我将使用 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 实例并在 switch 中使用它,如下所示:

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;

上面的一个变体将使用带索引的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 控件组的一致性。

【讨论】:

    猜你喜欢
    • 2018-03-02
    • 2022-08-21
    • 2021-08-08
    • 1970-01-01
    • 2011-12-30
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多