/// <summary> /// Add an entry consisting of a desciption and a DropDownList. /// /// The DropDownList will use supplied textValues to display the items with. /// </summary> /// <param name="description">The description.</param> /// <param name="values">The values.</param> /// <param name="textValues">The text values.</param> /// <param name="defaultValue">The default value.</param> public void AddDropDownListEntry(String description, IList values, IList textValues, Object defaultValue) { if (values.Count != textValues.Count) { throw new Exception("Two supplied arraylists to method AddDropDownListEntry have different Counts."); } InputFormDropDownListEntry inputFormDropDownListEntry = new InputFormDropDownListEntry(description, values, textValues, defaultValue); entries.Add(inputFormDropDownListEntry); }
/// <summary> /// Called when the OK button is pressed. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event arguments.</param> private void buttonOK_Click(object sender, System.EventArgs e) { foreach (InputFormEntry inputFormEntry in this.entries) { if (inputFormEntry is InputFormTextBoxEntry) { InputFormTextBoxEntry inputFormTextBoxEntry = inputFormEntry as InputFormTextBoxEntry; inputFormTextBoxEntry.Value = inputFormTextBoxEntry.TextBox.Text; inputFormTextBoxEntry.TextValue = inputFormTextBoxEntry.TextBox.Text; } else if (inputFormEntry is InputFormDropDownListEntry) { InputFormDropDownListEntry inputFormDropDownListEntry = inputFormEntry as InputFormDropDownListEntry; Object selectedItem = inputFormDropDownListEntry.ComboBox.SelectedItem; if (selectedItem == null) { inputFormDropDownListEntry.Value = null; inputFormDropDownListEntry.TextValue = ""; } else { inputFormDropDownListEntry.Value = (selectedItem as InputFormValueAndRepresentation).Value; inputFormDropDownListEntry.TextValue = (selectedItem as InputFormValueAndRepresentation).ValueRepresentation; } } else { // Do nothing. } } this.textOnButtonPressed = (sender as Control).Name; Close(); }
/// <summary> /// Put the controls on the Form. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event arguments.</param> private void InputForm_Load(object sender, System.EventArgs e) { int nextYPosition = this.topMargin; int newClientWidth = this.leftMargin + this.EntryDescriptionWidth + this.horizontalSpaceBetweenControls + this.EntryValueWidth + this.rightMargin; Text = this.caption; // Add the description if it is set. if (this.description.Length > 0) { TextBox descriptionTextBox = new TextBox(); this.description = this.description.Replace("\n\r", "\n"); this.description = this.description.Replace("\r\n", "\n"); this.description = this.description.Replace("\r", "\n"); String[] lines = this.description.Split(new Char[] { '\n' }); descriptionTextBox.Lines = lines; descriptionTextBox.Multiline = true; descriptionTextBox.ReadOnly = true; int numberOfLinesDisplayed = Math.Min(4, lines.Length) + 1; int descriptionHeight = (numberOfLinesDisplayed * (descriptionTextBox.Font.Height + 2)); descriptionTextBox.ClientSize = new Size(this.descriptionWidth, descriptionHeight); descriptionTextBox.Location = new Point(this.leftMargin, this.topMargin); if (lines.Length > numberOfLinesDisplayed) { descriptionTextBox.ScrollBars = ScrollBars.Both; } else { descriptionTextBox.ScrollBars = ScrollBars.Horizontal; } nextYPosition += descriptionHeight + this.verticalSpaceBetweenControls + verticalSpaceBetweenControls; descriptionTextBox.TabIndex = 2; Controls.Add(descriptionTextBox); } // Add the image if it is added. // ONLY SUPPORT ONE IMAGE FOR NOW. if (this.images.Count > 0) { nextYPosition += this.verticalSpaceBetweenControls; Image image = this.images[0] as Image; PictureBox pictureBox = new PictureBox(); pictureBox.Location = new Point((newClientWidth - image.Size.Width) / 2, nextYPosition); pictureBox.SizeMode = PictureBoxSizeMode.AutoSize; pictureBox.Image = image; pictureBox.BorderStyle = BorderStyle.Fixed3D; Controls.Add(pictureBox); nextYPosition += pictureBox.Size.Height + (this.verticalSpaceBetweenControls * 2); } // Add all the entries. foreach (InputFormEntry inputFormEntry in this.entries) { // Add the description label of the entry. Label label = new Label(); label.Text = inputFormEntry.Description; label.Size = new Size(EntryDescriptionWidth, verticalSizeEntryControls); label.Location = new Point(this.leftMargin, nextYPosition); this.Controls.Add(label); // Add the value control of the entry. Size sizeValueControl = new Size(EntryValueWidth, this.verticalSizeEntryControls); Point locationValueControl = new Point(this.leftMargin + this.EntryDescriptionWidth + this.horizontalSpaceBetweenControls, nextYPosition); if (inputFormEntry is InputFormTextBoxEntry) { // Add the textbox. InputFormTextBoxEntry inputFormTextBoxEntry = inputFormEntry as InputFormTextBoxEntry; TextBox textBox = new TextBox(); textBox.Text = inputFormTextBoxEntry.TextValue; textBox.ReadOnly = inputFormTextBoxEntry.ReadOnly; textBox.Size = sizeValueControl; textBox.Location = locationValueControl; inputFormTextBoxEntry.TextBox = textBox; this.Controls.Add(textBox); } else if (inputFormEntry is InputFormDropDownListEntry) { // Add the combobox with style DropDownList. InputFormDropDownListEntry inputFormDropDownListEntry = inputFormEntry as InputFormDropDownListEntry; ArrayList theDropDownListItems = new ArrayList(); InputFormValueAndRepresentation itemToSelect = null; for (int index = 0; index < inputFormDropDownListEntry.Values.Count; index++) { InputFormValueAndRepresentation newItem = new InputFormValueAndRepresentation(inputFormDropDownListEntry.Values[index], inputFormDropDownListEntry.TextValues[index] as String); if (newItem.Value == inputFormDropDownListEntry.Value) { itemToSelect = newItem; } theDropDownListItems.Add(newItem); } ComboBox comboBox = new ComboBox(); comboBox.Items.AddRange(theDropDownListItems.ToArray()); if (itemToSelect != null) { comboBox.SelectedItem = itemToSelect; } comboBox.Size = sizeValueControl; comboBox.Location = locationValueControl; comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; inputFormDropDownListEntry.ComboBox = comboBox; this.Controls.Add(comboBox); } else { // Do nothing. } nextYPosition += this.verticalSizeEntryControls + this.verticalSpaceBetweenControls; } // Add the buttons this.buttonControls = new ArrayList(); Graphics graphics = CreateGraphics(); int rightSideButtonXPosition = this.leftMargin + this.EntryDescriptionWidth + this.horizontalSpaceBetweenControls + this.EntryValueWidth; for (int index = 0; index < this.textOnButtons.Count; index++) { Button button = new Button(); String textOnButton = this.textOnButtons[index]; int minimumWidthNeededForButton = graphics.MeasureString(textOnButton, button.Font).ToSize().Width; int buttonWidth = Math.Max(75, minimumWidthNeededForButton + 16); button.Location = new Point(rightSideButtonXPosition - buttonWidth, nextYPosition); rightSideButtonXPosition -= (buttonWidth + this.horizontalSpaceBetweenControls); button.Name = textOnButton; button.ClientSize = new System.Drawing.Size(buttonWidth, 22); button.Text = textOnButton; button.Click += new System.EventHandler(this.buttonOK_Click); button.TabIndex = index + 1; this.Controls.Add(button); this.buttonControls.Add(button); if (index == 0) { button.Focus(); } } graphics.Dispose(); // Adjust the minimum and maximum size of the form. int newClientHeight = nextYPosition + 22 + this.bottomMargin; ClientSize = new Size(newClientWidth, newClientHeight); this.MinimumSize = Size; this.MaximumSize = Size; }