using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using TradeIdeas.ServerConnection; using TradeIdeas.TIProData; using TradeIdeas.XML; using System.Xml; using static TradeIdeas.TIProGUI.GuiEnvironment; namespace TradeIdeas.TIProGUI { public partial class IndexPanel : UserControl { private struct GradientColor { public GradientInfo inf; public Color? color; }; private IndexManager _indexManager; private string _symbol; public IndexPanel(String symbol) { _indexManager = IndexManager.Instance; _indexManager.receiveIndexEvent += _indexManager_receiveIndexEvent; _symbol = symbol; InitializeComponent(); LabelsStayVisible(true); } private void _indexManager_receiveIndexEvent(List data) { if (this.InvokeRequired) { this.BeginInvoke(new MethodInvoker(delegate { List tempData = new List(); tempData = data; IndicesData dat1 = GetSymbolData(tempData,_symbol); if (!String.IsNullOrEmpty(dat1.Symbol)) { try { LabelsStayVisible(true); string name = dat1.Name; double? change = dat1.Change; double? last = dat1.Last; double? percentChange = dat1.PercentChange; double? maxChange = dat1.MaxChange; double? maxPercentChange = dat1.MaxPercentChange; string prefix = ""; string arrow = ""; if (change < 0) { GradientColor gradCol = GetColor(change, maxChange); Color? c = gradCol.color; if (c.HasValue) { Color result = c.Value; GradientInfo inf = gradCol.inf; Color foreColor = GradientInfo.AltColor(result); arrow = System.Net.WebUtility.HtmlDecode("▼");//caret down SetColor(result, foreColor); } } if (change > 0) { GradientColor gradCol = GetColor(change, maxChange); Color? c = gradCol.color; if (c.HasValue) { Color result = c.Value; GradientInfo inf = gradCol.inf; Color foreColor = GradientInfo.AltColor(result); prefix = "+"; arrow = System.Net.WebUtility.HtmlDecode("▲");//caret up SetColor(result, foreColor); } } lblSymbol.Text = name; lblArrow.Text = arrow; lblChange.Text = prefix + String.Format("{0:0.00}", change); lblLast.Text = String.Format("{0:0.00}", last); lblChangePercent.Text = prefix + String.Format("{0:0.00}", percentChange) + "%"; } catch { } } })); } } public string GetSymbol() { return _symbol; } private GradientColor GetColor(double? change,double? maxChange) { GradientInfo info = new GradientInfo(); GradientColor retVal = new GradientColor(); Color? colorVal = null; if (change.HasValue && maxChange.HasValue) { if (change < 0) { double min = -1 * (double)maxChange; double shade = (double)change; info.Add(0, Color.White); info.Add(min, Color.Red); colorVal = info.GetColor(shade); retVal.inf = info; retVal.color = colorVal; } else if (change > 0) { double max = (double)maxChange; double shade = (double)change; info.Add(max, Color.Green); info.Add(0, Color.White); colorVal = info.GetColor(shade); retVal.inf = info; retVal.color = colorVal; } } return retVal; } private void LabelsStayVisible(bool keepVisible) { lblSymbol.Visible = keepVisible; lblLast.Visible = keepVisible; lblChange.Visible = keepVisible; lblChangePercent.Visible = keepVisible; lblArrow.Visible = keepVisible; } private void SetColor(Color c,Color foreColor) { this.BackColor = c; lblSymbol.ForeColor = foreColor; lblLast.ForeColor = foreColor; lblChange.ForeColor = foreColor; lblChangePercent.ForeColor = foreColor; lblArrow.ForeColor = foreColor; } public void ChangeFont(Font font) { lblChange.Font = font; lblChangePercent.Font = font; lblLast.Font = font; lblSymbol.Font = font; lblArrow.Font = font; this.Font = font; } private IndicesData GetSymbolData(List tempList,string symbol) { IndicesData temp = new IndicesData(); foreach (IndicesData dat in tempList.ToList()) { if (dat.Symbol == symbol) { temp = dat; break; } } return temp; } //the below two customized click handlers for this user control inspired from: https://stackoverflow.com/questions/9677062/user-control-click-event-not-working-when-clicking-on-text-inside-control public new event EventHandler Click { add { base.Click += value; foreach (Control control in Controls) { control.Click += value; } } remove { base.Click -= value; foreach (Control control in Controls) { control.Click -= value; } } } public new event MouseEventHandler MouseDoubleClick { add { base.MouseDoubleClick += value; foreach (Control control in Controls) { control.MouseDoubleClick += value; } } remove { base.MouseDoubleClick -= value; foreach (Control control in Controls) { control.MouseDoubleClick -= value; } } } } }