PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: tom cone jr on December 17, 2005, 11:23:26 PM

Title: Best practices - detecting change in text boxes
Post by: tom cone jr on December 17, 2005, 11:23:26 PM
I'm a newbie working on a flat file phone log program.  My form has a series of text boxes for display and edit of dates, names, phone numbers and messages.   Two questions come to mind:

1) what's the preferred way to detect that a change has occurred in any of the text boxes?  Is there a good reason to do it when focus leaves each field, or is it better to simply concatenate all the text before and after and compare to see if they are the same?  or something else?  Maybe there is a way to immediately detect a change as soon as a keystroke occurs?  This could be used to enable or disable some of the buttons I'm planning... like Save and Cancel.

2) If checking after focus changes away from a field is recommended how in the heck do I detect that the user has tabbed out of the field?  ( a simple example will help a bunch)

Thanks.

-- tom
Title: Re: Best practices - detecting change in text boxes
Post by: TechSupport on December 18, 2005, 03:07:24 PM
Hi Tom,

You can detect text changes in TextBoxes through the EN_CHANGE message:

Function FORM1_TEXT1_EN_CHANGE ( _
                              ControlIndex   As Long,  _  ' index in Control Array
                              hWndForm       As Dword, _  ' handle of Form
                              hWndControl    As Dword, _  ' handle of Control
                              idTextControl  As Long   _  ' identifier of text control
                              ) As Long

Local sText As String

sText = FF_Control_GetText( hWndControl )

End Function


Personally, I prefer to do text field error checking when the user clicks on "Save" (or something similar). Dealing with invalid text during every keypress or focus change seems to be a bit of overkill and a nuisance to the user. Sometimes that type of error checking is critical but most times for simple data entry programs you can do the checks prior to saving the data. If a field has an error then you can show a message and then set the cursor to the TextBox containing the error (ie. via the SetFocus API).
Title: Best practices - detecting change in text boxes
Post by: Roger Garstang on December 19, 2005, 10:13:24 PM
I agree, checking on everything is overkill and can cause many problems...like endless loops depending on the change.  At my current job I do collections and if I accidently enter a payment of 0 by hitting tab or clicking another control without noticing I get an endless loop of msgboxes telling me so without a way to get out of it because any click/keypress causes the check...including clicking the editbox!  You have to watch if your code changes the text too because in changing the text another EN_CHANGE is sent, so you need to set a variable on entry or something to let your function know you are still editing the text.
Title: Best practices - detecting change in text boxes
Post by: tom cone jr on December 20, 2005, 10:46:06 AM
Thanks.   -- tom