FocusOutOFQSTextBox1
Focus Out Of QuantitySelector TextBox & ChartControl eventunknown
csharp
2 years ago
12 kB
33
Indexable
Never
#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using System.Windows.Controls; // For TextBox //using System.Collections.ObjectModel; // For Flatten() multiple instrument using System.Runtime.InteropServices; using System.Windows.Interop; using NinjaTrader.Gui.NinjaScript; using NinjaTrader.NinjaScript.Indicators; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class myQsTextbox : Indicator { #region Class Level Variables private Position accountPosition; private List<Order> submitOrdersArray, multiOrders; private double currentPtPrice, currentSlPrice; private Order entryBuyMarketOrder, entrySellMarketOrder, profitTargetOrder, stopLossOrder; private Account myAccount; #region QuantitySelector // QS Define a Chart object to refer to the chart on which the indicator resides private Chart chartWindow; NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector; // QS private bool Is_ToolBar_Controls_Added; // QS https://stackoverflow.com/a/9301272/10789707 private int qs; private TextBox quantitySelectorTextBox; #endregion #endregion #region OnStateChange protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Orders Hotkeys Manager."; Name = "myQsTextbox"; Calculate = Calculate.OnEachTick; IsOverlay = true; DisplayInDataBox = false; AccountName = "Sim101"; } else if (State == State.Configure) { } else if (State == State.Historical) { //Call the custom addButtonToToolbar method in State.Historical to ensure it is only done when applied to a chart // -- not when loaded in the Indicators window if (!Is_ToolBar_Controls_Added) Add_Controls_To_Toolbar(); } else if (State == State.DataLoaded) { // Find our account lock (Account.All) myAccount = Account.All.FirstOrDefault(a => a.Name == AccountName); if (ChartControl != null) { ChartControl.Dispatcher.InvokeAsync((Action)(() => { quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown); })); ChartControl.PreviewKeyDown += OnPreviewKeyDown; } } else if (State == State.Terminated) { if (ChartControl != null) { quantitySelector.PreviewKeyDown -= DeleteBackspace_quantitySelector_PreviewKeyDown; quantitySelector.PreviewKeyDown -= NumbersInput_quantitySelector_PreviewKeyDown; quantitySelector.PreviewKeyDown -= FocusOut_quantitySelector_PreviewKeyDown; ChartControl.PreviewKeyDown -= OnPreviewKeyDown; quantitySelector.PreviewKeyDown -= OnPreviewKeyDown; } //Call a custom method to dispose of any leftover objects in State.Terminated DisposeCleanUp(); } } #endregion #region Add Controls To Toolbar / QuantitySelector & PreviewKeyDown Subscriptions private void Add_Controls_To_Toolbar() { // Use this.Dispatcher to ensure code is executed on the proper thread ChartControl.Dispatcher.InvokeAsync((Action)(() => { //Obtain the Chart on which the indicator is configured chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart; if (chartWindow == null) { Print("chartWindow == null"); return; } quantitySelector = new NinjaTrader.Gui.Tools.QuantityUpDown(); //quantitySelector.ValueChanged += On_quantitySelector_ValueChanged; quantitySelector.PreviewKeyDown += DeleteBackspace_quantitySelector_PreviewKeyDown; quantitySelector.PreviewKeyDown += NumbersInput_quantitySelector_PreviewKeyDown; quantitySelector.PreviewKeyDown += FocusOut_quantitySelector_PreviewKeyDown; quantitySelector.PreviewKeyDown += OnPreviewKeyDown; chartWindow.MainMenu.Add(quantitySelector); Is_ToolBar_Controls_Added = true; quantitySelector.Value = 1; })); } #endregion #region QuantitySelector PreviewKeyDown Event Snippets #region DeleteBackspace private void DeleteBackspace_quantitySelector_PreviewKeyDown(object sender, KeyEventArgs p) { NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown; if (p.Key == Key.Delete || p.Key == Key.Back) { p.Handled = true; quantitySelector.Value = 0; } } #endregion #region NumbersInput private void NumbersInput_quantitySelector_PreviewKeyDown(object sender, KeyEventArgs p) { NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown; if ((p.Key >= Key.D0 && p.Key <= Key.D9 || (p.Key >= Key.NumPad0 && p.Key <= Key.NumPad9))) { p.Handled = true; string number = p.Key.ToString(); string newnumber = qs.Value.ToString(); number = number.Replace("NumPad", ""); number = number.Replace("D", ""); int num = int.Parse(newnumber + number); if (qs != null) qs.Value = num; } } #endregion #region FocusOut private void FocusOut_quantitySelector_PreviewKeyDown(object sender, KeyEventArgs p) { Key C = p.Key; if(p.Key == Key.C) { ChildControly ccChildren = new ChildControly(); foreach (object o in ccChildren.GetChildren(quantitySelector, 0)) { if (o.GetType() == typeof(TextBox)) { quantitySelectorTextBox = (TextBox)o; FocusManager.SetFocusedElement(FocusManager.GetFocusScope(quantitySelectorTextBox), null); Keyboard.ClearFocus(); Print("Focus Out"); } } p.Handled = true; } } #endregion #endregion #region OnPreviewKeyDown / FocusOut & EnterBuyMarket Order OR EnterShortmarketOrder private async void OnPreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs p) { await ChartControl.Dispatcher.InvokeAsync((Action) (() => { if(p.Key == Key.D) { #region FocusOut of QS TextBox Keyboard.Focus(sender as ChartControl); #endregion #region Enter BuyMarketOrder TriggerCustomEvent(k => { Print("Buy"); }, null); #endregion } else if(p.Key == Key.F) { #region FocusOut of QS TextBox Keyboard.Focus(sender as ChartControl); #endregion #region Enter SellMarketOrder TriggerCustomEvent(k => { Print("Sell"); }, null); #endregion } p.Handled = true; })); } #endregion #region OnBarUpdate Prints Snippets protected override void OnBarUpdate() { } #endregion #region DisposeCleanUp Snippet private void DisposeCleanUp() { //ChartWindow Null Check if (chartWindow != null) { //Dispatcher used to Assure Executed on UI Thread ChartControl.Dispatcher.InvokeAsync((Action)(() => { if( quantitySelector != null ) chartWindow.MainMenu.Remove(quantitySelector); })); } } #endregion #region Properties [TypeConverter(typeof(NinjaTrader.NinjaScript.AccountNameConverter))] [Display(Name = "Account Name", Description = "Selects The Account (from Ones Available)", Order = 1, GroupName = "#1 Account Selector")] public string AccountName { get; set; } #endregion } #region ChildControly Class / Hotkeys Snippets public class ChildControly { private List<object> lstChildren; public List<object> GetChildren(Visual p_vParent, int p_nLevel) { if (p_vParent == null) { throw new ArgumentNullException("Element {0} is null!", p_vParent.ToString()); } this.lstChildren = new List<object>(); this.GetChildControls(p_vParent, p_nLevel); return this.lstChildren; } private void GetChildControls(Visual p_vParent, int p_nLevel) { int nChildCount = VisualTreeHelper.GetChildrenCount(p_vParent); for (int i = 0; i <= nChildCount - 1; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(p_vParent, i); lstChildren.Add((object)v); if (VisualTreeHelper.GetChildrenCount(v) > 0) { GetChildControls(v, p_nLevel + 1); } } } } #endregion } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private myQsTextbox[] cachemyQsTextbox; public myQsTextbox myQsTextbox() { return myQsTextbox(Input); } public myQsTextbox myQsTextbox(ISeries<double> input) { if (cachemyQsTextbox != null) for (int idx = 0; idx < cachemyQsTextbox.Length; idx++) if (cachemyQsTextbox[idx] != null && cachemyQsTextbox[idx].EqualsInput(input)) return cachemyQsTextbox[idx]; return CacheIndicator<myQsTextbox>(new myQsTextbox(), input, ref cachemyQsTextbox); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.myQsTextbox myQsTextbox() { return indicator.myQsTextbox(Input); } public Indicators.myQsTextbox myQsTextbox(ISeries<double> input ) { return indicator.myQsTextbox(input); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.myQsTextbox myQsTextbox() { return indicator.myQsTextbox(Input); } public Indicators.myQsTextbox myQsTextbox(ISeries<double> input ) { return indicator.myQsTextbox(input); } } } #endregion