Is it possible that the messagebox itself causes the item to lose focus?

Try writing debug messages to a text file instead! Just to make sure...