foreach,处理填充/未填充的文本框

时间:2015-07-02 10:02:06

标签: c# foreach textbox

我有一个包含多个对象(文本框,复选框,标签)的面板。在文本框中,您只能在>之间输入数字。 0& < 10.在下一步中,我必须检查用户是否根据需要输入了更多字段。因为每个文本框都意味着一个任务的结果,但是不允许学生回答所有问题,所以必须由他取消选择。他通常应该计为0或不填充。目前,我可以检测到他根据需要填充了更多文本框,但它总是将最后一个文本框设置为""在行中。但我希望当前选中的文本框将被清除""。我知道他为什么要清除最后一个文本框,但我不知道另一个想法。

我的听众:

   private void textBox_KTUTasks(object sender, EventArgs e)
        {
            int ungZaehler = 0, punkte = 0, i = 0;

            foreach (object tb in ((TextBox)sender).Parent.Controls)
            {

                if (tb is TextBox)
                {
                    if (!string.IsNullOrEmpty(((TextBox)tb).Text)  && ((TextBox)tb).Text != "0")
                    {
                        if (!int.TryParse(((TextBox)tb).Text, out ungZaehler))
                        {
                            ((TextBox)tb).Text = "";
                        }

                        if (ungZaehler <= 10 && ungZaehler >= 0)
                        {
                            punkte += ungZaehler;

                            i++;

                            if (i > 8) // 8 Tasks should be answered by him
                            {
                                MessageBox.Show("One Textbox must be free/unfilled", "Wrong Input",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                                ((TextBox)tb).Text = "";
                            }
                        }

                        else
                        {
                            MessageBox.Show("Not allowed to enter number higher than 10!", "Wrong Input",
                         MessageBoxButtons.OK, MessageBoxIcon.Error);
                            ((TextBox)tb).Text = "";
                        }

0 个答案:

没有答案