7 Replies Latest reply on Mar 23, 2018 5:02 AM by John Messinger

    Winform reloaded

    alanR

      Hi ,

       

      im after creating a winform with textboxs and AFComboBoxs,  when i close the form the inputted  values save to a configString. When i reopen that form  the values for the for the textbox reappear but the values picked for the combobox are set back to default, how can get it to display the previously chosen combobox values ?

       

      My code snippets;

        private void LoadUI()
              {
                 
                  txtTargetAttribute.Text = dataReference.TargetAttributeName;
                  cmbByTimeRange.Text = dataReference.TimeRange;
                  LoadComboBoxes();
      } 
              }
      

       

      private void LoadComboBoxes()
      {
      
        cmbByTimeRange.DataSource = Enum.GetValues(typeof(AFSummaryTypes));
      }
      
      
      
      

       

      Thanks

        • Re: Winform reloaded
          John Messinger

          Hi Alan,

           

          This is the way that I approach these:

           

          cmbByTimeRange.DataSource = Enum.GetValues(typeof(AFSummaryTypes)); // This will load the required items into the combo box
          cmbByTimeRange.SelectedIndex = 0;
          if (cmbByTimeRange.Items.Contains(dataReference.TimeRangeAttribute)
          {
               cmbByTimeRange.Text = dataReference.TimeRangeAttribute
          }
          
          2 of 2 people found this helpful
            • Re: Winform reloaded
              alanR

              Thanks John ,

               

              Which method would i put it in?

                • Re: Winform reloaded
                  John Messinger

                  This would go into your LoadComboBoxes() method.

                    • Re: Winform reloaded
                      alanR

                      I have tried

                      
                      
                      cmbByTimeRange.DataSource = Enum.GetValues(typeof(AFSummaryTypes));
                                  cmbByTimeRange.SelectedIndex = 0;
                                  if (cmbByTimeRange.Items.Contains(dataReference.TimeRange))
                                  {
                                      cmbByTimeRange.Text = dataReference.TimeRange;
                                  }
                      
                      
                      
                      
                      
                      
                      
                      
                      

                      When i reopen the form , it defaults to the first item in the list.

                       

                      It works for unit of measure and all i have declared is;

                      cmbSourceUnit.InitializeObject(new PISystems().DefaultPISystem.UOMDatabase);
                      

                       

                      Is the there a way to declare AFSummaryType the same way?

                        • Re: Winform reloaded
                          John Messinger

                          So if I understand your original post correctly, you are creating a Windows Form to use as a configuration editor for a custom data reference, and it appears that the intent is that when the form is loaded and becomes visible you want the previously saved data reference options (stored in the ConfigString) to be the selected/default values of your form controls - textboxes, combo boxes etc. You are populating the items list of your combo boxes from collections such as the UOM database and the AFSummaryTypes enumeration, and then expecting that the saved ConfigString item is contained within the list you have populated, and shown as the selected item. If my understanding of your intent is incorrect, can you please clarify what you are trying to do.

                           

                          The code sample I shared above is taken from one of my own custom data references, doing pretty much exactly what I just described. In that code, the dataReference.TimeRange refers to the public property of the data reference that holds the value originally selected from the cmbByTimeRange combo box, and is a name I selected for convenience for the code sample. Is that the name of your data reference property? If not, you need to modify the code accordingly.

                           

                          It works for unit of measure and all i have declared is;

                          cmbSourceUnit.InitializeObject(new PISystems().DefaultPISystem.UOMDatabase);

                          What works for unit of measure? The code you specified there will initialise the combo box with the entries from the UOM database - are you saying that this also automatically selects the saved selection that is stored in the data reference ConfigString?

                           

                          Is the there a way to declare AFSummaryType the same way?

                          Is this not what the following code is doing?

                           

                          cmbByTimeRange.DataSource = Enum.GetValues(typeof(AFSummaryTypes));
                          

                           

                          If you can clarify what it is you are trying to accomplish in a bigger picture way, it will make it easier to provide answers that will help you get there.

                           

                          Cheers,

                          John

                  • Re: Winform reloaded
                    Eugene Lee

                    Hi Alan,

                     

                    Your main error is to LoadComboboxes after assigning something to the text. You just have to switch the order of these two lines.

                     

                    cmbByTimeRange.Text = dataReference.TimeRange;

                    LoadComboBoxes();

                     

                    to

                     

                    LoadComboBoxes();

                    cmbByTimeRange.Text = dataReference.TimeRange;

                     

                    John's approach adds some validation logic which is useful as well.

                    2 of 2 people found this helpful