Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding TextBox to 'SampleWPFModifications.cs'

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

    Adding TextBox to 'SampleWPFModifications.cs'

    Hello,
    I'm new to WPF. I am attempting to modify the indicator 'SampleWPFModifications.cs'. My goal is to place 2 TextBlocks and 2 TextBoxes on the ChartTrader. In the attached screenshot the controls are showing. When I try to type in either textbox the Instrument Selection box appears. I added the code from 'TextBox.cs' that addresses the PreviewKeyDown event.
    Code:
                else if (State == State.Historical)
                {
                    // Because we're dealing with UI elements, we need to use the Dispatcher which created the object
                    // in order for us to update the contents that are created on the main UI thread
                    Dispatcher.Invoke((() =>
                    {
                        textBox = new TextBox();
    
                        textBox.PreviewKeyDown += TextBox_PreviewKeyDown;
                    
                        UserControlCollection.Add(textBox);
                    }));
                }
            private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                // this is pretty dumb, but should get you started
                // might want to validate this by using SHIFT or something
                // so Overlay Instrument Selector isn't unusable...
                TextBox textBoxSender = (TextBox) sender;
                textBoxSender.Text += e.Key.ToString();
                // handle the keydown event for the text box
                e.Handled = true;
            }​
    ​
    That stops the Instrument Selector box from appearing. I can type into the TextBox but none of the characters I type show in the TextBox. This is the code I used to add the TextBoxes and the event handler. This is my first exposure to PreviewKeyDown so the error is probably something simple.
    Code:
                    Dispatcher.Invoke((() =>
                    {
                        chartTraderCustomTextBoxArray[0] = new TextBox()
                        {
                            FontFamily = ChartControl.Properties.LabelFont.Family,
                            FontSize = 13,
                            Foreground = ChartControl.Properties.ChartText,
                            HorizontalAlignment = HorizontalAlignment.Left,
                            Margin = new Thickness(5, 5, 5, 5),
                            Text = string.Format("{0} ", shareQuantity)
                        };
    
                        //textBox.PreviewKeyDown += TextBox_PreviewKeyDown;
                        chartTraderCustomTextBoxArray[0].PreviewKeyDown += SampleWPFModifications_PreviewKeyDownOne;
    
                        UserControlCollection.Add(textBox);
                    }));
            private void SampleWPFModifications_PreviewKeyDownOne(object sender, System.Windows.Input.KeyEventArgs e)
            {
                // this is pretty dumb, but should get you started
                // might want to validate this by using SHIFT or something
                // so Overlay Instrument Selector isn't unusable...
                TextBox textBoxSender = (TextBox)sender;
                textBoxSender.Text += e.Key.ToString();
                // handle the keydown event for the text box
                e.Handled = true;
            }
    ​​
    I initially added a screen shot but it's not showing and I'm not seeing the Upload Attachments button.
    Thanks in advance for any suggestions.
    Last edited by ramckay; 07-15-2024, 03:16 PM.

    #2
    Hello ramckay,

    That is likely related to the Foreground you are using for the textbox, do you see the text if you just create a TextBox without additional style changes?

    chartTraderCustomTextBoxArray[0] = new TextBox();

    Comment


      #3
      Hi Jesse,
      That was the solution.
      Thanks

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      618 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      359 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      561 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      566 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X