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

How to change button color to different color when its on

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

    How to change button color to different color when its on

    Hi, How can I make button when its on to have different background color and when its off its default color? Thank you

    Code:
    #region Buttons
    
            private void AddButtonToToolbar()
            {
                chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
    
                if (chartWindow == null)
                {
                    Print("chartWindow == null");
                    return;
                }
    
                Style btnStyle = new Style();
                btnStyle.TargetType = typeof(System.Windows.Controls.Button);
    
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.FontSizeProperty, 11.0));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.FontFamilyProperty, new FontFamily("Arial")));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.FontWeightProperty, FontWeights.Bold));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.MarginProperty, new Thickness(2, 0, 2, 0)));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.PaddingProperty, new Thickness(4, 2, 4, 2)));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.ForegroundProperty, Brushes.WhiteSmoke));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.BackgroundProperty, Brushes.Gray));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.IsEnabledProperty, true));
                btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.HorizontalAlignmentProperty, HorizontalAlignment.Center));
    
    
    
    
                btnLongsOn = new System.Windows.Controls.Button();
                btnShortsOn = new System.Windows.Controls.Button();
                btnCloseAll = new System.Windows.Controls.Button();
    
                btnLongsOn.Content = "Longs On";
                btnShortsOn.Content = "Shorts On";
                btnCloseAll.Content = "Close All";
    
                btnLongsOn.Style = btnStyle;
                btnShortsOn.Style = btnStyle;
                btnCloseAll.Style = btnStyle;
    
    
                chartWindow.MainMenu.Add(btnLongsOn);
                chartWindow.MainMenu.Add(btnShortsOn);
                chartWindow.MainMenu.Add(btnCloseAll);
    
                btnLongsOn.Visibility = Visibility.Visible;
                btnShortsOn.Visibility = Visibility.Visible;
                btnCloseAll.Visibility = Visibility.Visible;
    
                btnLongsOn.Click += btnLongsOnClick;
                btnShortsOn.Click += btnShortsOnClick;
                btnCloseAll.Click += btnCloseAllClick;
    
                IsToolBarButtonAdded = true;
            }        
    
            private void btnLongsOnClick(object sender, RoutedEventArgs e)
            {
                System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    
                if (button != null)
                {                                
                    if (!longsOn)
                    {
                        longsOn = true;                    
                        btnLongsOn.Content = "Longs On";                
                    }
                    else
                    {
                        longsOn = false;
                        btnLongsOn.Content = "Longs Off";            
                    }
                }
            }        
    
            private void btnShortsOnClick(object sender, RoutedEventArgs e)
            {
                System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    
                if (button != null)
                {                                
                    if (!shortsOn)
                    {
                        shortsOn = true;                    
                        btnShortsOn.Content = "Shorts On";                    
                    }
                    else
                    {
                        shortsOn = false;
                        btnShortsOn.Content = "Shorts Off";            
                    }
                }            
            }
    
            private void btnCloseAllClick(object sender, RoutedEventArgs e)
            {
                System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    
                if (button != null)
                {
                    Dispatcher.InvokeAsync((() =>
                    {
                        if (Position.MarketPosition == MarketPosition.Long)
                            ExitLong();
                        else if (Position.MarketPosition == MarketPosition.Short)
                            ExitShort();                    
                    }));    
                }
            }
    
            private void DisposeCleanUp()
            {
                if (btnLongsOn != null) chartWindow.MainMenu.Remove(btnLongsOn);
                    btnLongsOn.Click -= btnLongsOnClick;
                if (btnShortsOn != null) chartWindow.MainMenu.Remove(btnShortsOn);
                    btnShortsOn.Click -= btnShortsOnClick;
                if (btnCloseAll != null) chartWindow.MainMenu.Remove(btnCloseAll);
                    btnCloseAll.Click -= btnCloseAllClick;            
            }
    
            #endregion​

    #2
    Hello tkaboris,

    Thank you for your note.

    Yes, you may toggle the color of the button based on whether it has been clicked or not. My colleague has posted an example of this here:
    I found the code posted by NinaTrader_ZacharyG and it's exactly what I needed! I couldn't have accomplished this on my own and I'm extremely grateful for the generosity. One small modification I'd like to make is to change the button color when it's clicked. Does anyone have a snippet of code showing how this is accomplished


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      I checked that example, I couldnt follow, that seemed a massive work for creating toggle off and on

      Comment


        #4
        Hello tkaboris,

        Thank you for your response.

        Here is the portion of the code that shows what happens OnButtonClick for the 5 different EMA buttons used in the script:

        Code:
                private void OnButtonClick(object sender, RoutedEventArgs rea)
                {
                    System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
                    if (button == ema1Button && button.Name == "EMA1Button" && button.Content == ema1ButtonOn)
                    {
                        button.Content = ema1ButtonOff;
                        button.Name = "EMA1ButtonOFF";
                        ema1ButtonClicked = true;
                        button.Background = Brushes.Gray;
                        Plots[0].Brush = Brushes.Transparent;
                        ForceRefresh();
                        return;
                    }
        
                    if (button == ema2Button && button.Name == "EMA2Button" && button.Content == ema2ButtonOn)
                    {
                        button.Content = ema2ButtonOff;
                        button.Name = "EMA2ButtonOFF";
                        ema2ButtonClicked = true;
                        button.Background = Brushes.Gray;
                        Plots[1].Brush = Brushes.Transparent;
                        ForceRefresh();
                        return;
                    }
                    if (button == ema3Button && button.Name == "EMA3Button" && button.Content == ema3ButtonOn)
                    {
                        button.Content = ema3ButtonOff;
                        button.Name = "EMA3ButtonOFF";
                        ema3ButtonClicked = true;
                        button.Background = Brushes.Gray;
                        Plots[2].Brush = Brushes.Transparent;
                        ForceRefresh();
                        return;
                    }
                    if (button == ema4Button && button.Name == "EMA4Button" && button.Content == ema4ButtonOn)
                    {
                        button.Content = ema4ButtonOff;
                        button.Name = "EMA4ButtonOFF";
                        ema4ButtonClicked = true;
                        button.Background = Brushes.Gray;
                        Plots[3].Brush = Brushes.Transparent;
                        ForceRefresh();
                        return;
                    }
                    if (button == ema5Button && button.Name == "EMA5Button" && button.Content == ema5ButtonOn)
                    {
                        button.Content = ema5ButtonOff;
                        button.Name = "EMA5ButtonOFF";
                        ema5ButtonClicked = true;
                        button.Background = Brushes.Gray;
                        Plots[4].Brush = Brushes.Transparent;
                        ForceRefresh();
                        return;
                    }
        
        
                    if (button == ema1Button && button.Name == "EMA1ButtonOFF" && button.Content == ema1ButtonOff)
                    {
                        button.Content = ema1ButtonOn;
                        button.Name = "EMA1Button";
                        ema1ButtonClicked = false;
                        button.Background = EMA1Brush;
                        Plots[0].Brush = EMA1Brush;
                        ForceRefresh();
                        return;
                    }
        
                    if (button == ema2Button && button.Name == "EMA2ButtonOFF" && button.Content == ema2ButtonOff)
                    {
                        button.Content = ema2ButtonOn;
                        button.Name = "EMA2Button";
                        ema2ButtonClicked = false;
                        button.Background = EMA2Brush;
                        Plots[1].Brush = EMA2Brush;
                        ForceRefresh();
                        return;
                    }
                    if (button == ema3Button && button.Name == "EMA3ButtonOFF" && button.Content == ema3ButtonOff)
                    {
                        button.Content = ema3ButtonOn;
                        button.Name = "EMA3Button";
                        ema3ButtonClicked = false;
                        button.Background = EMA3Brush;
                        Plots[2].Brush = EMA3Brush;
                        ForceRefresh();
                        return;
                    }
                    if (button == ema4Button && button.Name == "EMA4ButtonOFF" && button.Content == ema4ButtonOff)
                    {
                        button.Content = ema4ButtonOn;
                        button.Name = "EMA4Button";
                        ema4ButtonClicked = false;
                        button.Background = EMA4Brush;
                        Plots[3].Brush = EMA4Brush;
                        ForceRefresh();
                        return;
                    }
                    if (button == ema5Button && button.Name == "EMA5ButtonOFF" && button.Content == ema5ButtonOff)
                    {
                        button.Content = ema5ButtonOn;
                        button.Name = "EMA5Button";
                        ema5ButtonClicked = false;
                        button.Background = EMA5Brush;
                        Plots[4].Brush = EMA5Brush;
                        ForceRefresh();
                        return;
                    }​
        Depending on whether the button is clicked or not, the value for button.Background is changed to the desired color. You should be able to make changes in a similar way using btnLongsOn.Background, btnShortsOn.Background, and btnCloseAll.Background.

        Please let us know if we may be of further assistance.​
        Emily C.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        9 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        14 views
        0 likes
        Last Post AaronKoRn  
        Started by carnitron, Yesterday, 08:42 PM
        0 responses
        11 views
        0 likes
        Last Post carnitron  
        Started by strategist007, Yesterday, 07:51 PM
        0 responses
        13 views
        0 likes
        Last Post strategist007  
        Started by StockTrader88, 03-06-2021, 08:58 AM
        44 responses
        3,983 views
        3 likes
        Last Post jhudas88  
        Working...
        X