0

I always get an exception when I try to switch between different forms in my program. Maybe you will help me to solve this issue. Here is the exception message:

Control.Invoke must be used to interact with controls created on a separate thread

I have attached the forms to very nice variables and this problem occurs when I try to use command like MyForm.Show().

It does not happen when the forms are not attached to variables, but then I have collosal problems with refreshing the textboxes and stuff.

Hope to hear you soon!


edit;

I have 4 different forms. When I load the main module and main form, in the Sub (...) Handles MyBase.Load I execute the following code:

In module:

Public StartupForm As frmStartup
Public RegularForm As frmRegularUse
Public LoginForm As frmLogin
Public PasswordForm As frmPassword
Public SettingsForm As frmSettings

In main form:

RegularForm = Me
StartupForm = frmStartup
LoginForm = frmLogin
PasswordForm = frmPassword
SettingsForm = frmSettings

This is the aproach I worked out to get the full control over refreshing the forms. It is a program for Motorola Scanner with Windows CE. Now, for example, when I enter the correct password in LoginForm, I want to switch to the RegularForm. When I try to use RegularForm.Show() or RegularForm.ShowDialog or RegularForm.BringToFront(), I get an exception. When I try to call the form with the frmRegularUse.Show() I can call the form, but it is being created in a different thread, I believe, so I loose control over it (when I try to put something from the keyboard, there is no response).

jarek
  • 1,381
  • 1
  • 9
  • 6

2 Answers2

1

I doubt the forms are getting created in a different thread, but if they are then STOP, go back, and fix that. All of your forms should be created and accessed from the main GUI thread. Secondly, I don't think you "newed" the forms. You need something like this:

StartupForm = New frmStartup 
RegularForm = New frmRegularUse 
LoginForm = New frmLogin 
PasswordForm = New frmPassword 
SettingsForm = New frmSettings 
tcarvin
  • 10,715
  • 3
  • 31
  • 52
  • "The targeted version of the .NET Compact Framework does not support latebinding" ;-) – jarek Sep 27 '11 at 13:20
  • Yes, the VB.NET 'form type = form object' feature has gotten a lot of vb programmers in deep trouble when they try threading. Invoke STOP first, frmStartup.BeginInvoke() does *not* work. – Hans Passant Sep 27 '11 at 13:31
  • @jarek I'm not sure what you are trying to say, but I code for Moto and Intermec embedded devices all the time. If you want a single named instance of your forms then you will need to include that code in your Sub Main. In the call to Application.Run() pass the singleton that is your main form...don't new it again. – tcarvin Sep 27 '11 at 17:15
0

Actually, what I did is:
Still I have the same code in the main module, which is:

Public StartupForm As frmStartup
Public RegularForm As frmRegularUse
Public LoginForm As frmLogin
Public PasswordForm As frmPassword
Public SettingsForm As frmSettings

I managed it to work in the simpliest possible way. For example - I run the Login form, and execute the following code (long story short):

LoginForm = Me
frmRegularUse.ShowDialog()

I jump to the frmRegularUse form, where, once again, I execute:

RegularForm = Me
frmPasswordForm.ShowDialog()

And so on...
I made some tests and it works quite okay. Tomorrow I will try to make it a little bit more sophisticated. ;-)

jarek
  • 1,381
  • 1
  • 9
  • 6