Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distinguishing user and programmatic events

Status
Not open for further replies.

petherin

Programmer
Dec 3, 2002
13
0
0
GB
Is there a way of distinguishing between when a user clicks on a control, and a Click (or other events like Change or whatever) that are generated when some code is executed that fires the event (such as when you change the ListIndex of a combo box and its Click event is automatically fired)?

I got round this in a rather inelegant way of setting a property to 1 in the line of code just before I wrote the line that fires the Click event automatically. Then when the Click event of the control is fired, it can interrogate the property I set and 'know' that this was a code-generated event and not a user-generated event.

gObjSession.ValueChangedInCode = gObjSession.ValueChangedInCode + 1
frmMain.treMain.SelectedItem.Expanded = True
gObjSession.ValueChangedInCode = gObjSession.ValueChangedInCode - 1

There must be a more elegant way than this!
 
No, not really. There is no way for code in the event itself to tell what triggered the event. Your method could be a little cleaner, though. Try setting a boolean to true just before raising the event (or calling the event routine directly), and then clearing that event once you're in the routine. That way you don't have to worry about multiple global flags, and what happens if they get to 2, or 3, or 45 because you forgot to subtract again afterwards.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top