private void OKButton_Click(object sender, RoutedEventArgs e) { if (UsingModel) { SelectedModel = (string)ModelSelection.SelectedItem; } for (int n = 0; n < Parameters.Count; n++) { double newValue = 0; if (!double.TryParse(ValueBoxes[n].Text, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out newValue)) { MessageBox.Show(Parameters[n].Title + " must be numeric."); return; } ; if ((newValue == 0) && (!Parameters[n].AllowZero)) { MessageBox.Show(Parameters[n].Title + " cannot be zero."); return; } if ((newValue < 0) && (!Parameters[n].AllowNegative)) { MessageBox.Show(Parameters[n].Title + " cannot be negative."); return; } Quantity.Prefix newPrefix = UnitBoxes[n].SelectedIndex * 3 + Quantity.Prefix.Pico; Parameters[n].SetValueWithPrefix(newValue, newPrefix); } WasCancelled = false; this.Close(); }
//Add a quantity to the properties dialog public void AddQuantity(Quantity quantity) { Parameters.Add(quantity); //Add another row to the parameters grid CustomGrid.RowDefinitions.Add(new RowDefinition()); //Add the label for the parameter Label paramLabel = new Label(); paramLabel.Content = quantity.Title; paramLabel.HorizontalAlignment = HorizontalAlignment.Left; paramLabel.VerticalAlignment = VerticalAlignment.Center; Grid.SetRow(paramLabel, nextRow); Grid.SetColumn(paramLabel, 0); CustomGrid.Children.Add(paramLabel); //Determine the current value with prefix double val = 0; Quantity.Prefix pref = Quantity.Prefix.None; quantity.GetValueWithPrefix(ref val, ref pref); //Add the textbox for the value itself TextBox valueBox = new TextBox(); valueBox.Text = val.ToString(); valueBox.HorizontalAlignment = HorizontalAlignment.Left; valueBox.VerticalAlignment = VerticalAlignment.Center; Grid.SetRow(valueBox, nextRow); Grid.SetColumn(valueBox, 1); ValueBoxes.Add(valueBox); valueBox.Width = 80; CustomGrid.Children.Add(valueBox); //Add the units dropdown box ComboBox unitBox = new ComboBox(); unitBox.HorizontalAlignment = HorizontalAlignment.Left; unitBox.VerticalAlignment = VerticalAlignment.Center; Grid.SetRow(unitBox, nextRow); Grid.SetColumn(unitBox, 2); int index = 0; //Add all possible prefixes into the dropdown box for (Quantity.Prefix p = Quantity.Prefix.Pico; p <= Quantity.Prefix.Giga; p += 3) { unitBox.Items.Add(Quantity.PrependPrefix(quantity.Unit, p)); if (p == pref) { unitBox.SelectedIndex = index; } index++; } UnitBoxes.Add(unitBox); CustomGrid.Children.Add(unitBox); //Increment the counter specifiying the number of the next row nextRow++; }