Курсовая работа: Автоматизированная обучающая система по дисциплине "Программирование"
}
private void LecturesForm_VisibleChanged(object sender, EventArgs e)
{
if (Visible == true)
{
SetChooseLectureMode();
}
}
private void linkLabel1_LinkClicked_1(object sender,
LinkLabelLinkClickedEventArgs e)
{
new ResultsForm().ShowDialog();
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
}
}
Код тестовой формы
namespace Kursach
{
public partial class TestForm : Form
{
private ChoiseButtons buttons;
private Test test;
private string userName;
private List<int> correctAnswers = new List<int>();
private Point startPosition = new Point(5, 15);
private Size buttonSize = new Size(200,
30);
private int distance = 5;
private Form parentForm;
public TestForm(string path, string testName, string userName, Form
formToClose)
{
parentForm = formToClose;
formToClose.Hide();
InitializeComponent();
this.userName = userName;
try
{
test = new Test(testName, path);
}
catch (Exception)
{
ShowCriticalErrorMessage("Один из файлов тестов
имеет неверный формат\nПриложение будет закрыто");
}
test.BeginTest();
Question question = test.CurrentQuestion;
buttons = new ChoiseButtons(ButtonTypes.CheckboxButton, startPosition.X,
startPosition.Y, buttonSize.Width, buttonSize.Height, distance);
InitQuestionWindow(question);
questionsGroupBox.Select();
}
private void InitQuestionWindow(Question question)
{
ButtonTypes type = (question.CorrectAnswers.Count != 1) ?
ButtonTypes.CheckboxButton : ButtonTypes.RadioButton;
buttons.Reset(type);
questionsGroupBox.Controls.Clear();
this.Text = question.QuestonName;
this.textBoxTest.Text = question.QuestonText;
for (int i = 0; i < question.Answers.Count; i++)
{
buttons.AddNextButton(question.Answers[i + 1]);
questionsGroupBox.Controls.Add(buttons[i]);
}
}
public static void ShowCriticalErrorMessage(string message)
{
ShowErrorMessage(message, "Критическая ошибка");
Environment.Exit(1);
}
private static void ShowInfoMessage(string info, string message)
{
ShowMessage(message, info, MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
private static void ShowErrorMessage(string info, string message)
{
ShowMessage(message, info, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private static void ShowMessage(string info, string message,
MessageBoxButtons buttons, MessageBoxIcon icon)
{
MessageBox.Show(message, info, buttons, icon);
}
private void buttonNextQuestion_Click(object sender, EventArgs e)
{
List<int> selected = buttons.GetSelected();
bool isCorrect = false;
if (selected.Count == test.CurrentQuestion.CorrectAnswers.Count)
{
isCorrect = true;
foreach (int correctQuestionNumber in test.CurrentQuestion.CorrectAnswers)
{
if (!selected.Contains(correctQuestionNumber - 1))
{
isCorrect = false;
}
}
}
if (isCorrect) correctAnswers.Add(test.CurrentQuestion.QuestionNumber);
if (!test.IsTestEnded)
{
InitQuestionWindow(test.GoToNextQuestion());
}
else
{
EndTest();
}
}
private void EndTest()
{
FileStream testFileStream = new FileStream("data\\results.txt",
FileMode.Append);
StreamWriter testStreamWriter = new StreamWriter(testFileStream);
testStreamWriter.WriteLine(DateTime.Now.ToShortDateString() + "
" + DateTime.Now.ToShortTimeString() + "\t" + test.TestName +
"\t" + userName + ": " + correctAnswers.Count +
"/" + test.QuestionsCount);
testStreamWriter.Close();
testFileStream.Close();
DialogResult userDecision = MessageBox.Show("Ваш результат - " + correctAnswers.Count + "/" + test.QuestionsCount +
"\nПерейти к выбору лекций?",
"Тест окончен", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (userDecision == System.Windows.Forms.DialogResult.Yes)
{
parentForm.Show();
this.Close();
}
else
{
Environment.Exit(0);
}
}
private void SetChooseLectureMode()
{
questionsGroupBox.Visible = false;
buttonNextQuestion.Visible = false;
textBoxTest.Visible = false;
}
private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (parentForm.Visible == false)
{
parentForm.Close();
}
}
private void TestForm_Load(object sender, EventArgs e)
{
}
}
}
Код формы результатов
namespace Kursach
{
public partial class ResultsForm : Form
{
public ResultsForm()
{
InitializeComponent();
try
{
FileStream fileStream = new FileStream("data\\results.txt",
FileMode.Open);
StreamReader streamReader = new StreamReader(fileStream);
textBox1.Text = streamReader.ReadToEnd();
this.Select();
streamReader.Close();
fileStream.Close();
}
catch
{
textBox1.Text
= "Никто не проходил тесты";
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Код информационной формы
namespace Kursach
{
public partial class InitForm : Form
{
public InitForm()
{
InitializeComponent();
}
public string UserName
{
get { return textBoxName.Text + " " + textBoxSurname.Text; }
}
private void buttonBeginTest_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void InitForm_Load(object sender, EventArgs e)
{
}
}
}
|