有人可以缩短这段代码吗?

时间:2016-09-04 09:55:38

标签: c#-3.0

我刚刚完成了一个程序(我是这个编程的初学者)现在我可能会以完全错误的方式做这个或者我的逻辑可能不是最好的编程但是任何帮助都会很棒我会发布我的代码到目前为止

单击按钮时使用此代码,按钮将发送文本,然后文本框将获取文本。

        if (txt1.Text == "")
        {
            txt1.Text = "J";
            btn1.Visible = false;
        }
        else if (txt1.Text != "")
        {
            if (txt2.Text == "")
            {
                txt2.Text = "J";
                btn1.Visible = false;
            }
            else if (txt2.Text != "")
            {
                if (txt3.Text == "")
                {
                    txt3.Text = "J";
                    btn1.Visible = false;
                }
                else if (txt3.Text != "")
                {
                    if (txt4.Text == "")
                    {
                        txt4.Text = "J";
                        btn1.Visible = false;
                    }
                    else if (txt4.Text != "")
                    {
                        if (txt5.Text == "")
                        {
                            txt5.Text = "J";
                            btn1.Visible = false;
                        }
                        else if (txt5.Text != "")
                        {
                            if (txt6.Text == "")
                            {
                                txt6.Text = "J";
                                btn1.Visible = false;
                            }
                            else if (txt6.Text != "")
                            {
                                if (txt7.Text == "")
                                {
                                    txt7.Text = "J";
                                    btn1.Visible = false;
                                }
                                else if (txt7.Text != "")
                                {
                                    if (txt8.Text == "")
                                    {
                                        txt8.Text = "J";
                                        btn1.Visible = false;
                                    }
                                    else if (txt8.Text != "")
                                    {

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

2 个答案:

答案 0 :(得分:1)

你需要将所有这些文本案例放到一个数组中,以便以下循环起作用(我在这里调用了数组'txt')。根据你所写的内容,这个循环应该与你的代码做同样的事情,但我不确定这是不是你真正想要做的。您的代码将单个文本框设置为“J”,然后仅当前面的每个文本字段都不是空字符串时才隐藏您的按钮(例如,这将包括设置为null的任何字段)。条件然后退出。

       `for (int i = 0; i < txt.Length; i++) {
            if(txt[i] != "") {
                continue;
            }
            else if(txt[i] == "") {
                txt[i] = "J";
                btn1.Visible = false;
                break;
            }
        }

答案 1 :(得分:0)

注意:我不知道这是否适用于C#3(它应该)。试试吧。

首先,您应该将所有文本字段放入数组中:

TextField[] textFields = { txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, };

然后,遍历文本字段以查找其中没有文本的文本字段:

foreach (TextField tf in textFields) {
    if (tf.Text == "") {

    }
}

在找到之后,我们想将其文本设置为&#34; J&#34;并使btn1不可见。由于我们已经找到了文本字段,因此我们不再需要继续循环,因此我们break

tf.Text = "J";
btn1.Visible = false;
break;

如果这在C#3中不起作用,只需更新到C#5或6好吗?

相关问题