Advanced PowerBuilder

HomePrevious Lesson: Appending the New Result Set to the DataWindow
Next Lesson: TAB Key Behavior to the ENTER Key in a DataWindow

Appending a Row Automatically

When the user is on the last row and last column, pressing the down arrow key or ENTER key or TAB key does not automatically append a new row to the DataWindow. If you need that kind of functionality, we need to write script to do that.

Pressing the TAB key may or may not fire ItemChanged event depending on whether user changed the data or not. Please recall that ItemChanged event is fired only when the user changes the data and change the focus to another column/row within the same DataWindow. It does not fire ItemFocusChanged event because it is the last row and last column in the DataWindow, meaning nothing will happen by pressing the TAB key since there is no further field to go.

What you need to do is, declare a user-defined event ue_TabDownOut and map it to 'pbm_dwnTabDownOut event id and write the following code.
// Object: dw_product
// Event: ue_TabDownOut
long ll_RowNo
int  li_ColCount
li_ColCount = integer(this.object.DataWindow.Column.Count )
IF this.GetRow() = this.RowCount() and &
   this.GetColumn() = li_ColCount THEN
   ll_RowNo = this.InsertRow(0) // append a row.
   this.SetRow( ll_RowNo ) // set the focus to the new row.
   // SetRow doesn't take you to that row, 
   //        so, scroll to that row.
   this.ScrollToRow( ll_RowNo ) 
   this.SetColumn( 1 ) // set the focus to the 1st col.
END IF

The IF condition is checking whether the current row is equal to the total row count in the DataWindow and the current column number is equal to the total column count in the DataWindow. If so, it is appending a row to the DataWindow by passing zero as argument to the InsertRow() function and setting the focus to the first column in the last row.

This logic may not work with all DataWindows, especially when the first column's tab order is zero or it is protected. It worked well in the version I tested, but it is better not taking it for granted behavior. You have two options in that situation. First and easy one is, don't set the column to 1, rather leave to the existing column number. Second one, where you need to write some code is, in a loop check for the column's tab order and protection status and set the column's focus to the first column that has some tab order and not protected.
// Object: dw_product
// Event:  ue_TabDownOut
// Replace SetColumn() line with the following code.
// Insert the following 1 line to the variable 
//        declaration block.
int i, li_TabOrder, li_Protection
FOR i = 1 TO li_ColCount
    li_TabOrder = &
    integer( this.Describe( "#" + String(i) + &
           ".TabSequence" ))
    li_Protection = &
    integer( this.Describe( "#" + String(i) + ".Protect" ))
    IF li_TabOrder > 0 AND li_Protection = 0 THEN
       this.SetColumn( i )
       EXIT
    END IF
NEXT

Please note that, when the above code is triggered when the focus is on the last row and user presses ENTER or down arrow key. Similarly, as explained earlier, user can also press TAB key when the focus is on the last column in the last row. Don't you think it is a good idea to provide this functionality when (s)he presses TAB key also. If you agree with me, then declare a user-defined event ue_TabOut and map it to pbm_dwnTabOut event id and write the same code listed in this section. Alternately, you may want to just trigger the other event instead of duplicating the code.

Let's enhance the functionality by removing some code listed above. Yes, we want to remove some code to enhance the functionality. The above code inserts new row when the focus is on the last column on the last row. What if, if the focus is on some column in the last row and user presses down arrow key? The above code doesn't work. What you need to do to provide that functionality is, you need to remove the IF condition that is checking the row & column number.
HomePrevious Lesson: Appending the New Result Set to the DataWindow
Next Lesson: TAB Key Behavior to the ENTER Key in a DataWindow