Home All Groups Group Topic Archive Search About

WithEvents code in module cannot change textbox on main form

Author
14 Jun 2006 2:57 PM
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

Author
14 Jun 2006 3:04 PM
IdleBrain
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

J***@GlobalF.com wrote:
Show quoteHide quote
> 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:
> I do know that the event fires because if i put a breakpoint on the
> line, it breaks when a message arrives.
Author
14 Jun 2006 3:55 PM
JoelH
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?
Author
15 Jun 2006 1:33 PM
Grumpy Aero Guy
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...



--
Grumpy Aero Guy



<Jo***@GlobalF.com> wrote in message
Show quoteHide quote
news:1150297076.360777.166940@f6g2000cwb.googlegroups.com...
> 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
>