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 create dynamic listbox for a parameter?

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

    How to create dynamic listbox for a parameter?

    I would like to have a parameter for my indicator that has a list of values that can change at runtime. Therefore, I cannot use an enum variable type since the values for the enum will not be known at compile time.

    Is there a way to have a listbox appear for a parameter's value that is not using the enum type? The list of values to select would need to be collected in the "get" method of the parameter property. I have a way to collect the values into a string, string[] or List or Dictionary variable... I just need to know how to define the property so that it displays the list.

    Thanks!
    Bryan
    cassb
    NinjaTrader Ecosystem Vendor - Logical Forex

    #2
    Hello Bryan,
    Thanks for your note.

    You can create a custom TypeConverter which can be used to list the array etc.

    For example if you want to read the files in the sound folders and want to list those in the parameters box then you can create your custom TypeConverter.

    We can provide limited support in this respect. You can however refer to msdn to know more about type converters
    Provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Joydeep View Post
      Hello Bryan,
      Thanks for your note.

      You can create a custom TypeConverter which can be used to list the array etc.

      For example if you want to read the files in the sound folders and want to list those in the parameters box then you can create your custom TypeConverter.

      We can provide limited support in this respect. You can however refer to msdn to know more about type converters
      http://msdn.microsoft.com/en-us/libr...converter.aspx
      OK thank you. I have used the ConvertTo... function before. But I'm not certain how to use it in this application. Let's say in the Get accessor code, I can get a list of sound files and put them into a comma-delimited list, or Dictionary or a List variable. But then how do I set up the property to display them? Here's what I have so far, but I don't know how to get the parameter to display the dropdown list of file names.

      Code:
              [GridCategory("Parameters")]
              [Gui.Design.DisplayNameAttribute("Select Sound File")]
              public string SoundFile
              {
                  get 
                  { 
                      Print(DateTime.Now.ToString("HH:mm:ss tt") + " Entering Get accessor...");
                      // Build list of the available files
                      try
                      {
                          string fileString = String.Empty;
                          var files = Directory.GetFiles(Cbi.Core.UserDataDir.ToString() + "sounds", "*.wav");
                          foreach (string currentFile in files)
                          {
                              fileString = fileString  + "," + currentFile;
                              Print("   " + currentFile);
                          }
                          fileString = fileString.Substring(1, fileString.Length - 1);
                          Print(" *** string is " + fileString);
                      }
                      catch (Exception e)
                      {
                          Print(e.Message);
                      }
                      return "Select..."; 
                  }
                  set { soundFile = value; }
              }
      cassb
      NinjaTrader Ecosystem Vendor - Logical Forex

      Comment


        #4
        Hello Bryan,
        You might implement a stand value collection to do it.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Joydeep View Post
          Hello Bryan,
          You might implement a stand value collection to do it.
          OK, but I don't know what 'stand value' collection is... I'll look that up. But do I have the right property type so that the parameter displays the file list? Normally, I would just use a enum variable and the list would be shown when you click on the parameter, but like I said I cannot compile the list until runtime.

          public string SoundFile
          cassb
          NinjaTrader Ecosystem Vendor - Logical Forex

          Comment


            #6
            Hello Bryan,
            My apologies, It should read "standard". You can get more information from here
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Joydeep View Post
              Hello Bryan,
              My apologies, It should read "standard". You can get more information from here
              http://msdn.microsoft.com/en-us/libr...ollection.aspx
              OK yes I see now, thank you! But what I really need to know is how one would 'implement' this to work with the NinjaTrader parameter value to make it show the list of values. Do I have my property set up correctly to allow the user to select from the standard value collection?
              cassb
              NinjaTrader Ecosystem Vendor - Logical Forex

              Comment


                #8
                Hello Bryan,
                NinjaTrader is all C# and you have to implement the TypeConverter like you implement it in any visual studio projects.

                Unfortunately we do not have any NinjaScript sample code for it.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  No, what I mean is what is the data type of the property I should use to get it to allow a listbox selector when you click on the parameter? I tried "string" and "string[]", but they don't let the user pick the file name from a list. Thanks!

                  [GridCategory("Parameters")]
                  [Gui.Design.DisplayNameAttribute("Select Sound File")]
                  public string SoundFile
                  {
                  get {};
                  set {};
                  }
                  Last edited by cassb; 08-16-2012, 01:46 PM.
                  cassb
                  NinjaTrader Ecosystem Vendor - Logical Forex

                  Comment


                    #10
                    I've been busting my brain cells on this all day and I think I'm almost there. But it still does not present a dropdown box to the user like the enum type does. Here's the code so far:

                    Code:
                    private List<string> _file = new List<string>();
                    
                            [GridCategory(" Custom Parameters")]
                            [Gui.Design.DisplayNameAttribute("  Sound Files")]
                            public List<string> SoundFiles
                            {
                                    get  { 
                                           try {
                                                  _file.Clear();
                                                  string[] currentFiles = Directory.GetFiles(Cbi.Core.UserDataDir.ToString() + "sounds", "*.wav");
                                                  foreach (string filename in currentFiles) _file.Add(filename);
                                          }
                                         catch (Exception e)
                                         {
                                                 Print(e.Message);
                                         }
                                        return _file; 
                                    }
                                   set {  _file = value; }
                           }
                    This does build a dynamic list of file names at run time whenever you access the Sound Files parameter. However, the parameter just says "(Collection)" in it with an ellipsis button. See the first attachment.



                    And when you click the button, you get this dialog, which does not work to select one file name. See the second attachment.

                    How do I format this property so that it uses the dropdown list instead of this weird dialog?

                    Thanks!
                    Bryan
                    Attached Files
                    Last edited by cassb; 08-16-2012, 08:50 PM.
                    cassb
                    NinjaTrader Ecosystem Vendor - Logical Forex

                    Comment


                      #11
                      Hello Bryan,
                      Please try this code

                      Code:
                      public class SoundConverter : TypeConverter
                      	{
                      		
                      		public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
                      		{
                      			if (context == null)
                      			{
                      				return null;
                      			}
                      			ArrayList list = new ArrayList();
                      			
                      			
                      			DirectoryInfo dir = new DirectoryInfo(Cbi.Core.InstallDir + "sounds");
                      			
                      			FileInfo[] files= dir.GetFiles("*.wav");
                      			
                      			foreach (FileInfo file in files)
                      			{
                      				list.Add(file.Name);
                      			}
                      				
                      			
                      			return new TypeConverter.StandardValuesCollection(list);
                      		}
                      		
                      	
                      
                      		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
                      		{
                      			return true;
                      		}
                      	}
                      You can call the type converter from your code as

                      Code:
                      [GridCategory("Parameters")]
                      [TypeConverter(typeof(SoundConverter))]
                      public string SoundFiles
                      {get;set;}
                      Last edited by NinjaTrader_Joydeep; 08-17-2012, 03:00 AM.
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        Wow, thank you Joydeep! I figured I could not do it all in the Property itself, but had no idea that it needed its own class and override. Very good, it works!

                        Thanks again!
                        Bryan
                        cassb
                        NinjaTrader Ecosystem Vendor - Logical Forex

                        Comment


                          #13
                          2 listbox dependent one on another

                          Code:
                          Hello I created 2 listbox parameters using database using this code:
                          
                          		[Display(ResourceType = typeof(Custom.Resource), Name = "StrategyName",GroupName = "Parameters", Description = "",Order = -5)]
                          		[TypeConverter(typeof(StrategyConverter))]
                          		public string StrategyName {get;set;}
                          
                          		[Display(ResourceType = typeof(Custom.Resource), Name = "DiagramName",GroupName = "Parameters", Description = "",Order = -4)]
                          		[TypeConverter(typeof(DiagramConverter))]
                          		public string DiagramName {get;set;}
                          
                          	public class StrategyConverter : StrategyBaseConverter {
                          
                          		public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
                          			if (context == null) {
                          				return null;
                          			}
                          			ArrayList list = new ArrayList();
                          			MySqlConnection mySqlConn = new MySqlConnection(Golemator.General.dbConnection);
                          			mySqlConn.Open();
                          
                          			MySqlCommand mySqlCmd = new MySqlCommand("SELECT description FROM strategylist ORDER by id ASC", mySqlConn);
                          
                          			MySqlDataReader mySqlReader = mySqlCmd.ExecuteReader();
                          			while(mySqlReader.Read()) {
                          				list.Add(mySqlReader.GetString(0));
                          			}
                          
                          			mySqlCmd.Dispose();
                          			mySqlReader.Dispose();
                          			
                          			return new TypeConverter.StandardValuesCollection(list);
                          		}
                          
                          		public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
                          			return true;
                          		}
                          	}
                          
                          	public class DiagramConverter : StrategyBaseConverter {
                          
                          		public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
                          
                          			if (context == null) {
                          				return null;
                          			}
                          
                          			ArrayList list = new ArrayList();
                          			MySqlConnection mySqlConn = new MySqlConnection(Golemator.General.dbConnection);
                          			mySqlConn.Open();
                          
                          			int strategyId = 1;
                          
                          			list = new ArrayList();
                          
                          			MySqlCommand mySqlCmd = new MySqlCommand("SELECT name FROM diagramlist WHERE strategy_id ='"+ strategyId + "'", mySqlConn);
                          			MySqlDataReader mySqlReader = mySqlCmd.ExecuteReader();
                          			while(mySqlReader.Read()) {
                          				list.Add(mySqlReader.GetString(0));
                          			}
                          
                          			mySqlCmd.Dispose();
                          			mySqlReader.Dispose();
                          			
                          			return new StandardValuesCollection(list);
                          		}
                          
                          		public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
                          			return true;
                          		}
                          	}
                          in listbox DiagramName I want to have values based on StrategyName value selected. How do I make action after i change StrategyName value -> i want to reload and update available diagrams which depend on selected strategy name.

                          Can some one please help me out?

                          Comment


                            #14
                            Originally posted by kujista View Post
                            Code:
                                    [Display(ResourceType = typeof(Custom.Resource), Name = "StrategyName",GroupName = "Parameters", Description = "",Order = -5)]
                                    [TypeConverter(typeof(StrategyConverter))]
                                    public string StrategyName {get;set;}
                            in listbox DiagramName I want to have values based on StrategyName value selected. How do I make action after i change StrategyName value -> i want to reload and update available diagrams which depend on selected strategy name.

                            Can some one please help me out?
                            Try adding RefreshProperties to your StrategyName property, like this,

                            Code:
                            [Display(ResourceType = typeof(Custom.Resource), Name = StrategyName",
                            GroupName = "Parameters", Description = "", Order = -5)]
                            [TypeConverter(typeof(StrategyConverter))]
                            [COLOR=Red][RefreshProperties(RefreshProperties.All)][/COLOR]
                            public string StrategyName {get;set;}

                            Comment


                              #15
                              I have problem accessing strategy object inside public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
                              function... to get proper int strategyId = <number i want to get from strategy object>
                              [RefreshProperties(RefreshProperties.All)] I will try when I figure out how to get the strategyId value

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 12:02 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by GLFX005, Today, 03:23 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by nandhumca, Yesterday, 03:41 PM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by The_Sec, Yesterday, 03:37 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by vecnopus, Today, 06:15 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post vecnopus  
                              Working...
                              X