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 ImportCsvWindow : Form { private string csv; private bool allowExit = true; #region Public properties public int StartRow { get; set; } public int StartCol { get; set; } public int EndRow { get; set; } public int EndCol { get; set; } public string Csv{ get { return csv; } set { csv = value; UpdateGrid(); } } public string MatrixName { get { return inputName.Text; } set { inputName.Text = value; } } public MathNet.Numerics.LinearAlgebra.Matrix Matrix { get { return GridViewHelper.GetMatrix(dataGrid); } set { GridViewHelper.PutMatrix(value, dataGrid); } } #endregion private Dictionary separators = new Dictionary(); public ImportCsvWindow() { InitializeComponent(); separators.Add(',', true); separators.Add('\t', false); separators.Add(' ', false); separators.Add(';', false); } public void LoadFile(string filename) { if (System.IO.File.Exists(filename)) Csv = System.IO.File.ReadAllText(filename); } private void UpdateGrid() { List sep = new List(); foreach (var i in separators) if (i.Value) sep.Add(i.Key); // Update data dataGrid.DataSource = CsvParser.GetTable(Csv, sep.ToArray(), checkCombine.Checked); foreach (DataGridViewColumn i in dataGrid.Columns) { i.HeaderText = ""; i.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; } // Update sizes inputStartRow.Value = 0; inputStartColumn.Value = 0; inputEndColumn.Value = inputEndColumn.Maximum = dataGrid.Columns.Count - 1; inputEndRow.Value = inputEndRow.Maximum = dataGrid.Rows.Count - 1; } #region Separators private void checkCombine_CheckedChanged(object sender, EventArgs e) { UpdateGrid(); } private void checkSepComma_CheckedChanged(object sender, EventArgs e) { separators[','] = checkSepComma.Checked; UpdateGrid(); } private void checkSepSemicolon_CheckedChanged(object sender, EventArgs e) { separators[';'] = checkSepComma.Checked; UpdateGrid(); } private void checkSepSpace_CheckedChanged(object sender, EventArgs e) { separators[' '] = checkSepComma.Checked; UpdateGrid(); } private void checkSepOthers_CheckedChanged(object sender, EventArgs e) { List temp = new List(); foreach (var i in separators) if (i.Key != ' ' && i.Key != '\t' && i.Key != ';' && i.Key != ',') temp.Add(i.Key); foreach (var i in temp) separators[i] = false; if (checkSepOthers.Checked) foreach (var i in inputSepOthers.Text) separators[i] = true; UpdateGrid(); } private void checkSepTab_CheckedChanged(object sender, EventArgs e) { separators['\t'] = checkSepComma.Checked; UpdateGrid(); } private void inputSepOthers_TextChanged(object sender, EventArgs e) { List temp = new List(); foreach (var i in separators) if (i.Key != ' ' && i.Key != '\t' && i.Key != ';' && i.Key != ',') temp.Add(i.Key); foreach (var i in temp) separators[i] = false; if (checkSepOthers.Checked) foreach (var i in inputSepOthers.Text) separators[i] = true; UpdateGrid(); } #endregion #region Form buttons private void buttonEntire_Click(object sender, EventArgs e) { StartCol = StartRow = 0; inputEndColumn.Value = dataGrid.Columns.Count - 1; inputEndRow.Value = dataGrid.Rows.Count - 1; this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } private void buttonAccept_Click(object sender, EventArgs e) { // Get numbers StartCol = Convert.ToInt32(inputStartColumn.Value); StartRow = Convert.ToInt32(inputStartRow.Value); EndCol = Convert.ToInt32(inputEndColumn.Value); EndRow = Convert.ToInt32(inputEndRow.Value); // Validate input if (inputName.Text == "") { allowExit = false; MessageBox.Show("Name cannot be empty!", "Error!"); return; } if (EndCol - StartCol <= 0 || EndRow - StartRow <= 0) { allowExit = false; MessageBox.Show("Invalid range, the matrix must have at least one row and one column.", "Error!"); return; } // Trim unneeded rows and columns while (StartCol > 0) { dataGrid.Columns.RemoveAt(0); StartCol--; EndCol--; } while (StartRow > 0) { dataGrid.Rows.RemoveAt(0); StartRow--; EndRow--; } GridViewHelper.Resize(dataGrid, EndCol + 1, EndRow + 1); // Done, finish this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } private void buttonCancel_Click(object sender, EventArgs e) { this.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.Close(); } #endregion private void ImportCsvWindow_FormClosing(object sender, FormClosingEventArgs e) { if (!allowExit) { allowExit = true; e.Cancel = true; } } #region Painting protected override void OnPaintBackground(PaintEventArgs e) { DynamicLink.Controls.BackgroundGradient.Paint(e.Graphics, new Rectangle(-1, -1, this.Width, this.Height)); } #endregion } }