views:

57

answers:

2

I want to make a datawindow field accept only positive numbers. How can I do that?

Edit

I know I can validate the column using column specification's validation expression system. But is there any format available which I can put into the Format property and be done with it?

+1  A: 

No, the simple solution is a validation rule but a more complicated solution to really filter the key pressed is possible.

First you map a userevent (ue_nonnegative) to the datawindow control event pbm_dwnkey.

double ld_value
long  ll_row

if this.getcolumnname() = "value" then
    if key = KeySubtract! or key = KeyDash! then
         ll_row = this.GetRow()
         ld_value = this.getitemnumber(ll_row, "value")
        yield() 
         post event ue_filtervalue(ll_row, ld_value)
    else
        yield() 
        accepttext()
    end if
end if

Here the datawindow has a column named value. I check if the "-" is pressed, if yes I keep the current value and a new event ue_filtervalue is posted. A yield() is necessary to make sure that the ue_filtervalue event will happen after the remaining datawindow event.

The ue_filtervalue event will restore the previous value without the "-".

this.setitem(ad_row, "value", ad_value)
this.SelectText( Len( String(ad_value)) + 1, Len( String(ad_value)) )
RealHowTo
What does the yield function do?
Night Shade
yield permits the event processing in the waiting queue. This is necessary to let other datawindow event to be executed before doing our filtering.
RealHowTo
I recommend naming pbm_dwnkey _key_ for consistency with the PB controls that have a _key_ event. You also don't want to end up with the same event having different names in different DataWindows.
Hugh Brackett
@hugh, I disagree. The standard practice in the PB world is to prefix user defined event with "ue_".
RealHowTo
It's _not_ a user-defined event, it's built-in. The proof of that is when you map the event, you get arguments supplied by PowerBuilder. That doesn't happen with user-defined events. It's the same relabeled windows message that other PB controls call `key`. To name it anything else throws polymorphism out the window.
Hugh Brackett
+1  A: 

If you use an editmask that begins with + it will only accept positive values. The plus sign displays, which you may find undesirable.

Hugh Brackett