Is it possible that two items have the same m_iprogramCount value but one is visible and the other not? If not you could keep the return if the item is visible or break out of the loop if it is not visible.