private void addToolStripMenuItem_Click(object sender, EventArgs e) { char newVariable = CreateVariableDialog.EnterVariable(); if (newVariable != '\0') { // find the index in the variable list at which to add the variable int index = listBoxVariables.SelectedIndex != -1 ? listBoxVariables.SelectedIndex : DataSet.Variables.Length; // check the data set does not already contain this variable foreach (char existingVariable in listBoxVariables.Items) { if (existingVariable == newVariable) { MessageBox.Show("The variable " + newVariable + " already exists.", "Add Variable", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // adds the variable to the data set DataSet.AddVariable( index, newVariable, 0.0); // adds the variable to the list box listBoxVariables.Items.Insert(index, newVariable); listBoxVariables.SelectedIndex = index; } }
/// <summary> /// Prompts the user to enter a variable name. /// </summary> /// <param name="oneKey">Whether the dialog only requires a single key-press of input before automatically closing.</param> /// <param name="defaultChar">The default character displayed to the user in the dialog window.</param> /// <returns>Returns the character entered by the user. If the user cancels the interaction, the null character ('\0') is returned.</returns> public static char EnterVariable(char defaultChar = '?', bool oneKey = true) { CreateVariableDialog dialog = new CreateVariableDialog(defaultChar, oneKey); if (dialog.ShowDialog() == DialogResult.OK) { return(dialog.EnteredChar); } else { return('\0'); } }
private void renameToolStripMenuItem_Click(object sender, EventArgs e) { int selectedIndex = listBoxVariables.SelectedIndex; if (selectedIndex != -1) { // asks the user for the new name for the variable char newName = CreateVariableDialog.EnterVariable(DataSet.Variables[selectedIndex]); if (newName != '\0') { // renames the variable as long as the user did not close the dialog // (ie. when null character is returned) DataSet.Variables[selectedIndex] = newName; listBoxVariables.Items.RemoveAt(selectedIndex); listBoxVariables.Items.Insert(selectedIndex, newName.ToString()); } } }
/// <summary> /// Initializes the handlers for the buttons on the form that add the appropriate event handlers /// and keyboard shortcut handlers for all of the buttons on the form. This uses the /// <see cref="CreateTokenButton"/> method to create the event handlers. /// </summary> private void InitializeButtons() { #region Digits CreateTokenButton(button0, () => new DigitToken(0), "0 key", Keys.D0); CreateTokenButton(button1, () => new DigitToken(1), "1 key", Keys.D1); CreateTokenButton(button2, () => new DigitToken(2), "2 key", Keys.D2); CreateTokenButton(button3, () => new DigitToken(3), "3 key", Keys.D3); CreateTokenButton(button4, () => new DigitToken(4), "4 key", Keys.D4); CreateTokenButton(button5, () => new DigitToken(5), "5 key", Keys.D5); CreateTokenButton(button6, () => new DigitToken(6), "6 key", Keys.D6); CreateTokenButton(button7, () => new DigitToken(7), "7 key", Keys.D7); CreateTokenButton(button8, () => new DigitToken(8), "8 key", Keys.D8); CreateTokenButton(button9, () => new DigitToken(9), "9 key", Keys.D9); CreateTokenButton(buttonDecimalPoint, () => new SymbolicToken(SymbolicToken.SymbolicType.DecimalPoint), ". key", Keys.OemPeriod); #endregion #region Basic Operations CreateTokenButton(buttonAdd, () => new OperationToken(OperationToken.OperationType.Add), "Plus Key", Keys.Oemplus, Keys.Shift); CreateTokenButton(buttonSubtract, () => new OperationToken(OperationToken.OperationType.Subtract), "Minus Key", Keys.OemMinus); CreateTokenButton(buttonMultiply, () => new OperationToken(OperationToken.OperationType.Multiply), "Shift-8 (*)", Keys.D8, Keys.Shift); CreateTokenButton(buttonDivide, () => new OperationToken(OperationToken.OperationType.Divide), "Backslash", Keys.Oem5); #endregion #region Roots CreateTokenButton(buttonRoot, () => new RootToken(), "Ctrl-Shift-R", Keys.R, Keys.Control | Keys.Shift); CreateTokenButton(buttonSqrt, () => { var token = new RootToken(); token.Power.Add(new DigitToken(2)); return(token); }, "Ctrl-R", Keys.R, Keys.Control); #endregion #region Trig CreateTokenButton(buttonSin, () => new FunctionToken("sin")); CreateTokenButton(buttonCos, () => new FunctionToken("cos")); CreateTokenButton(buttonTan, () => new FunctionToken("tan")); CreateTokenButton(buttonArcsin, () => new FunctionToken("sin`")); CreateTokenButton(buttonArccos, () => new FunctionToken("cos`")); CreateTokenButton(buttonArctan, () => new FunctionToken("tan`")); #endregion #region Logs CreateTokenButton(buttonLogN, () => new LogToken(), "Ctrl-L", Keys.L, Keys.Control); CreateTokenButton(buttonLogE, () => { var token = new LogToken(); token.Base.Add(new ConstantToken(ConstantToken.ConstantType.E)); return(token); }, "Ctrl-Shift-L", Keys.L, Keys.Shift | Keys.Control); CreateTokenButton(buttonLog10, () => { var token = new LogToken(); token.Base.Add(new DigitToken(1)); token.Base.Add(new DigitToken(0)); return(token); }); #endregion #region Exponents CreateTokenButton(buttonExp, () => new ExpToken(), "Shift-6 (^)", Keys.D6, Keys.Shift); CreateTokenButton(buttonSquare, () => { var token = new ExpToken(); token.Power.Add(new DigitToken(2)); return(token); }, "Alt-2", Keys.D2, Keys.Alt); CreateTokenButton(buttonCube, () => { var token = new ExpToken(); token.Power.Add(new DigitToken(3)); return(token); }, "Alt-3", Keys.D3, Keys.Alt); CreateTokenButton(buttonReciprocate, () => { var token = new ExpToken(); token.Power.Add(new OperationToken(OperationToken.OperationType.Subtract)); token.Power.Add(new DigitToken(1)); return(token); }, "Alt-Minus", Keys.OemMinus, Keys.Alt); #endregion #region Constants CreateTokenButton(buttonPi, () => new ConstantToken(ConstantToken.ConstantType.Pi), "P", Keys.P); CreateTokenButton(buttonE, () => new ConstantToken(ConstantToken.ConstantType.E), "E", Keys.E); #endregion #region Misc CreateTokenButton(buttonFraction, () => new FractionToken(), "Forward-slash", Keys.OemQuestion); CreateTokenButton(buttonAbsolute, () => new AbsoluteToken(), "|", Keys.Oem5, Keys.Shift); CreateTokenButton(buttonBracket, () => new FunctionToken(""), "(", Keys.D9, Keys.Shift); // CreateExpressionButton(buttonComma, () => new SymbolicToken(SymbolicToken.SymbolicType.Comma), ",", Keys.Oemcomma); CreateTokenButton(buttonPercent, () => new SymbolicToken(SymbolicToken.SymbolicType.Percent), "%", Keys.D5, Keys.Shift); CreateTokenButton(buttonSymbolicExp, () => new SymbolicToken(SymbolicToken.SymbolicType.Exp10), "Ctrl-E", Keys.E, Keys.Control); #endregion CreateTokenButton(buttonXVariable, () => new VariableToken('x'), "X", Keys.X); CreateTokenButton(buttonYVariable, () => new VariableToken('y'), "Y", Keys.Y); CreateTokenButton(buttonCustomVariable, () => { // creates an EnterVariable dialog to allow the user to enter any variable // character they want (that isn't 'x' or 'y') char customVariable = CreateVariableDialog.EnterVariable(); if (customVariable != '\0') { return(new VariableToken(customVariable)); } else { return(null); } }, "Hash", Keys.Oem7); CreateTokenButton(buttonEquals, () => new SymbolicToken(SymbolicToken.SymbolicType.Equals), "Equals", Keys.Oemplus); }