Custom Component: Combobox Property

Last one in this vein.

I have a custom component. I need to add in a property that is essentially a combobox: letting the user select from a list of items. This list isn't predetermined: it can expand or contract based on certain conditions.

Essentially, I want to display a TStringList, and know which one of the objects the user has selected.

How would I do this?

Comments

  • edited June 2019
    Alright, I'll write down what I've come up with. There's no real easy way to do this, and you have to write a custom property editor.

    First, I made a custom property.
    class CustomStringList : public TStringList
    {
       typedef TStringList inherited;
      private:
      void __fastcall SetValue( UnicodeString Value )
        {
          [indent]SelectedItem = IndexOf( Value );[/indent]
         };
         UnicodeString __fastcall GEtStrValue()
        {
             if (SelectedItem >= 0 && SelectedItem < Count )
                     return Strings[SelectedItem];
              else
             return "";
        };
    public:
       int SelectedItem;
       __property UnicodeString SelectedString = {read=GetValue, write=SetValue};
       __fastcall CustomSTringList() : TStringList {SelectedItem = -1;};
    
    }
    

    Then I had to make a custom property editor
    class CustomStringListPropertyEditor : public TfrxPropertyEditor
    {
      typedef TfrxPropertyEditor inherited;
      public:
        TfrxPropertyAttributes virtual __fastcall GetAttributes()
        {
          TfrxPropertyAttributes Results;
          Results << paReadOnly;
          Results << paValueList;
          return Results;
        };
        
        virtual void __fastcall GetValues()
        {
            // Fetch all the current available options.
            CustomStringList* SL = (CustomStringList*) GetOrdValue(); // The pointer is stored in the int value
            Values->Clear();
            Values->AddStrings( SL );
        };
    
        inline __fastcall virtual CustomStringListPropertyEditor( TfrxCusotmDesigner* Designer); TfrxPropertyEditor(Designer) {};
    };
    

    In the custom component I needed to use that class as a property in the published section
    class CustomComponent : publicTfrxComponent
    {
       private:
          CustomStringList* SL;
      __published
          __property CustomStringList* Combobox = {read=SL, write=SL};
    };
    

    And then, in the initialization of the component, you have to register the property editor (as well as the custom component)
    ....
    (frxPropertyEditors())->Register( __delphirtti(CustomStringList), NULL, "", __classid(CustomStringListPropertyEditor));
    

Leave a Comment