using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace TradeIdeas.TIProGUI.Charting { public partial class ModifyParameters : Form, IFont { private List _indicators = new List(); public List Indicators { get { return _indicators; } } private List _indicatorsToRemove = new List(); public List IndicatorsToRemove { get { return _indicatorsToRemove; } } private TiqColumn _indicatorToAdd = null; public TiqColumn IndicatorToAdd { get { return _indicatorToAdd; } } private List _chartAreas = new List(); /// /// Constructor for outside the charting environment. /// /// The indicators. /// To add. public ModifyParameters(List indicators, TiqColumn toAdd) { _indicators = indicators; _chartAreas = null; _indicatorToAdd = toAdd; Font = GuiEnvironment.FontSettings; InitializeComponent(); StartPosition = FormStartPosition.CenterParent; Icon = GuiEnvironment.Icon; BuildGui(); } private bool _scrollToBottom = false; public ModifyParameters(List indicators, List chartAreas, TiqColumn toAdd) { _indicators = indicators; _chartAreas = chartAreas; _indicatorToAdd = toAdd; Font = GuiEnvironment.FontSettings; InitializeComponent(); StartPosition = FormStartPosition.CenterParent; Icon = GuiEnvironment.Icon; // Scroll to the bottom when adding a new indicator. if (_indicatorToAdd != null) _scrollToBottom = true; BuildGui(); } public List GetIndicatorList() { List toReturn = _indicators.ToList(); if (null != _indicatorToAdd) toReturn.Add(_indicatorToAdd); return toReturn; } private void BuildGui() { tableLayoutPanelParameters.Controls.Clear(); tableLayoutPanelParameters.RowStyles.Clear(); tableLayoutPanelParameters.RowCount = GetRowCount() + 1; int currentRowCount = 0; int widestHeaderWidth = 0; int widestValueWidth = 0; Control lastIndicatorControl = null; foreach (TiqColumn indicator in GetIndicatorList()) { Label indicatorLabel = new Label(); indicatorLabel.Text = indicator.ResolvedAbbreviatedName(); indicatorLabel.AutoSize = true; indicatorLabel.Dock = DockStyle.Fill; indicatorLabel.TextAlign = ContentAlignment.MiddleCenter; indicatorLabel.Margin = new Padding(0, 10, 0, 10); indicatorLabel.Font = new Font(Font, FontStyle.Bold); tableLayoutPanelParameters.Controls.Add(indicatorLabel, 0, currentRowCount); RowStyle indicatorRowStyle = new RowStyle(SizeType.AutoSize); tableLayoutPanelParameters.RowStyles.Add(indicatorRowStyle); tableLayoutPanel1.SetColumnSpan(indicatorLabel, 2); currentRowCount++; Label colorsLabel = new Label(); colorsLabel.Text = "Modify Colors"; colorsLabel.AutoSize = true; colorsLabel.Dock = DockStyle.Fill; colorsLabel.TextAlign = ContentAlignment.MiddleRight; colorsLabel.Margin = new Padding(0); tableLayoutPanelParameters.Controls.Add(colorsLabel, 0, currentRowCount); tableLayoutPanelParameters.RowStyles.Add(new RowStyle(SizeType.AutoSize)); Button modifyColorsButton = new Button(); modifyColorsButton.Margin = new Padding(0, 0, 0, 0); modifyColorsButton.Text = "Modify Colors"; modifyColorsButton.AutoSize = true; modifyColorsButton.Tag = indicator; modifyColorsButton.Click += ModifyColorsButton_Click; modifyColorsButton.TabIndex = currentRowCount; tableLayoutPanelParameters.Controls.Add(modifyColorsButton, 1, currentRowCount); currentRowCount++; Label statusLineLabel = new Label(); statusLineLabel.Text = "Show in Status Line"; statusLineLabel.AutoSize = true; statusLineLabel.Dock = DockStyle.Fill; statusLineLabel.TextAlign = ContentAlignment.MiddleRight; statusLineLabel.Margin = new Padding(0, 0, 0, 0); //tableLayoutPanelParameters.Controls.Add(statusLineLabel, 0, currentRowCount); tableLayoutPanelParameters.RowStyles.Add(new RowStyle(SizeType.AutoSize)); StatusCheckBox showInStatusCheckBox = new StatusCheckBox(); showInStatusCheckBox.Checked = indicator.ShowInStatusLine; showInStatusCheckBox.Margin = new Padding(0, 0, 0, 0); showInStatusCheckBox.Tag = indicator; showInStatusCheckBox.TabIndex = currentRowCount; showInStatusCheckBox.AutoSize = true; showInStatusCheckBox.Text = "Show in Status Line"; tableLayoutPanelParameters.Controls.Add(showInStatusCheckBox, 1, currentRowCount); currentRowCount++; Label removeLabel = new Label(); removeLabel.Text = "Remove Indicator"; removeLabel.AutoSize = true; removeLabel.Dock = DockStyle.Fill; removeLabel.TextAlign = ContentAlignment.MiddleRight; removeLabel.Margin = new Padding(0, 0, 0, 0); //tableLayoutPanelParameters.Controls.Add(removeLabel, 0, currentRowCount); tableLayoutPanelParameters.RowStyles.Add(new RowStyle(SizeType.AutoSize)); CheckBox removeCheckBox = new CheckBox(); removeCheckBox.Margin = new Padding(0, 0, 0, 0); removeCheckBox.Tag = indicator; removeCheckBox.TabIndex = currentRowCount; removeCheckBox.AutoSize = true; removeCheckBox.Text = "Remove Indicator"; tableLayoutPanelParameters.Controls.Add(removeCheckBox, 1, currentRowCount); lastIndicatorControl = removeCheckBox; if (null != _chartAreas) { currentRowCount++; Label chartAreaLabel = new Label(); chartAreaLabel.Text = "Chart Area"; chartAreaLabel.AutoSize = true; chartAreaLabel.Dock = DockStyle.Fill; chartAreaLabel.TextAlign = ContentAlignment.MiddleRight; chartAreaLabel.Margin = new Padding(0, 0, 0, 0); tableLayoutPanelParameters.Controls.Add(chartAreaLabel, 0, currentRowCount); tableLayoutPanelParameters.RowStyles.Add(new RowStyle(SizeType.AutoSize)); ComboBox chartAreaComboBox = new ComboBox(); chartAreaComboBox.Font = Font; chartAreaComboBox.Margin = new Padding(0, 0, 0, 0); chartAreaComboBox.Tag = indicator; chartAreaComboBox.TabIndex = currentRowCount; chartAreaComboBox.DropDownStyle = ComboBoxStyle.DropDownList; PopulateChartAreas(chartAreaComboBox, indicator.ChartArea); // Now calculate the correct width for the combobox based on its DropDownWidth. // Make sure we only change width if required. int dropDownWidth = DropDownWidth(chartAreaComboBox); if (dropDownWidth > chartAreaComboBox.Width) chartAreaComboBox.Width = dropDownWidth; tableLayoutPanelParameters.Controls.Add(chartAreaComboBox, 1, currentRowCount); lastIndicatorControl = chartAreaComboBox; if (removeLabel.Width > widestHeaderWidth) widestHeaderWidth = removeLabel.Width; if (chartAreaComboBox.Width > widestValueWidth) widestValueWidth = chartAreaComboBox.Width; } foreach (IndicatorParameter parameter in indicator.Parameters) { RowStyle parameterRowStyle = new RowStyle(SizeType.AutoSize); tableLayoutPanelParameters.RowStyles.Add(parameterRowStyle); currentRowCount++; Label parameterLabel = new Label(); parameterLabel.Text = parameter.Name; parameterLabel.AutoSize = true; parameterLabel.Dock = DockStyle.Fill; parameterLabel.TextAlign = ContentAlignment.MiddleRight; if (parameterLabel.Width > widestHeaderWidth) widestHeaderWidth = parameterLabel.Width; tableLayoutPanelParameters.Controls.Add(parameterLabel, 0, currentRowCount); NumericUpDown parameterUpDown = new NumericUpDown(); parameterUpDown.Minimum = (decimal)parameter.Minimum; parameterUpDown.Maximum = (decimal)parameter.Maximum; parameterUpDown.Value = (decimal)parameter.Value; parameterUpDown.Tag = parameter; parameterUpDown.TabIndex = currentRowCount; if (parameterUpDown.Width > widestValueWidth) widestValueWidth = parameterUpDown.Width; else parameterUpDown.Width = widestValueWidth; // Try to keep controls the same width as the combobox. tableLayoutPanelParameters.Controls.Add(parameterUpDown, 1, currentRowCount); lastIndicatorControl = parameterUpDown; } currentRowCount++; } RowStyle paddingRowStyle = new RowStyle(SizeType.Percent); paddingRowStyle.Height = 10; tableLayoutPanelParameters.RowStyles.Add(paddingRowStyle); if (lastIndicatorControl != null && _scrollToBottom) { panel1.ScrollControlIntoView(lastIndicatorControl); } } // Based on code from https://stackoverflow.com/questions/4842160/auto-width-of-comboboxs-content. private int DropDownWidth(ComboBox myCombo) { int maxWidth = 0; foreach (var obj in myCombo.Items) { int temp = 0; temp = TextRenderer.MeasureText(myCombo.GetItemText(obj), myCombo.Font).Width; if (temp > maxWidth) { maxWidth = temp; } } return maxWidth + SystemInformation.VerticalScrollBarWidth; } private void PopulateChartAreas(ComboBox comboBox, ChartArea selectedChartArea) { comboBox.Items.Clear(); if (null != _chartAreas) { comboBox.DisplayMember = "Tag"; comboBox.Items.Add("New Chart Area"); comboBox.SelectedIndex = 0; foreach (ChartArea chartArea in _chartAreas.ToList()) { comboBox.Items.Add(chartArea); } if (null != selectedChartArea) comboBox.SelectedItem = selectedChartArea; } } private void ModifyColorsButton_Click(object sender, EventArgs e) { Button button = sender as Button; if (null == button) return; TiqColumn indicator = button.Tag as TiqColumn; if (null == indicator) return; IndicatorColorChooser colorChooser = new IndicatorColorChooser(indicator, false); DialogResult result = colorChooser.ShowDialog(); if (result == DialogResult.OK) { } } /// /// Gets how many rows there should be based on number of indicators and parameters for each one. /// /// private int GetRowCount() { int rowCount = 0; int rowsPerIndicator = 5; if (null == _chartAreas) rowsPerIndicator = 4; foreach (TiqColumn indicator in GetIndicatorList()) { rowCount += rowsPerIndicator; rowCount += indicator.Parameters.Count; } return rowCount; } private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } private void buttonOk_Click(object sender, EventArgs e) { ChangeParametersFromForm(); DialogResult = DialogResult.OK; Close(); } private void ChangeParametersFromForm() { _indicatorsToRemove.Clear(); foreach (Control control in tableLayoutPanelParameters.Controls) { if (control as NumericUpDown != null) { NumericUpDown parameterUpDown = control as NumericUpDown; IndicatorParameter parameter = parameterUpDown.Tag as IndicatorParameter; if (null != parameter) parameter.Value = (double)parameterUpDown.Value; } if (null != _chartAreas && control as ComboBox != null) { ComboBox chartAreaComboBox = control as ComboBox; TiqColumn indicator = chartAreaComboBox.Tag as TiqColumn; if (null != indicator) { ChartArea chartArea = null; if (chartAreaComboBox.SelectedItem as ChartArea != null) chartArea = (ChartArea)chartAreaComboBox.SelectedItem; indicator.ChartArea = chartArea; } } if (control as StatusCheckBox != null) { CheckBox showInStatusCheckBox = control as StatusCheckBox; TiqColumn chartIndicator = showInStatusCheckBox.Tag as TiqColumn; if (null != chartIndicator) chartIndicator.ShowInStatusLine = showInStatusCheckBox.Checked; } else if (control as CheckBox != null) { CheckBox removeCheckBox = control as CheckBox; TiqColumn chartIndicator = removeCheckBox.Tag as TiqColumn; if (null != chartIndicator && removeCheckBox.Checked) _indicatorsToRemove.Add(chartIndicator); } } } private class StatusCheckBox: CheckBox { public StatusCheckBox() { } } public void selectTheFont() { Font = GuiEnvironment.FontSettings; } } }