Задать вопрос
@tryvols
Front-End разработчик

Почему не работает foreach?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Tester
{
    public partial class Test : Form
    {

        string[,] TestInfo = new string[30, 8];
        string TextAnswer = "";
        int Answers = 0;
        int RightAnswers = 0;
        int QuestionNumber = 1;

        public Test()
        {
            InitializeComponent();

            string conStr = @"Data Source=ADMINCHIC-PC;Initial Catalog=Tester;Integrated Security=True";
            SqlConnection connect = new SqlConnection(conStr);

            for (int i = 1; i < 31; i++)
            {
                connect.Open();

                SqlCommand read = new SqlCommand("SELECT * FROM Data1 WHERE ID = " + i.ToString(), connect);
                SqlDataReader reader = read.ExecuteReader();
                if (reader.Read())
                {
                    for (int j = 0; j < reader.FieldCount - 1; j++)
                    {
                        TestInfo[i - 1, j] = reader[j + 1].ToString();
                    }
                }

                connect.Close();
            }

            this.label2.Text = TestInfo[0, 0];
            this.checkBox1.Text = TestInfo[0, 1];
            this.checkBox2.Text = TestInfo[0, 2];

            if (TestInfo[0, 3] != null && TestInfo[0, 3] != "")
            {
                this.checkBox3.Text = TestInfo[0, 3];
                this.checkBox3.Visible = true;
            }
            else
            {
                this.checkBox3.Visible = false;
            }

            if (TestInfo[0, 4] != null && TestInfo[0, 4] != "")
            {
                this.checkBox4.Text = TestInfo[0, 4];
                this.checkBox4.Visible = true;
            }
            else
            {
                this.checkBox4.Visible = false;
            }

            if (TestInfo[0, 5] != null && TestInfo[0, 5] != "")
            {
                this.checkBox5.Text = TestInfo[0, 5];
                this.checkBox5.Visible = true;
            }
            else
            {
                this.checkBox5.Visible = false;
            }

            if (TestInfo[0, 6] != null && TestInfo[0, 6] != "")
            {
                this.checkBox6.Text = TestInfo[0, 6];
                this.checkBox6.Visible = true;
            }
            else
            {
                this.checkBox6.Visible = false;
            }

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (TestInfo[QuestionNumber, 0] != null && TestInfo[QuestionNumber, 0] != "")
            {

                this.label2.Text = TestInfo[QuestionNumber, 0];
                this.checkBox1.Text = TestInfo[QuestionNumber, 1];
                this.checkBox2.Text = TestInfo[QuestionNumber, 2];
                if (TestInfo[QuestionNumber, 3] != null && TestInfo[QuestionNumber, 3] != "")
                {
                    this.checkBox3.Text = TestInfo[QuestionNumber, 3];
                    this.checkBox3.Visible = true;
                }
                else
                {
                    this.checkBox3.Visible = false;
                }

                if (TestInfo[QuestionNumber, 4] != null && TestInfo[QuestionNumber, 4] != "")
                {
                    this.checkBox4.Text = TestInfo[QuestionNumber, 4];
                    this.checkBox4.Visible = true;
                }
                else
                {
                    this.checkBox4.Visible = false;
                }

                if (TestInfo[QuestionNumber, 5] != null && TestInfo[QuestionNumber, 5] != "")
                {
                    this.checkBox5.Text = TestInfo[QuestionNumber, 5];
                    this.checkBox5.Visible = true;
                }
                else
                {
                    this.checkBox5.Visible = false;
                }

                if (TestInfo[QuestionNumber, 6] != null && TestInfo[QuestionNumber, 6] != "")
                {
                    this.checkBox6.Text = TestInfo[QuestionNumber, 6];
                    this.checkBox6.Visible = true;
                }
                else
                {
                    this.checkBox6.Visible = false;
                }

                foreach (Control che in this.Controls)
                {
                    if (che.GetType().ToString().IndexOf("CheckBox") > -1)
                    {
                        CheckBox che1 = (CheckBox)che;
                        if (che1.Checked)
                            if (TestInfo[QuestionNumber - 1, 7] == che1.Text)
                            {
                                RightAnswers++;
                            }
                        che1.Checked = false;
                    }
                }

                Answers++;

                QuestionNumber++;

            }
            else
            {
                MessageBox.Show("Вы ответили правильно на " + RightAnswers.ToString() + " из " + Answers.ToString() + " вопросов!", "Результаты прохождения теста: ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

    }
}
  • Вопрос задан
  • 365 просмотров
Подписаться 1 Оценить 2 комментария
Ответ пользователя Андрей Ласточкин К ответам на вопрос (3)
lasalas
@lasalas
.NET Architect
Ответ написан
Комментировать