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

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Market1337, Today, 08:54 AM
        0 responses
        12 views
        0 likes
        Last Post Market1337  
        Started by Bobin, Today, 04:29 AM
        1 response
        13 views
        0 likes
        Last Post bltdavid  
        Started by Lyubomir Kratinov, Today, 06:36 AM
        2 responses
        17 views
        0 likes
        Last Post Lyubomir Kratinov  
        Started by joostb, Today, 12:59 AM
        2 responses
        12 views
        0 likes
        Last Post joostb
        by joostb
         
        Started by Shai Samuel, Today, 02:35 AM
        0 responses
        12 views
        0 likes
        Last Post Shai Samuel  
        Working...
        X