private void SettingButtonClick(object Sender, EventArgs e) { // Find the corresponding button details. int i = 0; while ((i < settingButtonProperties.Length) && ((settingButtonProperties[i] == null) || (settingButtonProperties[i].button != Sender))) { i++; } if (i >= settingButtonProperties.Length) { throw new Exception("Unable to find setting button properties"); } // Take the appropriate action. Setting setting = settings[i]; switch (setting.Type) { case "openfile": case "savefile": { FileDialogData data = (FileDialogData)settingButtonProperties[i].data; FileDialog dialog = setting.Type == "openfile" ? (FileDialog)(new OpenFileDialog()) : (FileDialog)(new SaveFileDialog()); dialog.FileName = controls[i].Text; dialog.Filter = data.filter; dialog.FilterIndex = data.filterIndex; dialog.InitialDirectory = data.initialDir; if (dialog.ShowDialog() == DialogResult.OK) { controls[i].Text = dialog.FileName; } break; } case "exefile": { FormFileTreeView dialog = new FormFileTreeView(); dialog.FileName = controls[i].Text; if (dialog.ShowDialog() == DialogResult.OK) { controls[i].Text = dialog.FileName; } break; } case "comportin": case "comportout": { FormComSelect dialog = new FormComSelect(); dialog.ComPortSpec = settingButtonProperties[i].data.ToString(); dialog.IsInputPort = setting.Type == "comportin"; if (dialog.ShowDialog() == DialogResult.OK) { settingButtonProperties[i].data = dialog.ComPortSpec; controls[i].Text = GetComPortDescription(settingButtonProperties[i].data.ToString()); } break; } default: throw new Exception("Invalid setting type: " + setting.Type); } }
public FormSettings(Settings settings, SettingsValidator validator) { // // Required for Windows Form Designer support // InitializeComponent(); // Keep track of the validator. this.validator = validator; // This assumes all fonts on the form are the same size. Graphics g = CreateGraphics(); int charWidth = (int)g.MeasureString("H", Font).Width; // Keep a reference to the supplied Settings object. this.settings = settings; // Create a tab page for each settings group. for (int i = 0; i < settings.Count; i++) { Setting setting = settings[i]; int j = 0; while ((j < tabControl.TabPages.Count) && (setting.Group != tabControl.TabPages[j].Text)) { j++; } if (j >= tabControl.TabPages.Count) { TabPage newPage = new TabPage(); newPage.Text = setting.Group; tabControl.TabPages.Add(newPage); } } // Create a scrollable panel on each tab page. controlPanels = new ScrollablePanel[tabControl.TabPages.Count]; for (int i = 0; i < tabControl.TabPages.Count; i++) { controlPanels[i] = new ScrollablePanel(); controlPanels[i].Location = new Point(0, 0); controlPanels[i].Size = tabControl.TabPages[i].ClientSize; tabControl.TabPages[i].Controls.Add(controlPanels[i]); } // Create controls for each setting. controls = new Control[settings.Count]; settingButtonProperties = new SettingButtonProperties[settings.Count]; int[] controlOffset = new int[tabControl.TabPages.Count]; for (int i = 0; i < settings.Count; i++) { Setting setting = settings[i]; // Assemble the parameters for the setting. int j = 0; System.Collections.Hashtable parameters = new Hashtable(); if (setting.Params.Length % 2 != 0) { throw new Exception("Params array does not have an even number of elements"); } while (j < setting.Params.Length) { parameters[setting.Params[j]] = setting.Params[j + 1]; j += 2; } // Find the corresponding tab page and panel. j = 0; while ((j < tabControl.TabPages.Count) && (setting.Group != tabControl.TabPages[j].Text)) { j++; } if (j >= tabControl.TabPages.Count) { throw new Exception("Tab page not found. Should be impossible."); } ScrollablePanel controlPanel = controlPanels[j]; int maxControlWidth = controlPanel.ContentWidth - 2 * marginLeft; // Add the appropriate control. if (setting.Type == "boolean") { // The caption is part of the check box control. CheckBox checkBox = new CheckBox(); checkBox.Text = setting.Caption; checkBox.Width = maxControlWidth; checkBox.Checked = (bool)setting.Value; controls[i] = checkBox; } else { // Display a caption with the corresponding control underneath it. Label caption = new Label(); caption.Left = marginLeft; caption.Top = marginTop + controlOffset[j]; caption.Width = maxControlWidth; caption.Text = setting.Caption; controlOffset[j] += caption.Height; controlPanel.ContentControls.Add(caption); switch (setting.Type) { case "string": case "integer": case "long": case "double": { int maxLength = parameters.Contains("maxlength") ? (int)parameters["maxlength"] : 0; TextBox textBox = new TextBox(); if (maxLength > 0) { textBox.MaxLength = maxLength; textBox.ClientSize = new Size((maxLength + 1) * charWidth, textBox.ClientSize.Height); if (textBox.Width > maxControlWidth) { textBox.Width = maxControlWidth; } } else { textBox.Width = maxControlWidth; } textBox.Text = setting.Value.ToString(); textBox.GotFocus += new EventHandler(textBox_GotFocus); textBox.LostFocus += new EventHandler(textBox_LostFocus); controls[i] = textBox; break; } case "updown": { if (!parameters.Contains("min") || !parameters.Contains("max")) { throw new Exception("Missing 'min' and/or 'max' parameter(s)"); } int maxLength = parameters.Contains("maxlength") ? (int)parameters["maxlength"] : 0; int min = (int)parameters["min"]; int max = (int)parameters["max"]; NumericUpDown upDown = new NumericUpDown(); upDown.Value = (int)setting.Value; upDown.Minimum = min; upDown.Maximum = max; if (maxLength > 0) { upDown.ClientSize = new Size((maxLength + 1) * charWidth + 30, upDown.ClientSize.Height); if (upDown.Width > maxControlWidth) { upDown.Width = maxControlWidth; } } else { upDown.Width = maxControlWidth; } controls[i] = upDown; break; } case "comportin": case "comportout": { Button button = new Button(); button.Text = ".."; button.Width = 20; button.Top = marginTop + controlOffset[j]; button.Left = controlPanel.ContentWidth - marginLeft - button.Width; button.Click += new EventHandler(SettingButtonClick); settingButtonProperties[i] = new SettingButtonProperties(button, setting.Value); controlPanel.ContentControls.Add(button); TextBox textBox = new TextBox(); textBox.ReadOnly = true; textBox.Width = button.Left - marginLeft - 2; textBox.Text = GetComPortDescription(setting.Value.ToString()); controls[i] = textBox; break; } case "combobox": { if (!parameters.Contains("items")) { throw new Exception("Missing 'items' parameter"); } object[] items = (object[])parameters["items"]; ComboBox combo = new ComboBox(); combo.Width = maxControlWidth; for (int k = 0; k < items.Length; k++) { combo.Items.Add(items[k]); } combo.SelectedItem = setting.Value; controls[i] = combo; break; } case "openfile": case "savefile": case "exefile": { object data = null; if (setting.Type != "exefile") { data = new FileDialogData(parameters.Contains("filter") ? (string)parameters["filter"] : "All Files (*.*)|*.*", parameters.Contains("filterindex") ? (int)parameters["filterindex"] : 1, parameters.Contains("initialdir") ? (string)parameters["initialdir"] : ""); } Button button = new Button(); button.Text = ".."; button.Width = 20; button.Top = marginTop + controlOffset[j]; button.Left = controlPanel.ContentWidth - marginLeft - button.Width; button.Click += new EventHandler(SettingButtonClick); settingButtonProperties[i] = new SettingButtonProperties(button, data); controlPanel.ContentControls.Add(button); TextBox textBox = new TextBox(); textBox.Width = button.Left - marginLeft - 2; textBox.Text = setting.Value.ToString(); textBox.GotFocus += new EventHandler(textBox_GotFocus); textBox.LostFocus += new EventHandler(textBox_LostFocus); controls[i] = textBox; break; } default: { throw new Exception("Unknown setting type: " + setting.Type); } } } controls[i].Left = marginLeft; controls[i].Top = marginTop + controlOffset[j]; controlOffset[j] += controls[i].Height + controlSpacing; controlPanel.ContentControls.Add(controls[i]); } // Set the heights of the control panel contents. for (int i = 0; i < controlPanels.Length; i++) { controlPanels[i].ContentHeight = controlOffset[i]; } }
public FormSettings(Settings settings, SettingsValidator validator) { // // Required for Windows Form Designer support // InitializeComponent(); // Keep track of the validator. this.validator = validator; // This assumes all fonts on the form are the same size. Graphics g = CreateGraphics(); int charWidth = (int)g.MeasureString("H", Font).Width; // Keep a reference to the supplied Settings object. this.settings = settings; // Create a tab page for each settings group. for (int i = 0; i < settings.Count; i++) { Setting setting = settings[i]; int j = 0; while ((j < tabControl.TabPages.Count) && (setting.Group != tabControl.TabPages[j].Text)) j++; if (j >= tabControl.TabPages.Count) { TabPage newPage = new TabPage(); newPage.Text = setting.Group; tabControl.TabPages.Add(newPage); } } // Create a scrollable panel on each tab page. controlPanels = new ScrollablePanel[tabControl.TabPages.Count]; for (int i = 0; i < tabControl.TabPages.Count; i++) { controlPanels[i] = new ScrollablePanel(); controlPanels[i].Location = new Point(0, 0); controlPanels[i].Size = tabControl.TabPages[i].ClientSize; tabControl.TabPages[i].Controls.Add(controlPanels[i]); } // Create controls for each setting. controls = new Control[settings.Count]; settingButtonProperties = new SettingButtonProperties[settings.Count]; int[] controlOffset = new int[tabControl.TabPages.Count]; for (int i = 0; i < settings.Count; i++) { Setting setting = settings[i]; // Assemble the parameters for the setting. int j = 0; System.Collections.Hashtable parameters = new Hashtable(); if (setting.Params.Length % 2 != 0) throw new Exception("Params array does not have an even number of elements"); while (j < setting.Params.Length) { parameters[setting.Params[j]] = setting.Params[j + 1]; j += 2; } // Find the corresponding tab page and panel. j = 0; while ((j < tabControl.TabPages.Count) && (setting.Group != tabControl.TabPages[j].Text)) j++; if (j >= tabControl.TabPages.Count) throw new Exception("Tab page not found. Should be impossible."); ScrollablePanel controlPanel = controlPanels[j]; int maxControlWidth = controlPanel.ContentWidth - 2 * marginLeft; // Add the appropriate control. if (setting.Type == "boolean") { // The caption is part of the check box control. CheckBox checkBox = new CheckBox(); checkBox.Text = setting.Caption; checkBox.Width = maxControlWidth; checkBox.Checked = (bool)setting.Value; controls[i] = checkBox; } else { // Display a caption with the corresponding control underneath it. Label caption = new Label(); caption.Left = marginLeft; caption.Top = marginTop + controlOffset[j]; caption.Width = maxControlWidth; caption.Text = setting.Caption; controlOffset[j] += caption.Height; controlPanel.ContentControls.Add(caption); switch (setting.Type) { case "string": case "integer": case "long": case "double": { int maxLength = parameters.Contains("maxlength") ? (int)parameters["maxlength"] : 0; TextBox textBox = new TextBox(); if (maxLength > 0) { textBox.MaxLength = maxLength; textBox.ClientSize = new Size((maxLength + 1) * charWidth, textBox.ClientSize.Height); if (textBox.Width > maxControlWidth) textBox.Width = maxControlWidth; } else { textBox.Width = maxControlWidth; } textBox.Text = setting.Value.ToString(); textBox.GotFocus += new EventHandler(textBox_GotFocus); textBox.LostFocus += new EventHandler(textBox_LostFocus); controls[i] = textBox; break; } case "updown": { if (! parameters.Contains("min") || ! parameters.Contains("max")) throw new Exception("Missing 'min' and/or 'max' parameter(s)"); int maxLength = parameters.Contains("maxlength") ? (int)parameters["maxlength"] : 0; int min = (int)parameters["min"]; int max = (int)parameters["max"]; NumericUpDown upDown = new NumericUpDown(); upDown.Value = (int)setting.Value; upDown.Minimum = min; upDown.Maximum = max; if (maxLength > 0) { upDown.ClientSize = new Size((maxLength + 1) * charWidth + 30, upDown.ClientSize.Height); if (upDown.Width > maxControlWidth) upDown.Width = maxControlWidth; } else { upDown.Width = maxControlWidth; } controls[i] = upDown; break; } case "comportin": case "comportout": { Button button = new Button(); button.Text = ".."; button.Width = 20; button.Top = marginTop + controlOffset[j]; button.Left = controlPanel.ContentWidth - marginLeft - button.Width; button.Click += new EventHandler(SettingButtonClick); settingButtonProperties[i] = new SettingButtonProperties(button, setting.Value); controlPanel.ContentControls.Add(button); TextBox textBox = new TextBox(); textBox.ReadOnly = true; textBox.Width = button.Left - marginLeft - 2; textBox.Text = GetComPortDescription(setting.Value.ToString()); controls[i] = textBox; break; } case "combobox": { if (! parameters.Contains("items")) throw new Exception("Missing 'items' parameter"); object[] items = (object[])parameters["items"]; ComboBox combo = new ComboBox(); combo.Width = maxControlWidth; for (int k = 0; k < items.Length; k++) combo.Items.Add(items[k]); combo.SelectedItem = setting.Value; controls[i] = combo; break; } case "openfile": case "savefile": case "exefile": { object data = null; if (setting.Type != "exefile") { data = new FileDialogData(parameters.Contains("filter") ? (string)parameters["filter"] : "All Files (*.*)|*.*", parameters.Contains("filterindex") ? (int)parameters["filterindex"] : 1, parameters.Contains("initialdir") ? (string)parameters["initialdir"] : ""); } Button button = new Button(); button.Text = ".."; button.Width = 20; button.Top = marginTop + controlOffset[j]; button.Left = controlPanel.ContentWidth - marginLeft - button.Width; button.Click += new EventHandler(SettingButtonClick); settingButtonProperties[i] = new SettingButtonProperties(button, data); controlPanel.ContentControls.Add(button); TextBox textBox = new TextBox(); textBox.Width = button.Left - marginLeft - 2; textBox.Text = setting.Value.ToString(); textBox.GotFocus += new EventHandler(textBox_GotFocus); textBox.LostFocus += new EventHandler(textBox_LostFocus); controls[i] = textBox; break; } default: { throw new Exception("Unknown setting type: " + setting.Type); } } } controls[i].Left = marginLeft; controls[i].Top = marginTop + controlOffset[j]; controlOffset[j] += controls[i].Height + controlSpacing; controlPanel.ContentControls.Add(controls[i]); } // Set the heights of the control panel contents. for (int i = 0; i < controlPanels.Length; i++) controlPanels[i].ContentHeight = controlOffset[i]; }