public void displaySolutionForm(CalculatorFormData data) { // Create new SolutionForm instance and pass the form data Form solutionForm = new SolutionForm(data); solutionForm.Show(); }
// Calculate and display solution form private void calculateButton_Click(object sender, EventArgs e) { // Get form values // ... Create struct on stack. CalculatorFormData data = new CalculatorFormData(); // Get the checked radio button foreach (var control in unitsRadioGroup.Controls) { RadioButton radio = control as RadioButton; if (radio != null && radio.Checked && radio.Name == "radioKilogramsMeters") { data.units = Units.Metric; } else if (radio != null && radio.Checked && radio.Name == "radioPoundsFeet") { data.units = Units.Imperial; } else { // default data.units = Units.Metric; } } // Validate email field if (!IsValidEmail(emailField.Text)) { // Display input validation error in message box and inline MessageBox.Show("Please enter a valid email address...", "Error"); emailErrorLabel.Show(); emailLabel.Hide(); return; } else { // Set defaults for email labels emailErrorLabel.Hide(); emailLabel.Show(); data.email = emailField.Text; } // Input is validated by the control itself for the following data.mass1 = Convert.ToInt32(mass1.Value); data.mass2 = Convert.ToInt32(mass2.Value); data.distance = Convert.ToInt32(distance.Value); // Send data struct to solution form displaySolutionForm(data); }
// Calculate Gravitational Force public static double calculateGravitationalForce(CalculatorFormData data) { double F = 0; // Gravitational Constant const double G = 6.6720e-11; // Calculate mass product int massProd = data.mass1 * data.mass2; // Distance squared int distSquared = data.distance ^ 2; // Calculate force F = G * (massProd / distSquared); return(F); }