using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MatrixCalculator { public partial class NumericInput : Form { public enum NumberType { Integer, Real }; #region Data bool allowExit = true; private NumberType type; public NumberType Type { get { return type; } set { if (value == NumberType.Integer) labelMessage.Text = "Insert an integer number:"; else labelMessage.Text = "Insert a real number:"; type = value; } } public double NumberReal { get { double ret = 0; double.TryParse(inputBox.Text, out ret); return ret; } set { inputBox.Text = value.ToString(); } } public int NumberInt { get { int ret = 0; int.TryParse(inputBox.Text, out ret); return ret; } set { inputBox.Text = value.ToString(); } } #endregion public NumericInput(NumberType t = NumberType.Real) { InitializeComponent(); Type = t; } private void buttonAccept_Click(object sender, EventArgs e) { this.DialogResult = System.Windows.Forms.DialogResult.OK; // Validate input bool ok = true; double val; int vali; if (Type == NumberType.Real) ok = double.TryParse(inputBox.Text, out val); else ok = int.TryParse(inputBox.Text, out vali); // Is everything ok? if (!ok) { allowExit = false; inputBox.BackColor = Color.LightPink; labelMessage.Font = new Font(labelMessage.Font, FontStyle.Bold); } } private void buttonCancel_Click(object sender, EventArgs e) { this.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.Close(); } private void NumericInput_FormClosing(object sender, FormClosingEventArgs e) { if (!allowExit) { allowExit = true; e.Cancel = true; } } #region Paint protected override void OnPaintBackground(PaintEventArgs e) { DynamicLink.Controls.BackgroundGradient.Paint(e.Graphics, new Rectangle(-1, -1, this.Width, this.Height)); } #endregion } }