Hello,
here I have a question on how to use Listboxes that work on the
PrimaryBizobj. I use the technique described on the wiki (how to
populate lists) to set up a list box. It's DataSource is the
PrimaryBizobj, its DataField the
primary key of that table. On the form there are other controls. I
would expect that as I move through the listbox, that these other
controls gets updated. They don't.

Then I tried and bound the ValueChanged property of the listbox to a
method which calls moveToPK and update (see below). That gives weird
results. It seems that the primary key of the current record gets
overwritten and then the cursor is moved as requested.

Any hint?

Kind regards,
Karsten.

#!/usr/bin/env python
# -*- coding: iso8859_15 -*-

version = dict(version="0.1", revision="1", stage="development")

import dabo
import dabo.dEvents as ev
dabo.ui.loadUI("wx")
from dabo.dLocalize import _

class BizDynListbox(dabo.biz.dBizobj):
    def initProperties(self):
        """This is the place for specifying how to interpret the table.
           You can set the datatype for the fields, set the type of
           primary key you are using, and set default values."""
        self.Caption = _("BizObj for table 'textdata'")
        self.DataSource = "textdata"
        self.KeyField = "id"
        self.AutoPopulatePK = True
        self.DataStructure = (
                ("id", "I", True, "dt", "id"),
                ("textval", "T", False, "dt", "textval"),
        )
        self.DefaultValues = {}

class FrmDynListbox(dabo.ui.dForm):
    def afterInit(self):
        """This is the place to insert all data controls into the form."""
        self.Sizer = dabo.ui.dSizer("v")

        # put a panel in the back. This enables Tab/Shift-Tab functionality
        # between the controls.
        panel = dabo.ui.dPanel(self)
        self.Sizer.append1x(panel)
        panel.Sizer = dabo.ui.dSizer("v")

        # display the id
        ui = self.addObject(dabo.ui.dTextBox, RegID="tbId",
DataSource="textdata", DataField="id", Enabled=False)
        panel.Sizer.append(ui, halign="Center", border=5)

        # a textbox to edit the textval
        ui = dabo.ui.dTextBox(panel, RegID="tbTextval",
DataSource="textdata", DataField="textval")
        panel.Sizer.append(ui, halign="Center", border=5)

        # the listbox
        ui = dabo.ui.dListBox(panel, ValueMode="Key",
DataSource="textdata", DataField="id", RegID="lbTextval")
        panel.Sizer.append1x(ui, halign="center")
        self.lbTextval.bindEvent(ev.ValueChanged, self.onLbTextvalValueChanged)

    def createBizobjs(self):
        """This is the place to create all bizobjs for the application."""
        conn = self.Application.getConnectionByName("myconn")
        biz = BizDynListbox(conn)
        biz.addFrom("textdata")
        biz.addField("id")
        biz.addField("textval")
        self.addBizobj(biz) # the first added bizobj is the PrimaryBizobj

    def afterInitAll(self):
        self.requery()
        k = []
        c = []
        ds = self.PrimaryBizobj.getDataSet()
        for t in ds:
            k.append(t["id"])
            c.append(t["textval"])
        self.lbTextval.Keys = k
        self.lbTextval.Choices = c

    def onLbTextvalValueChanged(self, evt):
        k = self.lbTextval.KeyValue
        b = self.PrimaryBizobj
        print "moving to ", k
        print "dataset before: ", b.getDataSet()
        if k is not None:
            b.moveToPK(k)
            dabo.ui.callAfter(self.update)
            print "dataset after: ", b.getDataSet()

if __name__ == "__main__":
    from dabo.dApp import dApp
    from dabo.dLocalize import _
    from dabo.db.dConnectInfo import dConnectInfo

    app = dApp()

    # Manages how preferences are saved
    app.BasePrefKey = "dabo.app.minimal.dtForm"
    dabo.settings.MDI = False
    app.MainFormClass = FrmDynListbox

    ## The following information can be used in various places in your app:
    app.setAppInfo("appShortName", _("DynListbox Form"))
    app.setAppInfo("appName", _("Dabo Minimal Example: DynListboxForm"))
    app.setAppInfo("appDescription", _("Dabo demo program
demonstrating the use of listboxes."))

    ## Set appVersion and appRevision from __version__.py:
    app.setAppInfo("appVersion", version["version"])
    app.setAppInfo("appRevision", version["revision"])

    ## Connections:
    if version["stage"] == "development":
        dbFileName = ":memory:"
        connInfo = dConnectInfo(Name="myconn", DbType="SQLite",
Database=dbFileName)
        app.addConnectInfo(connInfo)

    ## create database on the fly:
    # this is uncommon for real applications, as you will usually connect to
    # a populated database.
    conn = app.getConnectionByName("myconn")
    curs= conn.getDaboCursor()
    # create tables and testdata
    scriptList = """
        create table textdata (
            id integer primary key autoincrement not null,
            textval text
        );
        insert into textdata (textval) values ('spring');
        insert into textdata (textval) values ('summer');
        insert into textdata (textval) values ('autumn');
        """.split(";")
    for stmt in scriptList:
        if stmt.strip():
            curs.execute(stmt)
            dabo.infoLog.write(stmt)

    ## start the app
    app.setup()
    app.start()


_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/[EMAIL PROTECTED]

Reply via email to