/* LOAD MENU FUNCTIONS */ private void MenuItem_Load_Click(object sender, EventArgs e) { Reset(); OpenFileDialog getUserFile = new OpenFileDialog(); getUserFile.Title = "Load Equation Data"; getUserFile.Multiselect = false; getUserFile.CheckFileExists = true; getUserFile.CheckPathExists = true; string[] fileExtensions = ControlFlow.GetValidFileTypes(); if (fileExtensions != null) { string filter = fileExtensions[0] + "|" + fileExtensions[0]; for (int i = 1; i < fileExtensions.Length; i++) { filter += "|" + fileExtensions[i] + "|" + fileExtensions[i]; } getUserFile.Filter = filter; } if (getUserFile.ShowDialog() == DialogResult.OK) { UpdateLog("Reading from: " + getUserFile.FileName + Environment.NewLine); string[] fileContents = ControlFlow.ControlFile(getUserFile.FileName); if (fileContents != null) { Txt_Equation.Text = fileContents[0]; HandleResults(fileContents[0], fileContents[1]); } } else { UpdateLog("Error: Open File Dialog was closed." + Environment.NewLine); } Txt_Log.AppendText(logMessages); ClearLog(); return; }
/* CALCULATE BUTTON FUNCTION */ private void Btn_Calculate_Click(object sender, EventArgs e) { string equation = ControlFlow.ConditionRawInput(Txt_Equation.Text, false); string variables = ""; List <string> varNames = new List <string>(); string[] delimiters = ControlFlow.GetDelimiters(); Grid_Vars.AllowUserToAddRows = false; foreach (DataGridViewRow var in Grid_Vars.Rows) { if (var.Cells[0].Value != null && var.Cells[1].Value != null && var.Cells[2].Value != null) { if (var.Cells[0].Value.ToString() != "" && var.Cells[1].Value.ToString() != "" && var.Cells[2].Value.ToString() != "") { if (!varNames.Contains(Regex.Replace(var.Cells[0].Value.ToString(), @"\s+", ""))) { variables += var.Cells[0].Value.ToString() + delimiters[1] + var.Cells[1].Value.ToString() + delimiters[1] + var.Cells[2].Value.ToString() + delimiters[0]; varNames.Add(Regex.Replace(var.Cells[0].Value.ToString(), @"\s+", "")); } else { UpdateLog("Warning: Found duplicate variable name (" + Regex.Replace(var.Cells[0].Value.ToString(), @"\s+", "") + "). Removing from the list." + Environment.NewLine); Grid_Vars.Rows.Remove(var); } } } else { Grid_Vars.Rows.Remove(var); } } Grid_Vars.AllowUserToAddRows = true; HandleResults(equation, variables); Txt_Log.AppendText(logMessages); ClearLog(); return; }
/* VARIABLE EXTRACTION FUNCTION */ private void Btn_ExtractVars_Click(object sender, EventArgs e) { string eq = ControlFlow.ConditionRawInput(Txt_Equation.Text, false); string[] variables; string[] varTableRow; bool contains = false; if (eq != "") { variables = ControlFlow.ExtractVariables(eq); if (variables != null) { Txt_Equation.BackColor = System.Drawing.SystemColors.Window; if (variables.Length > 0) { varTableRow = new string[] { "", "", "" }; foreach (string varName in variables) { foreach (DataGridViewRow row in Grid_Vars.Rows) { if (!(row.Cells[0].Value == null)) { if (row.Cells[0].Value.ToString().Contains(varName)) { contains = true; } } } if (!contains) { varTableRow[0] = varName; Grid_Vars.Rows.Add(varTableRow); } contains = false; } Btn_ExtractVars.Enabled = false; } else { Btn_Calculate.Enabled = true; } } else { Txt_Equation.BackColor = System.Drawing.Color.Red; } } else { UpdateLog("Error: Equation field is empty. Please enter an equation into the Equation field to proceed." + Environment.NewLine); Txt_Equation.BackColor = System.Drawing.Color.Red; } Txt_Log.AppendText(logMessages); ClearLog(); return; }