Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Stealing text input focus from a chart window

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Stealing text input focus from a chart window

    Hi all, I'm trying to add a TextBox control to an indicator which appears on the chart. However, I'm having trouble keeping the 'Symbol Selection Dropdown' overlay from popping up when a key stroke is entered. I can keep the keyboard focus within the textbox, but am unsure as to how I can prevent this 'symbol input' overlay page from appearing at the first key stroke. Any simple solution?

    Thanks a lot for your time,
    Nicholas
    Last edited by NicholasJoannette; 07-30-2021, 12:30 AM.

    #2
    After some better searching I found a great answer in this thread.

    Thanks a lot!

    Comment


      #3
      I tried the code out in that thread. When I type the 1 character or 2 character I end up with D1 or D2 inserted in the text box. I haven't yet tried the longer complicated code at the end of that referenced thread.

      Further, in reviewing the code, this entire process seems like a rather hacked together way of handling this. The keys are converted to strings only to then be processed differently?

      Further the entire process of editing in the window needs to be reproduced here when the text box is already capable of doing this work.

      Makes me wonder what the quantity text field on the chart trader does to handle this.
      Last edited by ntbone; 05-11-2022, 11:16 PM.

      Comment


        #4
        I have revised the code provided in the referenced thread.

        Code:
        void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        
            // handle the keydown event for the text box,					
            var textBoxSender = (TextBox)sender;
            var textBoxText = textBoxSender.Text;
        
            switch(e.Key)
            {
                case Key.Space:
                    textBoxText += " ";
                    break;
                case Key.Back:
                    if(textBoxSender.Text.Length > 0)
                    {
                        var index = textBoxSender.SelectionStart;
        
                        if(textBoxSender.SelectedText != "")
                        {							
                            textBoxSender.Text = textBoxSender.Text.Replace(textBoxSender.Text.Substring(textBoxSender.SelectionStart, textBoxSender.SelectionLength), "");
                            textBoxSender.SelectionStart = index;
                            return;
                        }
        
                        if(index == 0)
                        {
                            return;
                        }
        
                        if(index <= textBoxSender.Text.Length)
                        {
                            var lengthOfString = textBoxSender.Text.Length;
                            var firstHalf = textBoxSender.Text.Substring(0, index - 1);
                            var secondHalf = textBoxSender.Text.Substring(index, lengthOfString- index);
                            textBoxText = firstHalf + secondHalf;
                            textBoxSender.Text = textBoxText;
                            textBoxSender.SelectionStart = index - 1;
                        }
                    }
                    return;
                case Key.Delete:
                    if(textBoxSender.Text.Length > 0)
                    {
                        var index = textBoxSender.SelectionStart;
        
                        if(textBoxSender.SelectedText != "")
                        {							
                            textBoxSender.Text = textBoxSender.Text.Replace(textBoxSender.Text.Substring(textBoxSender.SelectionStart, textBoxSender.SelectionLength), "");
                            textBoxSender.SelectionStart = index;
                            return;
                        }
        
                        if(index < textBoxSender.Text.Length)
                        {
                            var firstHalf = textBoxSender.Text.Substring(0, index);
                            var secondHalf = textBoxSender.Text.Substring(firstHalf.Length+1);
                            textBoxText = firstHalf + secondHalf;
                            textBoxSender.Text = textBoxText;
                            textBoxSender.SelectionStart = index;
                        }
                    }
                    return;
                case Key.Capital:
                    return;
                case Key.Left:
                    if(textBoxSender.SelectionStart > 0)
                    {
                        textBoxSender.SelectionStart = textBoxSender.SelectionStart - 1;
                    }
                    return;
                case Key.Right:
                    if(textBoxSender.SelectionStart < textBoxSender.Text.Length)
                    {
                        textBoxSender.SelectionStart = textBoxSender.SelectionStart + 1;
                    }
                    return;
                default:
                    {
                        int index = 0;
                        if(textBoxSender.Text.Length > 0)
                        {
                            index = textBoxSender.SelectionStart;
                        }
        
                        var firstHalf = "";
                        var secondHalf = "";
                        if(index < textBoxSender.Text.Length)
                        {
                            firstHalf = textBoxSender.Text.Substring(0, index);
                            secondHalf = textBoxSender.Text.Substring(firstHalf.Length);
        
                        }
                        else if(index != 0)
                        {
                            firstHalf = textBoxSender.Text.Substring(0, index);
                        }
        
                        var toAdd = string.Empty;
        
                        if((Keyboard.IsKeyToggled(Key.CapsLock) || (Keyboard.IsKeyDown(Key.LeftShift) ||
                            Keyboard.IsKeyDown(Key.RightShift))) && e.Key.ToString().Length == 1)
                        {
                            toAdd = e.Key.ToString();
                        }
                        else if(e.Key.ToString().Length == 1)
                        {
                            toAdd = e.Key.ToString().ToLower();
                        }
                        else if(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
                        {
                            switch(e.Key)
                            {
                                case Key.D0:
                                    toAdd = ")";
                                    break;
                                case Key.D1:
                                    toAdd = "!";
                                    break;
                                case Key.D2:
                                    toAdd = "@";
                                    break;
                                case Key.D3:
                                    toAdd = "#";
                                    break;
                                case Key.D4:
                                    toAdd = "$";
                                    break;
                                case Key.D5:
                                    toAdd = "%";
                                    break;
                                case Key.D6:
                                    toAdd = "^";
                                    break;
                                case Key.D7:
                                    toAdd = "&";
                                    break;
                                case Key.D8:
                                    toAdd = "*";
                                    break;
                                case Key.D9:
                                    toAdd = "(";
                                    break;
                                case Key.OemSemicolon:
                                    toAdd = ":";
                                    break;
                            }
                        }
                        else
                        {
                            switch(e.Key)
                            {
                                case Key.D0:
                                    toAdd = "0";
                                    break;
                                case Key.D1:
                                    toAdd = "1";
                                    break;
                                case Key.D2:
                                    toAdd = "2";
                                    break;
                                case Key.D3:
                                    toAdd = "3";
                                    break;
                                case Key.D4:
                                    toAdd = "4";
                                    break;
                                case Key.D5:
                                    toAdd = "5";
                                    break;
                                case Key.D6:
                                    toAdd = "6";
                                    break;
                                case Key.D7:
                                    toAdd = "7";
                                    break;
                                case Key.D8:
                                    toAdd = "8";
                                    break;
                                case Key.D9:
                                    toAdd = "9";
                                    break;
                                case Key.NumPad0:
                                    toAdd = "0";
                                    break;
                                case Key.NumPad1:
                                    toAdd = "1";
                                    break;
                                case Key.NumPad2:
                                    toAdd = "2";
                                    break;
                                case Key.NumPad3:
                                    toAdd = "3";
                                    break;
                                case Key.NumPad4:
                                    toAdd = "4";
                                    break;
                                case Key.NumPad5:
                                    toAdd = "5";
                                    break;
                                case Key.NumPad6:
                                    toAdd = "6";
                                    break;
                                case Key.NumPad7:
                                    toAdd = "7";
                                    break;
                                case Key.NumPad8:
                                    toAdd = "8";
                                    break;
                                case Key.NumPad9:
                                    toAdd = "9";
                                    break;
                                case Key.Divide:
                                    toAdd = "/";
                                    break;
                                case Key.Multiply:
                                    toAdd = "*";
                                    break;
                                case Key.Subtract:
                                    toAdd = "-";
                                    break;
                                case Key.Add:
                                    toAdd = "+";
                                    break;
                                case Key.Decimal:
                                case Key.OemPeriod:
                                    toAdd = ".";
                                    break;
                                case Key.OemMinus:
                                    toAdd = "-";
                                    break;
                                case Key.OemPlus:
                                    toAdd = "+";
                                    break;
                                case Key.OemOpenBrackets:
                                    toAdd = "[";
                                    break;
                                case Key.OemCloseBrackets:
                                    toAdd = "]";
                                    break;
                                case Key.OemSemicolon:
                                    toAdd = ";";
                                    break;
                            }
        
                        }
        
                        if(textBoxSender.SelectionLength == 0)
                        {
                            textBoxText = firstHalf + toAdd + secondHalf;
                            textBoxSender.Text = textBoxText;
                        }
                        else
                        {
                            textBoxSender.Text = textBoxSender.Text.Replace(textBoxSender.Text.Substring(textBoxSender.SelectionStart, textBoxSender.SelectionLength), toAdd);
                        }
        
                        if(toAdd != string.Empty)
                        {
                            textBoxSender.SelectionStart = index + 1;
                        }
                        else
                        {
                            textBoxSender.SelectionStart = index;
                        }
                    }
                    break;
            }
        }

        Comment


          #5
          Hi ntbobne,

          This is gr8 ! many thanks for the code !!
          Seems to me that this is working for Englisch Keyboard layout, what if i use a German Keyboard, or any other language. Can i just set the type of keyboard at first before looking for a particular key? How would I cater for other languages best?

          Many thanks for any input !

          Comment


            #6
            After a quick review of the code, the default switch statement may need additional handling for any punctuation or accent characters for Germain or other languages to be supported. Its also possible that the punctuation characters that show up there are incorrect and would need to be mapped differently. I would recommend putting the code for the default case statement perhaps in its own function, check the language for the keyboard in use and then have a function to handle each desired language.

            Comment


              #7
              Where can i fond more information on the Key-mapping. how do i know what for example a Key.D6 represents on my keyboard? I can see for example the Key.OemSemicolon in your code but when i hit my "semicolon key on the German keyboard" i get nothing; so i guess, i need to use another mapping.

              Comment


                #8
                The PreviewKeyDown event handler above is from WPF framework. WPF is the UI framework that NinjaTrader is built off of. Documentation for it can be found
                https://learn.microsoft.com/en-us/do...owsdesktop-7.0

                This link gives a more general overview of the key event handlers.
                https://learn.microsoft.com/en-us/do...owsdesktop-7.0

                This is the documentation for the KeyEventArgs
                https://learn.microsoft.com/en-us/do...owsdesktop-7.0

                And finally this
                https://learn.microsoft.com/en-us/do...input-key-left
                contains documentation about values for Key.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by rtwave, 04-12-2024, 09:30 AM
                4 responses
                30 views
                0 likes
                Last Post rtwave
                by rtwave
                 
                Started by yertle, Yesterday, 08:38 AM
                7 responses
                29 views
                0 likes
                Last Post yertle
                by yertle
                 
                Started by bmartz, 03-12-2024, 06:12 AM
                2 responses
                22 views
                0 likes
                Last Post bmartz
                by bmartz
                 
                Started by funk10101, Today, 12:02 AM
                0 responses
                6 views
                0 likes
                Last Post funk10101  
                Started by gravdigaz6, Yesterday, 11:40 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X