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.Diagnostics; using System.Windows.Forms; using System.Net.Http; namespace TradeIdeas.TIProGUI { public partial class SymbolLookupCombo : SymbolLookupB { public float FontSize { set { if (value > 0) cboSymbol.Font = new Font("Microsoft San Serif", value, FontStyle.Regular); } } public bool IsFlatStyle { set { if (value) cboSymbol.FlatStyle = FlatStyle.Flat; } } private bool _updateSymbolSelectedOnComboboxSelection = false; private bool _updatingSymbolComboBox = false; public SymbolLookupCombo():base() { InitializeComponent(); cboSymbol.AutoSize = false; // suppress right click menu cboSymbol.ContextMenu = new ContextMenu(); // set event handlers for combobox cboSymbol.GotFocus += cboSymbol_GotFocus; cboSymbol.LostFocus += cboSymbol_LostFocus; } public bool UpdateSymbolSelectedOnComboboxSelection { get { return _updateSymbolSelectedOnComboboxSelection; } set { _updateSymbolSelectedOnComboboxSelection = value; } } /// /// Unsubscribe to events. Call this for cleanup. /// public override void UnsubscribeEvents() { // delete event handlers for combobox cboSymbol.GotFocus -= cboSymbol_GotFocus; cboSymbol.LostFocus -= cboSymbol_LostFocus; base.UnsubscribeEvents(); } /// /// Get/Set the font for the textbox/combobox. /// public new Font TextBoxFont { get => cboSymbol.Font; set => cboSymbol.Font = value; } /// /// Get the height of the textbox/combobox. /// public new int TextBoxHeight => cboSymbol.Height; /// /// Get the width of the textbox/combobox. /// public new int TextBoxWidth => cboSymbol.Width; /// /// Set whether or not the symbol is valid. This will set the background color of the control /// /// Valid symbol. public override void SetValidSymbol(bool valid) { cboSymbol.BackColor = valid ? Color.White : Color.FromArgb(249, 167, 175); } /// /// Automatically sets the height of the control based on the font size of the textbox/combox /// public override void AutoSetHeight() { this.Height = cboSymbol.PreferredHeight; } /// /// Set the focus to the text box control. /// public override void SetTextBoxFocus() { cboSymbol.Focus(); } protected override void SetControlSymbol(string newSymbol) { _updatingSymbolComboBox = true; cboSymbol.Text = newSymbol; // Insert new symbol entries at the top of the list. if (!string.IsNullOrEmpty(newSymbol) && !cboSymbol.Items.Contains(newSymbol)) cboSymbol.Items.Insert(0, newSymbol); _updatingSymbolComboBox = false; } /// /// Gets the symbol in the textbox/combobox. /// public override string GetSymbol() { return cboSymbol.Text; } protected override void autoCompleteListBox_DrawItem(object sender, DrawItemEventArgs e, string searchText) { base.autoCompleteListBox_DrawItem(sender, e, cboSymbol.Text); } private void cboSymbol_GotFocus(object sender, EventArgs e) { if (_autoCompleteShown) _lstSymbolLookup.ClearSelected(); } private void cboSymbol_LostFocus(object sender, EventArgs e) { // Don't execute this code when it lose focus because of the autocomplete showing. if (_autoCompleteShown) return; // Added the same code as pressing the enter key. _showLookup = false; timerKeystroke.Stop(); // call symbol selected event CallSymbolSelectedEvent(((ComboBox)sender).Text); if (_autoCompleteShown) HideAutoComplete(false); } protected override void timerKeystroke_Tick(object sender, EventArgs e, string searchText) { base.timerKeystroke_Tick(sender, e, cboSymbol.Text); } private void cboSymbol_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { _showLookup = false; timerKeystroke.Stop(); // call symbol selected event CallSymbolSelectedEvent(((ComboBox)sender).Text); if (_autoCompleteShown) HideAutoComplete(false); cboSymbol.SelectionStart = 0; cboSymbol.SelectionLength = cboSymbol.Text.Length; } } private void cboSymbol_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= 'a' && e.KeyChar <= 'z') e.KeyChar -= (char)32; else if (e.KeyChar == 13) { e.Handled = true; } } private void cboSymbol_TextChanged(object sender, EventArgs e) { //Debug.WriteLine("Text changed. " + (_dontUpdateLookup ? "Don't Update" : "")); if (_dontUpdateLookup) { if (_selectionChanged) _dontUpdateLookup = _selectionChanged = false; return; } // set backcolor to white if it's not if (cboSymbol.BackColor != Color.White) cboSymbol.BackColor = Color.White; // search for text if (cboSymbol.Text.Length > 0) { timerKeystroke.Stop(); _showLookup = true; timerKeystroke.Start(); } else if (_autoCompleteShown) { _showLookup = false; timerKeystroke.Stop(); HideAutoComplete(true); } else { _showLookup = false; timerKeystroke.Stop(); } } private void cboSymbol_SelectionChangeCommitted(object sender, EventArgs e) { _dontUpdateLookup = true; _selectionChanged = true; } private void cboSymbol_SelectedIndexChanged(object sender, EventArgs e) { if (_updatingSymbolComboBox) return; // call symbol selected event if update is true if (_updateSymbolSelectedOnComboboxSelection && cboSymbol.SelectedItem != null) CallSymbolSelectedEvent(cboSymbol.SelectedItem.ToString()); //Debug.WriteLine("Index changed."); } protected override void ShowPopup() { _popup.Show(cboSymbol, 0, cboSymbol.Height); } protected override void lstSymbolLookup_KeyPress(object sender, KeyPressEventArgs e) { if (Char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)32) // character, digit, or space { cboSymbol.Text += Char.ToUpper(e.KeyChar); } else if (e.KeyChar == (char)8) // backspace { if (cboSymbol.Text.Length > 0) cboSymbol.Text = cboSymbol.Text.Substring(0, cboSymbol.Text.Length - 1); } base.lstSymbolLookup_KeyPress(sender, e); } } }