Manipulation to the drop-down list box in powerbuilder and data association can



Using powerbuilder engaged in the process of database development, often run to use the list box to select the data, but the average drop-down list box does not provide the methods and data connected, while the use of data sub-window drop-down list box to place it looks a little awkward. Here are a kind of good-looking and easy to use way.

We have thought it possible to use a pointer to the data to connect to a select statement from a query result fetch out, then put the data to the drop-down list box to additem.

Although this is a feasible approach, but his speed was not satisfactory, and too affected by the amount of data.

We can use the data window powerbuilder most respected technology as a database and drop-down list box to connect the bridge, the method that is convenient for fast, safe and reliable.

Well, make the following drop-down list box, the completed will bring many unexpected convenience!

1. Open powerbuilder of the "select user object" brush, select , in the "new user object", select "Visual" category of the "Standard" and press the , in pop up the "Select Standard Visual Type" dialog box, select the list of "dropdownlistbox" and press the . Then add some instance variables, select "Declare" menu item in the "Instance Variables .."
Adding variable declaration: datastore ids_user int il_row and press .
We object to some of the new restrictions on the object's "constructor" incident in the accession process:
sorted = false / / sentence is necessary
reset ()

2. Well, now the most critical time, we want to retrieve the data out into ids_user variable.

Select the menu "Declare" item "User Object Function", in pop-up dialog box to create a new object function by pressing name:
init
Parameter a:
sqlstr string
Return value integer: integer
Now we need to dynamically create a datastore
In the init function, wrote:
/ / Integer init (sqlstr string) begin
long ll_rowcount
long ll_row
string ls_sql
string errors
string ls_return
ids_user = create datastore
/ / Sql statement is generated according to the data window
ls_return = sqlca.SyntaxFromSQL (sqlstr,''style (type = grid)'', errors)
/ / Sql statement is wrong
if len (errors)> 0 then
messagebox (''error'', errors)
return -1
end if
/ / Dynamically create the data window according to the syntax
ids_user.create (ls_return)
ids_user.settransobject (sqlca)
ll_rowcount = ids_user.retrieve ()
/ / No data return
if ll_rowcount = 0 then
return -1
end if
/ / Put the data into the control
for ll_row = 1 to ll_rowcount
this.additem (string (ids_user.Object.Data [ll_row, 1]))
next
/ / Returns 0 on success
return 0
/ / Integer init (sqlstr string) end

Now the drop-down list box has to display data, and we select data Di Zai's not what it needs at the same time often show up in the value, as display of a persons name, and what we need is job number, can then use the select statement Ba needs The data retrieval out? with the following procedures, we do not have a headache.

Repeat the above steps to establish the function, create a new function called getdata the user object,
Return value is "any" written procedures in function:
/ / Any getdata () begin
any la_return
long ll_row
if text =''''then
return''''
end if
/ / Did not choose or not to retrieve data
if ids_user.rowcount () = 0 or il_row = 0 then
return''''
end if
/ / Return default data in the second column
la_return = ids_user.Object.data [il_row, 2]
return la_return
/ / Andy getdata () end

Now can not return the correct data, also need a function setidx (idx int)
Procedures are as follows:
/ / Setidx (idx int) begin
il_row = idx
/ / Setidx (idx int) end

Now the user object has been completed, and we try to do what test.
Create a new application app_test, a window w_main, according to the method just create
Build a user object: uo_dbdropdownlistbox, into w_main in
The open event in app_test written statement to connect the database and add open (w_main)
The open event in w_main wrote:
int li_result
li_result = uo_1.init (''select name, id from sysobjects'')
The selectionchanged event in uo_1 wrote:
setidx (index)
messagebox (''tips'', "see" + string (this.getdata ()))
Where our user objects have been chosen according to the corresponding id of the name to return.
For more features, yet fans of powerbuilder thinking constantly created. If you want in-depth discussion, please contact me: fly-back@163.com