WithEvents code in module cannot change textbox on main form

  • Thread starter Thread starter JoelH
  • Start date Start date
J

JoelH

This is really weird...
I'm using Visual Basic 2005 Express
I have a form - Form1 - with a control called txtStatusBox

I have a module attached to the solution for storing some other code.
This links to an Outlook MAPI folder (variable ContItems), that waits
for new items to arrive.
When a new message is delivered, the event is fired.
However, when it tries to change the text in a textbox on Form1, the
box remains unchanged:

Private Sub ConItems_ItemAdd(ByVal Item As Object) Handles
ConItems.ItemAdd
MyForm.txtStatusBox.Text="text has changed"
End Sub

I do know that the event fires because if i put a breakpoint on the
line, it breaks when a message arrives.

If I call another sub that lies within the same module, such as

Public Sub ChangeTextOnForm()
MyForm.txtStatusBox.Text="text has changed"
End Sub

then it DOES work. So, both subs are in the same module, and have the
same code, and neither reports an error. But for some reason, the code
does not work for the even procudure.
If I try to call the first sub from within the second, then it still
doesn't work. It's like the event code is running in a lower security
context than other code in the module.

Can anyone explain this please? it's driving me nuts!


thanks
joel
 
See if this helps:

Private Sub ConItems_ItemAdd(ByVal Item As Object) Handles
ConItems.ItemAdd
MyForm.txtStatusBox.Text="text has changed"
MyForm.txtStatusBox.Refresh()
End Sub
 
No tried that unfortunately.
It seems to be a security thing. can code in one module be restricted
from editing textboxes on another form?
the event is not fired by any user input but by an email arriving in
the inbox, so maybe the procedure runs under an anonymous user account?
 
You may need a factory class...

Create a class with a shared property referring to the form instance whose
textbox you're trying to manipulate via the module.

You'll need to "store" the form instance in the class when the form opens.

Do a search on "global variables" in help. As they do not exist in VB2005, I
thing you'll get an article or two indicating how to do this...
 
Back
Top