Technical Resources at AEGIS

PowerBuilder Tips

Getting Focus on a Row with Protected Columns

Tip By: Mike Barlotta


When building an application sometimes information on a row should not be edited once the row has been saved to the database. The column containing the information can only be editable if the row is new, otherwise it is protected. This type of functionality can easily be achieved using datawindow attributes.

For each column that requires protection add this to the protect attribute:

IF(IsRowNew(), 0, 1)

This is not new and has actually been promoted as a tip before. However the following drawback can occur when using this feature. When all the columns on a retreived row are protected, the user cannot get focus on the row once they have inserted a new row. It is possible to access these rows prior to doing an insert, but afterwards clicking, using arrow keys, and SetRow do not allow the row to get focus. I am not sure if this is a PB bug or not. This can be a problem if the user needs to access the row after inserting new rows. A user may need to access a row despite not being able to edit any columns for several reasons including:

  • Need to Delete the Row
  • Need to access detailed information for the Row

To solve this problem:

When all columns are protected on a row the user can not getfocus on that row. By adding a computed column (hidden_col) that has the following properties:

  • Display Only - ON
  • Show Focus Rectangle - OFF
  • Tab Order - Highest on the DW
  • Text Color - Set to DW BackGround Color
  • BackGround Color - Set to DW BackGround Color
  • Right Justified Text
  • Make the width as small as possible

Since most columns in a result set are not in the same order as they appear in the DataWindow two instance variables are needed. These variables are used to keep track of the first column in the Tab order and the last column in the Tab Order

string is_first_col, is_last_col

These can be set programmatically or directly by the developer.

For example:

Constructor Event of the DataWindow

is_first_col = 'column name'
is_last_col = 'column name'

Add this code to the itemfocuschanged event of the DataWindow:

// This code will enable 'Regular' tab and shift tab functionality // when the user 'moves' to the hidden column long ll_rowcount, ll_Row this.SetReDraw(FALSE) ll_row = this.GetRow() ll_rowcount = this.RowCount() IF Lower(this.GetColumnName())='hidden_col' THEN IF KeyDown(KeyShift!) THEN // go back one column this.SetColumn(is_last_col) ELSEIF KeyDown(KeyTab!) THEN IF ll_row = ll_rowcount THEN // go back one column this.SetColumn(is_last_col) ELSE // go to first column on next row // if can not get to first column because it is protected - // stay on the row we are on because we moved to another // protected row. this.SetColumn(is_first_col) IF Lower(this.GetColumnName()) ='hidden_col' THEN this.SetRow(ll_row + 1) END IF END IF ELSE // Must have used mouse, arrow keys etc. // RowFocus has already changed so lets // just move to the first column this.SetColumn(is_first_col) END IF END IF this.SetReDraw(TRUE)