实例化一个表单,然后稍后找到它,而不是最初显示它
我遇到了一个对我来说很奇怪的问题,但希望对其他人来说并不那么奇怪。 :) 一些背景:我正在开发一个简单的 IM 客户端,它允许用户向多个收件人广播消息。目标是为每个接收者创建一个包含广播消息文本的聊天表单,然后仅在接收者响应广播者时才显示该表单。但是,当应用程序收到响应然后尝试查找该特定聊天会话的表单(使用 Application.OpenForms)时,它无法找到它,除非我在创建它时显示。我想避免在创建时显示此表单,因为这意味着用户将在屏幕上看到闪烁。在我显示表单之前,该表单似乎并未真正被创建,但似乎必须有一种方法可以在不先显示的情况下执行此操作。有人可以帮忙吗?
如果需要,我可以提供代码片段,但我在这篇文章中没有提供,因为这感觉更像是我的概念误解,而不是代码中的错误。提前致谢!
I am having a problem that is strange to me but hopefully is not so strange to someone else. : ) Some background: I am working on a simple IM client that allows the user to broadcast messages to multiple recipients. The goal is to create a chat form for each of the recipients containing the text of the broadcast message, then show that form only if the recipient responds to the broadcast-er. However, when the application receives a response then attempts to locate the form for that particular chat session (using Application.OpenForms) it cannot find it UNLESS I .Show at the time it is created. I would like to avoid having to show this form when it is created because this means that the user will see a flash on the screen. The form doesn't seem to really be created until I show it, but it would seem there has to be a way to do this without showing first. Can anyone assist?
I can provide code snippets if needed, I didn't in this post because this feels more like a conceptual misunderstanding on my part than a bug in the code. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用表单作为基类,而是以另一种方式执行,创建一个可以引用表单的类。这样,您将让类了解内容,并将其反映在表单上(如果已初始化),而不是相反。
您不应该依赖表单作为对象的基础。使用Application.OpenForms应该是不必要的。
等等
将您的联系人保存在某种列表中,事情应该相对简单。
(请注意,Windows 窗体不是线程安全的,如果您尝试从与主线程不同的线程更改任何控件的任何属性,将会引发异常)
Instead of using the form as a base class, do it the other way, create a class that can reference a form. That way, you'll keep the class informed of the content, and reflect it on the form (if it's initialized), not the other way around.
You shouldn't rely on Forms as a basis of your objects. Using Application.OpenForms should be unnecessary.
etc
Keep your contacts in some sort of list, and things should be relatively simple.
(Be aware that windows forms aren't thread-safe, and will throw an exception if you try to alter any properties of any of the controls from a different thread than main)
Windows 窗体具有 Hide()、Show() 和 Activate() 等方法。使用这些方法来解决您的问题。
windows form has methods like Hide(),Show() and Activate(). use these method for your problem.
为什么不在聊天会话中存储对表单的引用,并在需要显示表单时使用它来调用
.Show()
:然后您可以创建表单而不显示它,并且您不会每次想要引用它时都会有调用
Application.OpenForms
的开销。我知道这是显而易见的,但
OpenForms
不会找到尚未显示的表单,因为它未打开。Why not store a reference to the form with the chat session and use that to call
.Show()
when you need to display the form:You can then create the form without showing it and you don't have the overhead of calling
Application.OpenForms
each time you want to reference it.I know this is stating the obvious but
OpenForms
won't find a form that hasn't been shown because it's not open.由于在显示表单之前不会创建表单句柄,因此您可以手动分配它,如下所示:
As the form handle does not get created until the form is shown you can assign it manually like so: