Exemplo n.º 1
0
        private void dataGridViewTests_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView             dataGrid = (DataGridView)sender;
            DataGridViewButtonColumn button   = dataGrid.Columns[e.ColumnIndex] as DataGridViewButtonColumn;

            int id = ((int)dataGrid.Rows[e.RowIndex].Cells[0].Value);

            if (e.ColumnIndex == dataGrid.Columns["ColumnEditTestButton"].Index && e.RowIndex >= 0)
            {
                using (Model.KeynerContext db = new Model.KeynerContext())
                {
                    Model.Test  test = db.TestSet.FirstOrDefault(t => t.Id == id);
                    FormNewTest form = new FormNewTest(ref test);
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.Yes)
                    {
                        this.FillDataGridTests();
                    }
                }
            }

            if (e.ColumnIndex == dataGrid.Columns["ColumnDeleteTestButton"].Index && e.RowIndex >= 0)
            {
                if (MessageBox.Show("Do you want delete this test?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    using (Model.KeynerContext db = new Model.KeynerContext())
                    {
                        db.Entry(db.TestSet.FirstOrDefault(t => t.Id == id)).State = System.Data.Entity.EntityState.Deleted;
                        db.SaveChanges();
                        dataGrid.Rows.RemoveAt(e.RowIndex);
                    }
                }
            }
        }
Exemplo n.º 2
0
 public FormNewTest(ref Model.Test test) : this()
 {
     _test = test;
     richTextBoxText.Text             = _test.Text;
     numericUpDownCountMistakes.Value = _test.CountMistakes;
     numericUpDownMaxTime.Value       = _test.MaxTime;
     _modified = true;
 }
Exemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (_test == null)
            {
                _test = new Model.Test();
            }

            _test.Text = "";

            if (numericUpDownCountRepeat.Value > 0)
            {
                for (int i = 0; i < (int)numericUpDownCountRepeat.Value; i++)
                {
                    _test.Text += richTextBoxText.Text + "\n";
                }
            }
            else
            {
                _test.Text = richTextBoxText.Text;
            }
            _test.CountMistakes = (int)numericUpDownCountMistakes.Value;
            _test.MaxTime       = (int)numericUpDownMaxTime.Value;

            List <ValidationResult> validationResults = new List <ValidationResult>();
            ValidationContext       validationContext = new ValidationContext(_test);

            if (!Validator.TryValidateObject(_test, validationContext, validationResults, true))
            {
                string errorMesseges = "";
                foreach (ValidationResult item in validationResults)
                {
                    errorMesseges += item.ErrorMessage + "\n";
                }
                MessageBox.Show(errorMesseges, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            using (Model.KeynerContext db = new Model.KeynerContext())
            {
                if (_modified)
                {
                    db.Entry(_test).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    db.TestSet.Add(_test);
                }
                db.SaveChanges();
                this.DialogResult = DialogResult.Yes;
                this.Close();
            }
        }