Search code examples
comboboxlistboxvisual-foxprofoxpro

How can i populate a listbox in a selected combobox in vfp?


For example, combobox had data of categories in table1, and i want to populate the listbox from the second table with subcategory where categories = combobox.


Solution

  • What a coincidence (?), I just answered a similar question. This code is from that answer, modified slightly to use Northwind sample database's Categories and Products (subcategory for you) tables:

    Public oForm
    oForm = Createobject('SampleForm')
    oForm.Show()
    
    
    Define Class SampleForm As Form
        Height = 800
        Width=600
        DataSession = 2
        Add Object cmbCategories As ComboBox With Top=10, Left=10, Width=250
        Add Object lstProducts As ListBox With Top=10, Left=280, Height=780, Width=310
    
        Procedure Init
            With This.cmbCategories
                .RowSourceType = 3 && -SQL
                .RowSource = "select CategoryName, CategoryId from ('"+;
                    _Samples+;
                    "Northwind\Categories') into cursor crsCategories nofilter"
                .ListIndex=1
            Endwith
            With This.lstProducts
                .RowSourceType = 3 && -SQL
                .RowSource = "select ProductName, ProductId from ('"+;
                    _Samples+;
                    "Northwind\Products') p"+;
                    " where p.CategoryId = crsCategories.CategoryId"+;
                    " into cursor crsProducts nofilter"
            Endwith
        Endproc
    
        Procedure cmbCategories.InteractiveChange
            With Thisform.lstProducts
                .ListIndex = 0
                .Requery()
            Endwith
        Endproc
    Enddefine