窗体之间切换 (VB.NET)
当我尝试在程序中的不同形式之间切换时,我总是遇到异常。也许你会帮助我解决这个问题。以下是异常消息:
Control.Invoke 必须用于与在单独线程上创建的控件进行交互
我已将表单附加到非常好的变量,当我尝试使用像 MyForm 这样的命令时,就会出现此问题。展示()。
当表单未附加到变量时,这种情况不会发生,但是我在刷新文本框和其他内容时遇到了巨大的问题。
希望很快能听到您的声音!
编辑;
我有4种不同的形式。当我加载主模块和主窗体时,在 Sub (...) Handles MyBase.Load 中执行以下代码:
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
这是我想出的方法来完全控制刷新形式。它是适用于Windows CE 的摩托罗拉扫描仪的程序。现在,例如,当我在 LoginForm 中输入正确的密码时,我想切换到 RegularForm。当我尝试使用 RegularForm.Show() 或 RegularForm.ShowDialog 或 RegularForm.BringToFront() 时,出现异常。当我尝试使用 frmRegularUse.Show() 调用表单时,我可以调用该表单,但我相信它是在不同的线程中创建的,所以我失去了对它的控制(当我尝试从键盘输入某些内容时,没有任何反应)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑这些表单是在不同的线程中创建的,但如果它们是,则停止,返回并修复它。所有表单都应该从主 GUI 线程创建和访问。其次,我不认为你“更新”了表格。你需要这样的东西:
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:
事实上,我所做的是:
我在主模块中仍然有相同的代码,即:
我设法让它以最简单的方式工作。例如 - 我运行登录表单,并执行以下代码(长话短说):
我跳转到 frmRegularUse 表单,我再次执行:
等等...
我做了一些测试,效果很好。明天我会尝试让它变得更复杂一些。 ;-)
Actually, what I did is:
Still I have the same code in the main module, which is:
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):
I jump to the frmRegularUse form, where, once again, I execute:
And so on...
I made some tests and it works quite okay. Tomorrow I will try to make it a little bit more sophisticated. ;-)