Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

error CS0117 does not contain a definition for 'value'

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

    error CS0117 does not contain a definition for 'value'

    I'm trying to loop through an enum's values to change a button's Content/label based on the related enum's value:


    Code:
                  foreach(string value in Enum.GetNames(typeof (MyEnum)))
                  {
                    if (aA == MyEnum.value)
                      bB = value.ToString();
                  }​


    from code similar to this

    Code:
    public enum MyEnum
    {
      X,
      Y,
      Z
    }
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class MyXConverterStrat : Strategy
        {  
            private string bB;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                  ...
                  foreach(string value in Enum.GetNames(typeof (MyEnum)))
                  {
                    if (aA == MyEnum.value)
                      bB = value.ToString();
                  }
                  
                  
                  aA            = MyEnum.X;
                  Button1Label    = bB;
                  
                  ...
                }
            }
                  
            ...      
                  
            #region Properties
            
            [Display(Name="Button Methods", Description="", Order=1, GroupName="Button Parameters")]
            [RefreshProperties(RefreshProperties.All)]
            public MyEnum aA
            { get; set; }
            
            [NinjaScriptProperty]
            [Display(Name="Label Button 1", Order=2, GroupName="Buttons Parameters")]
            public string Button1Label
            { get; set; }
            
            #endregion
        }
        
    }​
    I found this solution for int:
    Code:
    ((Months)int.Parse(monthNum)).ToString()​
    I am a beginner to C# and have reviewed my class notes, researched online, and even copied some people. I still can't understand what I'm doing wrong in this code. I have tried changing the data ty...


    How to adapt it to string?
    Last edited by PaulMohn; 08-23-2024, 11:23 AM.

    #2
    Hello PaulMohn,

    This would fall under general c# education.

    Below is a link to a google search.

    I have the following enum: public enum Urgency { VeryHigh = 1, High = 2, Routine = 4 } I can fetch an enum "value" as string like this: ((int)Urgency.Routine).ToString() // retur...

    Learn about C# enumeration types that represent a choice or a combination of choices
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Fix:

      Code:
      foreach (string value in Enum.GetNames(typeof(MyEnum)))
      {
          // Convert the string value back to the MyEnum type
          MyEnum enumValue = (MyEnum)Enum.Parse(typeof(MyEnum), value);
      
          if (aA == enumValue) // Compare against the enum value
          {
              bB = value; // No need to call ToString(), value is already a string
          }
      }​

      Code:
      public enum MyEnum
      {
        X,
        Y,
        Z
      }
      
      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public class MyXConverterStrat : Strategy
          {  
              private string bB;
              
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      ...
                      
                      // aA must be assigned the Enum value before the foreach loop
                        // else it takes the Enum's 1st/0th index value (X) by default
                      aA              = MyEnum.Z;
                      
                      foreach (string value in Enum.GetNames(typeof(MyEnum)))
                      {
                          // Convert the string value back to the MyEnum type
                          MyEnum enumValue = (MyEnum)Enum.Parse(typeof(MyEnum), value);
                      
                          if (aA == enumValue) // Compare against the enum value
                          {
                              bB = value; // No need to call ToString(), value is already a string
                          }
                      }
                      
                      
                      Button1Label    = bB;
                    
                    ...
                  }
              }
                    
              ...      
                    
              #region Properties
              
              [Display(Name="Button Methods", Description="", Order=1, GroupName="Button Parameters")]
              [RefreshProperties(RefreshProperties.All)]
              public MyEnum aA
              { get; set; }
              
              [NinjaScriptProperty]
              [Display(Name="Label Button 1", Order=2, GroupName="Buttons Parameters")]
              public string Button1Label
              { get; set; }
              
              #endregion
          }
          
      }




      In this article, we are going to learn how to convert string and int to Enum in C#. We'll show different options to do the same thing.
      Last edited by PaulMohn; 08-24-2024, 03:30 AM.

      Comment


        #4
        NinjaTrader_ChelseaB Hello,

        I'm trying to restrict the displaying of the Tick numbers ( ButtonTicks ) in the Buttons Content/Labels based on which Enum ( MyEnum )​ value is selected.
        Only if certain subvalues from the Enum set ( MyEnum.A or MyEnum.B or MyEnum.C ​) are selected in the buttons, should the Tick numbers Tick numbers ( _button1Ticks.ToString() or _button2Ticks.ToString())​ be displayed, else return string.Empty .

        Code:
                                #region ADD BUTTONS TICKS # ONLY TO STP SUB ENUM ITEMS
                                
                                    string[] SubEnumArray = new string[]
                                    {
                                        "MyEnum.A",
                                        "MyEnum.B",
                                        "MyEnum.C"
                                    };
                                    
                                    foreach (string enumsubValue in SubEnumArray)
                                    {
                                        // An unitialized variable.
                                        //MyEnum myEnum;
                                        
                                        MyEnum inputAsEnum = (MyEnum)Enum.Parse(typeof(MyEnum), enumsubValue);
        
                                        xX = ( ( xEnum == inputAsEnum ) ? _button1Ticks.ToString() : string.Empty );
                                        yY = ( ( YEnum == inputAsEnum ) ? _button2Ticks.ToString() : string.Empty );
        
                                        Print(" ");
                                        Print("enumsubValue " + enumsubValue);
                                        Print("inputAsEnum " + inputAsEnum);
                                        Print("_button1Ticks.ToString() " + _button1Ticks.ToString());
                                        Print("_button2Ticks.ToString() " + _button2Ticks.ToString());
                                        Print("xEnum " + xEnum);
                                        Print("YEnum " + YEnum);
                                    }    
                                
                                #endregion​
        Code:
                                switch(i)
                                {
                                  case 0:
                                    ButtonLabels        = Button1Label;
                                    ButtonBackground    = Button1Color;
                                    ButtonTicks         = xX;
                                    break;
                                  case 1:
                                    ButtonLabels        = Button2Label;
                                    ButtonBackground    = Button2Color;
                                    ButtonTicks         = yY;
                                    break;    
                                  default:
                                    ButtonLabels        = "";
                                    ButtonBackground    = null;
                                    ButtonTicks         = "1";                        
                                    break;
                                }
                                
                                buttonsArray[i]    = new System.Windows.Controls.Button()    
                                {
                                    Content            = string.Format("{0} + {1}",ButtonLabels,ButtonTicks),
                                    Height            = 25,
                                    Margin            = new Thickness(0,0,0,0),
                                    Padding            = new Thickness(0,0,0,0),
                                    Style            = basicButtonStyle,
                                    FontFamily         = new FontFamily("Agency Fb"),
                                    FontSize         = 11,
                                    FontWeight        = FontWeights.Bold
                                };
        
                                buttonsArray[i].BorderBrush    = Brushes.DimGray;
                                buttonsArray[i].Background = ButtonBackground;
                                
                            }​


        The buttons stop being displayed on the grid (the strategy loads on the chart and execute trades, but no buttons/extra grids are displayed anymore on the ChartTrader) with a similar code to the following one, no log tab error, nor ninjascript error:

        Code:
        public enum MyEnum
        {
            A,
            B,
            C,
        
            X,
            Y,
            Z
        }
        
        //This namespace holds Strategies in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.Strategies
        {
            public class MyXConverterStrat : Strategy
            {  
                private string xX, yY;
                
                private string Btn1Lbl, Btn2Lbl;
                
                private int _button1Ticks, _button2Ticks;
                
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        ...
                      
                        xEnum              = MyEnum.C;
                        YEnum              = MyEnum.B;
                        
                        foreach (string value in Enum.GetNames(typeof(MyEnum)))
                        {
                            MyEnum enumValue = (MyEnum)Enum.Parse(typeof(MyEnum), value);
                        
                            if (xEnum == enumValue)
                                btn1Lbl = value;
                        
                            if (YEnum == enumValue)
                                btn2Lbl = value;
                        }
                        
                        
                        Button1Label    = Btn1Lbl;
                        Button2Label    = Btn2Lbl;
        
                        _button1Ticks   = 4;​
                        _button2Ticks   = 2;​​
                      
                      ...
                    }
                    
                    ...
                    
        
                    protected void CreateWPFControls()
                    {                
                        #region BUTTONS ARRAY AND LOOP
        
                            // all of the buttons are basically the same so to save lines of code I decided to use a loop over an array
                            buttonsArray = new System.Windows.Controls.Button[2];
        
                            for (int i = 0; i < 2; ++i)
                            {
                                System.Windows.Media.Brush ButtonBackground;
                                string ButtonLabels, ButtonTicks;
                                
                                #region ADD BUTTONS TICKS # ONLY TO STP SUB ENUM ITEMS
                                
                                    string[] SubEnumArray = new string[]
                                    {
                                        "MyEnum.A",
                                        "MyEnum.B",
                                        "MyEnum.C"
                                    };
                                    
                                    foreach (string enumsubValue in SubEnumArray)
                                    {
                                        // An unitialized variable.
                                        // MyEnum myEnum;
                                        
                                        MyEnum inputAsEnum = (MyEnum)Enum.Parse(typeof(MyEnum), enumsubValue);
        
                                        xX = ( ( xEnum == inputAsEnum ) ? _button1Ticks.ToString() : string.Empty );
                                        yY = ( ( YEnum == inputAsEnum ) ? _button2Ticks.ToString() : string.Empty );
        
                                        Print(" ");
                                        Print("enumsubValue " + enumsubValue);
                                        Print("inputAsEnum " + inputAsEnum);
                                        Print("_button1Ticks.ToString() " + _button1Ticks.ToString());
                                        Print("_button2Ticks.ToString() " + _button2Ticks.ToString());
                                        Print("xEnum " + xEnum);
                                        Print("YEnum " + YEnum);
                                    }    
                                
                                #endregion
                                
                                switch(i)
                                {
                                  case 0:
                                    ButtonLabels        = Button1Label;
                                    ButtonBackground    = Button1Color;
                                    ButtonTicks         = xX;
                                    break;
                                  case 1:
                                    ButtonLabels        = Button2Label;
                                    ButtonBackground    = Button2Color;
                                    ButtonTicks         = yY;
                                    break;    
                                  default:
                                    ButtonLabels        = "";
                                    ButtonBackground    = null;
                                    ButtonTicks         = "1";                        
                                    break;
                                }
                                
                                buttonsArray[i]    = new System.Windows.Controls.Button()    
                                {
                                    Content            = string.Format("{0} + {1}",ButtonLabels,ButtonTicks),
                                    Height            = 25,
                                    Margin            = new Thickness(0,0,0,0),
                                    Padding            = new Thickness(0,0,0,0),
                                    Style            = basicButtonStyle,
                                    FontFamily         = new FontFamily("Agency Fb"),
                                    FontSize         = 11,
                                    FontWeight        = FontWeights.Bold
                                };
        
                                buttonsArray[i].BorderBrush    = Brushes.DimGray;
                                buttonsArray[i].Background = ButtonBackground;
                                
                            }
                            
                        #endregion
                        
                    }
                    
                    
                    ...
                }
                      
                ...      
                      
                #region Properties
                
                [Display(Name="xEnum Button Methods", Description="", Order=1, GroupName="Button Parameters")]
                [RefreshProperties(RefreshProperties.All)]
                public MyEnum xEnum
                { get; set; }
                
                [Display(Name="YEnum Button Methods", Description="", Order=2, GroupName="Button Parameters")]
                [RefreshProperties(RefreshProperties.All)]
                public MyEnum YEnum
                { get; set; }
                
                
                [NinjaScriptProperty]
                [Display(Name="Label Button 1", Order=3, GroupName="Buttons Parameters")]
                public string Button1Label
                { get; set; }
                
                [NinjaScriptProperty]
                [Display(Name="Label Button 2", Order=4, GroupName="Buttons Parameters")]
                public string Button2Label
                { get; set; }
                
                
                [NinjaScriptProperty]
                [Range(0, int.MaxValue)]
                [Display(Name="Button1Ticks", Order=5, GroupName="Buttons Parameters")]
                public int _Button1Ticks
                {
        
                    get { return _button1Ticks; }
        
                    set { _button1Ticks = value; }
                }
                
                [NinjaScriptProperty]
                [Range(0, int.MaxValue)]
                [Display(Name="Button2Ticks", Order=6, GroupName="Buttons Parameters")]
                public int _Button2Ticks
                {
        
                    get { return _button2Ticks; }
        
                    set { _button2Ticks = value; }
                }
                
                #endregion
            }
            
        }


        I tried to print every relevant variable but no output is returned.

        Print(" ");
        Print("enumsubValue " + enumsubValue);
        Print("inputAsEnum " + inputAsEnum);
        Print("_button1Ticks.ToString() " + _button1Ticks.ToString());
        Print("_button2Ticks.ToString() " + _button2Ticks.ToString());
        Print("xEnum " + xEnum);
        Print("YEnum " + YEnum);​


        Why don't the buttons grid get displayed on the ChartTrader anymore? What's the fix?

        I have an enum that I'd like to display all possible values of. Is there a way to get an array or list of all the possible values of the enum instead of manually creating such a list? e.g. If I hav...


        Code:
        
        
                            MyEnum[] MyEnumArray1 = new MyEnum[]
                            {
                                MyEnum.B,
                                MyEnum.C,
                            };
                            
                            foreach (var value in MyEnumArray1)
                            {
                                Print("value " + value);
                                Print("st1EnumVar " + st1EnumVar);
                                Print("nd2EnumVar " + nd2EnumVar);
                                
                                xX = ( ( st1EnumVar == value ) ? ( " + " + _button1Ticks.ToString() ) : string.Empty );
                                yY = ( ( nd2EnumVar == value ) ? ( " + " + _button2Ticks.ToString() ) : string.Empty );
                                
                                Print("xX " + xX);
                                Print("yY " + yY);
                                
                            }​


        Last edited by PaulMohn; 08-26-2024, 05:07 AM.

        Comment


          #5
          Originally posted by PaulMohn View Post
          Fix:

          Code:
          foreach (string value in Enum.GetNames(typeof(MyEnum)))
          {
          // Convert the string value back to the MyEnum type
          MyEnum enumValue = (MyEnum)Enum.Parse(typeof(MyEnum), value);
          
          if (aA == enumValue) // Compare against the enum value
          {
          bB = value; // No need to call ToString(), value is already a string
          }
          }​

          Code:
          public enum MyEnum
          {
          X,
          Y,
          Z
          }
          
          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class MyXConverterStrat : Strategy
          {
          private string bB;
          
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          ...
          
          // aA must be assigned the Enum value before the foreach loop
          // else it takes the Enum's 1st/0th index value (X) by default
          aA = MyEnum.Z;
          
          foreach (string value in Enum.GetNames(typeof(MyEnum)))
          {
          // Convert the string value back to the MyEnum type
          MyEnum enumValue = (MyEnum)Enum.Parse(typeof(MyEnum), value);
          
          if (aA == enumValue) // Compare against the enum value
          {
          bB = value; // No need to call ToString(), value is already a string
          }
          }
          
          
          Button1Label = bB;
          
          ...
          }
          }
          
          ...
          
          #region Properties
          
          [Display(Name="Button Methods", Description="", Order=1, GroupName="Button Parameters")]
          [RefreshProperties(RefreshProperties.All)]
          public MyEnum aA
          { get; set; }
          
          [NinjaScriptProperty]
          [Display(Name="Label Button 1", Order=2, GroupName="Buttons Parameters")]
          public string Button1Label
          { get; set; }
          
          #endregion
          }
          
          }




          https://web.archive.org/web/20240226...d-int-to-enum/
          NinjaTrader_ChelseaB Hello,

          Why does it still not set bB = value when changing the value from the Stategy User Input Window?
          I mean when I select a new value in the enum dropdown selector in the user input window from the Chart it still does not reflect the new value.
          Here is some similar testing code prints showing it does print as expected Button1Label: Y when first loaded on the chart:

          But I can't test prints further for when changing the enum selection manually from th UI Window.


          In this article you will see use of Enum data type in C#. convert enum to String, convert String to enum.




          Check out this C# code I wrote. Run it and see what it does!








          I have more than 20 years of experience in the Software Development industry that includes working as a Consultant and Trainer in a wide array of different industries. Welcome to my resume and work history portfolio website!


          Last edited by PaulMohn; 08-26-2024, 12:56 PM.

          Comment


            #6
            Hello PaulMohn,

            If the prints are not appearing I would expect the code is not being reached or a run-time error is occurring or you are trying to access the UI thread without using a dispatcher invoke async.

            What is the last line of code that was properly executed, what is the first line of code that is not executed?

            You are trying to set a button content based on the value of an enum?

            Converting to a string and back to an enum appears unnecessary.

            I've copied and pasted from the SampleUniversalMovingAverage and SampleWPFModifications to make a reduced test script that does this.
            CheckEnumValueTest_NT8.zip

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks a lot NinjaTrader_ChelseaB !

              I'll try and reduce the part about the ticks # as per your sample example next.
              But first, I still can't fix the buttons labels string to be set by the buttons enums strings upon Strategy Properties window selection issue.

              I reduced as much as possible my production script in attachement _MyEnum2.zip.

              Here's the most relevant part:

              Code:
                    
                          protected override void OnStateChange()
                          {    
                              #region (State == State.SetDefaults)
                              
                                  if (State == State.SetDefaults)
                                  {​
                                    ...
                                      
                                      #region #1 Buttons
                                      
                                          #region ENUM LOOP BUTTON LABEL == BUTTON ENUM STRING
                                      
                                              #region BUTTONS PARAMETERS - BUTTONS METHOD ENUM SELECTORS
                                      
                                                  st1EnumVar                    = MyEnum2nd.X;
                                                  nd2EnumVar                    = MyEnum2nd.B;
                                          
                                              #endregion
                              
                                              foreach (string value in Enum.GetNames(typeof(MyEnum2nd)))
                                              {
                                                  Print("value0: " + value);
                                                  
                                                  MyEnum2nd enumValue = (MyEnum2nd)Enum.Parse(typeof(MyEnum2nd), value);
                                                  
                                                  Print("enumValue: " + enumValue);
                                                  Print(" ");
                                              
                                                  if (st1EnumVar == enumValue)
                                                      Btn1Lbl = value;
                                                  
                                                  Print("value1: " + value);
                                                  Print("Btn1Lbl: " + Btn1Lbl);
                                              
                                                  if (nd2EnumVar == enumValue)
                                                      Btn2Lbl = value;
                                                  Print("value2: " + value);
                                                  Print("Btn2Lbl: " + Btn2Lbl);
                                              }
                                          
                                          #endregion
                                          
                                          #region BUTTONS PARAMETERS
                                          
                                              Button1                        = true;
                                              Button1Label                = Btn1Lbl;
                                              Button1Color                = Brushes.ForestGreen;
                                              _button1Ticks                = 4;
                                              
                                              Button2                        = true;
                                              Button2Label                = Btn2Lbl;
                                              Button2Color                = Brushes.Crimson;
                                              _button2Ticks                = 2;
                                          
                                          #endregion
                                      
                                      #endregion
                                  }​
              Which prints:

              value1: X
              Btn1Lbl: X
              value2: X
              Btn2Lbl: B
              value0: Y
              enumValue: Y

              value1: Y
              Btn1Lbl: X
              value2: Y
              Btn2Lbl: B
              value0: Z
              enumValue: Z

              value1: Z
              Btn1Lbl: X
              value2: Z
              Btn2Lbl: B
              value0: A
              enumValue: A

              value1: A
              Btn1Lbl:
              value2: A
              Btn2Lbl:
              value0: B
              enumValue: B

              value1: B
              Btn1Lbl:
              value2: B
              Btn2Lbl: B
              value0: C
              enumValue: C

              value1: C
              Btn1Lbl:
              value2: C
              Btn2Lbl: B
              value0: X
              enumValue: X

              value1: X
              Btn1Lbl: X
              value2: X
              Btn2Lbl: B
              value0: Y
              enumValue: Y

              value1: Y
              Btn1Lbl: X
              value2: Y
              Btn2Lbl: B
              value0: Z
              enumValue: Z

              value1: Z
              Btn1Lbl: X
              value2: Z
              Btn2Lbl: B​
              The problem is no label is set to the enum value when selecting a new enum value in the strategies chart window:

              Label Button 1 should be set to "A" (same as BttonMethods Selector Button 1 value)
              Label Button 2 should be set to "C" ​(same as ButtonMethods Selector Button 2 value)

              Click image for larger version  Name:	labelenum.png Views:	0 Size:	42.1 KB ID:	1315708
              If the prints are not appearing I would expect the
              code is not being reached
              or a run-time error is occurring
              or you are trying to access the UI thread without using a dispatcher invoke async.
              How to check these issues?

              The most relevant code is in the (State == State.SetDefaults) scope so I'm not sure if I must use the dispatcher as I've never seen any example of this.
              How do you check for runtime error in the Ninjascript editor?
              The code looks like it is reaching the most relevant code from the prints.
              Last edited by PaulMohn; 08-26-2024, 09:04 AM.

              Comment


                #8
                Hello PaulMohn,

                This code cannot be in State.SetDefaults. When this runs there is no ChartControl and no values have been selected in the UI. This state is used for a clone for getting the default property values. A script does not re-enter this state after it has been configured.

                Likely this code needs to be in State.DataLoaded or State.Historical. Have a look at the exampe I provided you. The code that sets the button content is in State.Historical.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  NinjaTrader_ChelseaB thanks!

                  It does not set anything with either:

                  State.Historical ( exported as attachment _MyEnum3.zip )

                  Code:
                  #region (OnStateChange)
                          
                              protected override void OnStateChange()
                              {    
                                  #region (State == State.SetDefaults)
                                  
                                      if (State == State.SetDefaults)
                                      {
                                          ...
                                  
                                          #region BUTTONS PARAMETERS - BUTTONS METHOD ENUM SELECTORS
                                  
                                              st1EnumVar                    = MyEnum3.X;
                                              nd2EnumVar                    = MyEnum3.B;
                                      
                                          #endregion
                                      
                                          #region BUTTONS PARAMETERS
                                          
                                              Button1                        = true;
                                              Button1Label                = Btn1Lbl;
                                              Button1Color                = Brushes.ForestGreen;
                                              _button1Ticks                = 4;
                                              
                                              Button2                        = true;
                                              Button2Label                = Btn2Lbl;
                                              Button2Color                = Brushes.Crimson;
                                              _button2Ticks                = 2;
                                          
                                          #endregion
                  
                                      }
                                      
                                  #endregion
                                  
                                  #region (State == State.Configure)
                                  
                                      else if (State == State.Configure)
                                      {
                                      }
                                      
                                  #endregion
                                  
                                  #region (State == State.Historical)
                                  
                                      else if (State == State.Historical)
                                      {
                                          if (ChartControl != null)
                                          {
                                              ChartControl.Dispatcher.InvokeAsync(() =>
                                              {
                                                  EnumtoLabel();
                                                  
                                                  CreateWPFControls();
                                              });
                                          }
                                      }
                                      
                                  #endregion
                                      
                                  #region (State == State.Terminated)
                  
                                      else if (State == State.Terminated)
                                      {
                                          if (ChartControl != null)
                                          {
                                              ChartControl.Dispatcher.InvokeAsync(() =>
                                              {
                                                  DisposeWPFControls();
                                              });
                                          }
                                      }
                                      
                                  #endregion
                              }
                          
                          #endregion
                              
                          #region
                              
                              public void EnumtoLabel()
                              {        
                                  #region #1 Buttons
                                  
                                      #region ENUM LOOP BUTTON LABEL == BUTTON ENUM STRING
                          
                                          foreach (string value in Enum.GetNames(typeof(MyEnum3)))
                                          {
                                              Print("value0: " + value);
                                              
                                              MyEnum3 enumValue = (MyEnum3)Enum.Parse(typeof(MyEnum3), value);
                                              
                                              Print("enumValue: " + enumValue);
                                              Print(" ");
                                          
                                              if (st1EnumVar == enumValue)
                                                  Btn1Lbl = value;
                                              
                                              Print("value1: " + value);
                                              Print("Btn1Lbl: " + Btn1Lbl);
                                          
                                              if (nd2EnumVar == enumValue)
                                                  Btn2Lbl = value;
                                              Print("value2: " + value);
                                              Print("Btn2Lbl: " + Btn2Lbl);
                                          }
                                      
                                      #endregion
                                  
                                  #endregion
                              }
                              
                          #endregion​

                  Click image for larger version  Name:	labelenum1.png Views:	0 Size:	37.4 KB ID:	1315735

                  Comment


                    #10
                    State.DataLoaded ( exported as attachment _MyEnum4.zip )​

                    Code:
                            #region (OnStateChange)
                            
                                protected override void OnStateChange()
                                {    
                                    #region (State == State.SetDefaults)
                                    
                                        if (State == State.SetDefaults)
                                        {
                                            ...
                                    
                                            #region BUTTONS PARAMETERS - BUTTONS METHOD ENUM SELECTORS
                                    
                                                st1EnumVar                    = MyEnum4.X;
                                                nd2EnumVar                    = MyEnum4.B;
                                        
                                            #endregion
                                        
                                            #region BUTTONS PARAMETERS
                                            
                                                Button1                        = true;
                                                Button1Label                = Btn1Lbl;
                                                Button1Color                = Brushes.ForestGreen;
                                                _button1Ticks                = 4;
                                                
                                                Button2                        = true;
                                                Button2Label                = Btn2Lbl;
                                                Button2Color                = Brushes.Crimson;
                                                _button2Ticks                = 2;
                                            
                                            #endregion
                    
                                        }
                                        
                                    #endregion
                                    
                                    #region (State == State.Configure)
                                    
                                        else if (State == State.Configure)
                                        {
                                        }
                                        
                                    #endregion
                                    
                                    #region (State == State.DataLoaded)
                                    
                                        else if (State == State.DataLoaded)
                                        {
                                            if (ChartControl != null)
                                            {
                                                ChartControl.Dispatcher.InvokeAsync(() =>
                                                {
                                                    EnumtoLabel();
                                                });
                                            }
                                        }
                                        
                                    #endregion
                                    
                                    #region (State == State.Historical)
                                    
                                        else if (State == State.Historical)
                                        {
                                            if (ChartControl != null)
                                            {
                                                ChartControl.Dispatcher.InvokeAsync(() =>
                                                {
                                                    CreateWPFControls();
                                                });
                                            }
                                        }
                                        
                                    #endregion
                                        
                                    #region (State == State.Terminated)
                    
                                        else if (State == State.Terminated)
                                        {
                                            if (ChartControl != null)
                                            {
                                                ChartControl.Dispatcher.InvokeAsync(() =>
                                                {
                                                    DisposeWPFControls();
                                                });
                                            }
                                        }
                                        
                                    #endregion
                                }
                            
                            #endregion
                                
                            #region
                                
                                public void EnumtoLabel()
                                {        
                                    #region #1 Buttons
                                    
                                        #region ENUM LOOP BUTTON LABEL == BUTTON ENUM STRING
                            
                                            foreach (string value in Enum.GetNames(typeof(MyEnum4)))
                                            {
                                                Print("value0: " + value);
                                                
                                                MyEnum4 enumValue = (MyEnum4)Enum.Parse(typeof(MyEnum4), value);
                                                
                                                Print("enumValue: " + enumValue);
                                                Print(" ");
                                            
                                                if (st1EnumVar == enumValue)
                                                    Btn1Lbl = value;
                                                
                                                Print("value1: " + value);
                                                Print("Btn1Lbl: " + Btn1Lbl);
                                            
                                                if (nd2EnumVar == enumValue)
                                                    Btn2Lbl = value;
                                                Print("value2: " + value);
                                                Print("Btn2Lbl: " + Btn2Lbl);
                                            }
                                        
                                        #endregion
                                    
                                    #endregion
                                }
                                
                            #endregion​
                    Click image for larger version

Name:	labelenum2.png
Views:	160
Size:	37.6 KB
ID:	1315745

                    Comment


                      #11
                      Hello PaulMohn,

                      The code you have suggested in post # 9 is still showing in State.SetDefaults.

                      Try using the test script I have provided you as a starting point and add you code to it.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        NinjaTrader_ChelseaB

                        I separated into a new callable method EnumtoLabel() in posts 9 and 10.

                        I also checked your sample but there's no enum to string/label snippet in it:


                        Last edited by PaulMohn; 08-26-2024, 12:32 PM.

                        Comment


                          #13
                          Hello PaulMohn,

                          Converting an enum to a string and back would fall under general C# education. This thread will remain open for any community memebers that would like to assist with advanced custom C# code.

                          The example I have provided shows it is possible to use an enum to control what text appears in the .Content of a button.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            NinjaTrader_ChelseaB

                            I've not been inquiring about converting an enum back to a string, that conversion was taken care of already in post #3 and #4 (#4 being latest development):


                            Code:
                                                MyEnum[] MyEnumArray1 = new MyEnum[]
                                                {
                                                    MyEnum.B,
                                                    MyEnum.C,
                                                };
                                                
                                                foreach (var value in MyEnumArray1)
                                                {
                                                    Print("value " + value);
                                                    Print("st1EnumVar " + st1EnumVar);
                                                    Print("nd2EnumVar " + nd2EnumVar);
                                                    
                                                    xX = ( ( st1EnumVar == value ) ? ( " + " + _button1Ticks.ToString() ) : string.Empty );
                                                    yY = ( ( nd2EnumVar == value ) ? ( " + " + _button2Ticks.ToString() ) : string.Empty );
                                                    
                                                    Print("xX " + xX);
                                                    Print("yY " + yY);
                                                    
                                                }​

                            I am inquring rather as to why despite no error the EnumtoLabel() callable method does not do the following as expected (even when called from
                            State.Historical
                            State.DataLoaded):

                            Label Button 1 should be set to "A" (same as BttonMethods Selector Button 1 value)
                            Label Button 2 should be set to "C" ​(same as ButtonMethods Selector Button 2 value)​

                            Your sample does not address this issue. My samples MyEnum3 and MyEnum4 already provide all the snippets (which would have to to replicated if adapting yours).
                            I cannot test further with prints as stated previously in post #5 because
                            I can't test prints further for when changing the enum selection manually from the UI Window.

                            What else beside moving the relevant code in the dispatcher in State.Historical or /and State.DataLoaded​ do you suggest would help doing (having already unsucussfully tested the former 2 suggestions in post #9 and #10)?

                            Comment


                              #15
                              Hello PaulMohn,

                              This may be due to the advanced custom C# code in your script including converting enums to strings.

                              I've modified the script to include the same text as the enum value in the button content.
                              CheckEnumValueTest_NT8.zip

                              If prints are not appearing the code is not being reached or a run-time error has been hit.

                              Have I misunderstood the issue?
                              Are you not trying to control text appearing in the button content?
                              Are you trying to set a public string input value based on the selection of an enum input value?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, 03-13-2026, 05:17 AM
                              0 responses
                              81 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              150 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              79 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              52 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              59 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X