/// <summary> /// Function that processor can call to get a six digit value from the user /// </summary> private string PromptUserForSixDigitInput() { string inputValue = string.Empty; string errorMessage = $"Please enter a value in the format '+{GetSixInstructionPlaceholder()}'" + $" or '-{GetSixInstructionPlaceholder()}'"; // Create prompt Form inputPrompt = new Form() { Width = 400, Height = 150, FormBorderStyle = FormBorderStyle.FixedDialog, Text = "Input Value", StartPosition = FormStartPosition.CenterScreen }; // Create prompt message label Label promptMessageLabel = new Label() { Left = 10, Top = 20, Width = 290, Text = $"{errorMessage}:" }; inputPrompt.Controls.Add(promptMessageLabel); // Create prompt input textbox TextBox inputValueTextBox = new TextBox() { Left = 300, Top = 20, Width = 75 }; inputPrompt.Controls.Add(inputValueTextBox); // Create event for when form X button is clicked inputPrompt.FormClosing += (sender, e) => { if (inputValue == string.Empty && !runButton.Enabled) { promptMessageLabel.ForeColor = System.Drawing.Color.Red; e.Cancel = true; } }; // Create submit button Button submitButton = new Button() { Text = "Submit", Left = 150, Width = 50, Top = 50, DialogResult = DialogResult.OK }; submitButton.Click += (sender, e) => { string inputValueTextBoxStr = inputValueTextBox.Text.Trim(); if (ValidationEngine.ValidateInstruction(inputValueTextBoxStr) == InstructionError.None) { inputValue = inputValueTextBoxStr; inputPrompt.Close(); } else { promptMessageLabel.ForeColor = System.Drawing.Color.Red; } }; inputPrompt.Controls.Add(submitButton); // Show form inputPrompt.ShowDialog(); // Pass callback function the value the user entered return(inputValue); }
/// <summary> /// Validates instruction lines for the load instructions and edit instructions dialogs /// </summary> /// <param name="instructionLines">The list of lines to validate.</param> /// <returns>An empty string if there were no errors, otherwise, the error.</returns> private string ValidateInstructionsFromDialog(List <string> instructionLines) { // Show error if user entered more than MAX_INSTRUCTION_COUNT instructions if (instructionLines.Count > Settings.Default.MemorySize) { return($"Please enter {Settings.Default.MemorySize} or fewer instructions. You entered {instructionLines.Count}."); } else { // Validate each instruction foreach (string instructionLine in instructionLines) { if (string.IsNullOrWhiteSpace(instructionLine)) { return($"Error: Please do not enter any empty lines."); } else if (instructionLine[0] == '#') { if (!ValidationEngine.ValidateStartLocation(instructionLine.Substring(1, instructionLine.Length - 1))) { return($"Error: One of the '#' instructions is referencing an invalid memory location."); } } else { InstructionError instructionError = ValidationEngine.ValidateInstruction(instructionLine); if (instructionError == InstructionError.InvalidLength) { return($"Error: One of the instructions isn't { Settings.Default.InstructionSize } characters in length."); } else if (instructionError == InstructionError.SignMissing) { return("Error: One of the instructions does not start with a '+' or '-' sign."); } else if (instructionError == InstructionError.InvalidCharacters) { return("Error: One of the instructions contains invalid characters."); } } } // End foreach return(string.Empty); } }
/// <summary> /// Gets program starting locations. /// </summary> /// <returns>A list containing one or two starting locations. If program #2's starting location /// was blank, only one starting location (program 1's) /// is returned. If program #1's starting location was blank, 0 is returned for program #1's /// starting location.</returns> private List <int> GetProgramStartingLocations() { List <int> startingLocations = new List <int>(); if (program1StartLocationTextBox.Text == string.Empty) { startingLocations.Add(0); } else { if (ValidationEngine.ValidateStartLocation(program1StartLocationTextBox.Text)) { startingLocations.Add(Convert.ToInt32(program1StartLocationTextBox.Text)); } else { MessageBox.Show($"Please enter a valid starting location for program 1."); } } if (Settings.Default.MultiThreaded) { if (program2StartLocationTextBox.Text == string.Empty) { startingLocations.Add(0); } else { if (ValidationEngine.ValidateStartLocation(program2StartLocationTextBox.Text)) { startingLocations.Add(Convert.ToInt32(program2StartLocationTextBox.Text)); } else { MessageBox.Show($"Please enter a valid starting location for program 2."); } } } return(startingLocations); }
/// <summary> /// Validates the instructions (i.e. makes sure there are no empty instructions, instructions /// missing digits or signs, or instructions with invalid opcodes) /// </summary> /// <returns>False if there was an instruction validation error; else, true</returns> private bool InstructionsValidated() { string error = string.Empty; string stringFormat = GetStringFormat(); for (int idx = 0; idx < instructionsGridView.Rows.Count; idx++) { string instruction = instructionsGridView[GRID_INSTRUCTION_IDX, idx].Value != null ? instructionsGridView[GRID_INSTRUCTION_IDX, idx].Value.ToString().Trim() : string.Empty; // Don't validate instructions of length 0 if (instruction.Length != 0) { InstructionError instructionError = ValidationEngine.ValidateInstruction(instruction); if (instructionError == InstructionError.InvalidLength) { error = $"Instruction at row { idx.ToString(stringFormat) } needs to be " + $"{ Settings.Default.InstructionSize } characters in length."; } else if (instructionError == InstructionError.SignMissing) { error = $"Instruction at row { idx.ToString(stringFormat) } is missing its sign (+ or -)."; } else if (instructionError == InstructionError.InvalidCharacters) { error = $"Instruction at row { idx.ToString(stringFormat) } contains invalid characters."; } } } if (error != string.Empty) { MessageBox.Show(error, "Error", MessageBoxButtons.OK); return(false); } return(true); }