第二十四讲 深入窗体 进一步了解数据窗口控件 打印本页  
 
   上一讲,我们介绍了INI资源文件的应用。知道PB的编程是很灵活的。我们已经说过,PowerBuilder的强大之处在于它的数据库开发,而这一点正是由PB提供的数据窗口来实现的。因此,我们在接下来的几讲,我们将学习制作较复杂的数据窗口,同时将这些数据窗口嵌入到Window中的数据窗口控件中去。
  下面先让我们看一个例子。
  [插入video_25]
  有关retrieve事件
  retrievestart事件顾名思义就是取数开始时发生的,一旦后台开始传输数据,系统就触发该事件。
  retrieverow事件是指在取数过程中被不断触发的事件。系统在接到后台传输的数据时,会不断发出消息给该事件。
  retrieveend事件是指在取数结束时被触发的事件。
  开始编程
  建立一个新的Application对象,起名为data_window。
  打开Application对象的Script,定义全局变量,用于存储INI文件的路径,及文件名的。
  在Application对象Script中写入程序,作用是初始化,与后台数据库建立连接,同时打开主窗体界面
  string Ls_file_name
  int Li_hApp
  Li_hApp = Handle (this, TRUE)
  if Li_hApp > 0 then
  Prompt ("系统已经运行")
  HALT
  end if
  profile = Sys_Path + Profile_name
  if not fileexists(profile) then
  if GetFileOpenName ("打开文件", profile, Ls_file_name, "*.ini", "初始化文件,*.ini") <> 1 then
  Prompt ("无法打开初始化文件")
  halt
  end if
  end if
  sqlca.DBMS = ProfileString (profile, "Database", "dbms", " ")
  sqlca.Database = ProfileString (profile, "Database", "database", " ")
  sqlca.ServerName = ProfileString (profile, "Database", "servername", " ")
  sqlca.UserID = ProfileString (profile, "Database", "UserID", " ")
  sqlca.DbParm = ProfileString (profile, "Database", "DBparm", " ")
  sqlca.DBPass = ProfileString (profile, "Database", "DatabasePassword", " ")
  sqlca.Lock = ProfileString (profile, "Database", "lock", " ")
  sqlca.LogId = ProfileString (profile, "Database", "logid", " ")
  sqlca.LogPass = ProfileString (profile, "Database", "LogPassword", " ")
  
  if SQLca.SQLCode <> 0 then
  show_db_error("网络数据库连接错误")
  halt
  end if
  disconnect;
  建立一个窗体
  保存为w_datawindow。
  编写open事件
  open(w_datawindow)
  加入以下几个控件:
  增加两个单行文本编辑框控件。
  加入一个Datawindow控件,命名为“dw_browse”,选中“Hscroll Bar”和“Vscroll Bar”。单击确定按钮。
  添加一个GroupBox控件
  增加两个StaticText控件
  建立一个datawindow
  采用Grid格式,选择表customer作为数据源建立一个datawindow:
  定义两个参数:
  在Where标签页中写入查询条件:
  调整格式
  回到w_datawindow窗口界面。
  增加两个CommandButton按钮,一个起名为“cb_retrieve”,text为“&R 取数”,用于执行取数命令;另一个起名为“cb_exit”,text为“&X 退出”,用于终止程序。
  在“退出”按钮中添加一个语句:
  close(parent)
  在“取数”按钮中编写程序:
  int li_id1,li_id2
  li_id1=integer(trim(sle_1.text))
  li_id2=integer(trim(sle_2.text))
  if isnull(sle_1.text) or trim(sle_1.text)="" then
  li_id1=0
  end if
  if isnull(sle_2.text) or trim(sle_2.text)="" then
  li_id2=999
  end if
  dw_browse.settransobject(sqlca)
  int li_count
  li_count=dw_browse.retrieve(li_id1,li_id2)
  再添加两个控件:
  一个是单行文本编辑框,
  另一个是StaticText(静态文本)控件。
  再次打开cb_retrieve控件的Script,在最底行加入如下语句:
  sle_3.text=trim(string(li_count))
  控制datawindow控件中取出的数据记录数:
  选择RetrieveStart事件。打开Declare的instance Variables定义一个instance变量:
  long iv_rowcount
  iv_rowcount=1
  iv_rowcount++
  if iv_rowcount>100 then
  return(1)
  end if
  运行程序