是否有在将任何内容绘制到屏幕之前调用的 Form 事件?

发布于 2024-09-24 12:05:29 字数 359 浏览 12 评论 0原文

我尝试重写 OnLoad 事件,但在此方法完成之前表单已被绘制。我正在调用 base.OnLoad 方法。但是,在事件期间,表单被部分绘制(可以看到伪像)。我注意到这一点是因为我正在访问数据库并且需要一些时间。在这种情况下,我将获取一些数据并将其绑定到表单的控件。请不要告诉我使用单独的线程。为简单起见,我宁愿在加载数据时仅显示繁忙的光标。

更新:

好的,我想你们已经说服了我。我将使用一个单独的线程。我不知道BackgroundWorker,它很容易实现。现在我的表单正在快速加载。然后,突然间我的组合框就被填充了。但是,我想阻止用户在填充组合之前单击组合。使用 Winforms 执行此操作的最佳方法/标准方法是什么?有没有办法在后台工作完成之前关闭表单中的输入事件?

I've tried overriding the OnLoad event, but the form is getting drawn before this method finishes. I am calling the base.OnLoad method. But, the form is partially getting drawn (artifacts are seen) during the event. I notice this because I'm hitting the database and it is taking some time. In the event, I'm getting some data and binding it to the form's controls. Please don't tell me to use a separate thread. For simplicity, I would rather just show a busy cursor while the data is being loaded.

UPDATE:

Ok, I think you guys/gals have convinced me. I'll use a separate thread. I wasn't aware of the BackgroundWorker and it was very easy to implement. Now my form is loading quickly. And then, all of a sudden my combo boxes are populated. But, I'd like prevent the user from clicking on the combos before they're populated. What is the best way/standard way of doing this using Winforms? Is there a way to turn off input events in the form until the background worker is finished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情域 2024-10-01 12:05:30

我建议您在开始加载之前用 Loading 标签覆盖表单。

I would recommend that you cover the form with a Loading label before you start loading.

囚我心虐我身 2024-10-01 12:05:30

您应该能够通过将加载放在调用 IntializeComponent() 之前的构造函数代码中来解决该问题。此时,窗体上的控件尚未创建(因为这是 InitializeComponent 所做的)。

然而,这个阶段还看不到形式。如果你想显示一个空白表单,那么我认为一个可能的解决方案(我没有尝试过,但我认为它应该可行)是调用 this.Show() (以显示form) 和 Application.DoEvents() 让 WinForms 处理事件并显示表单。

You should be able to solve the problem by placing your loading in the constructor code before the call to IntializeComponent(). At this point, the controls on the form have not yet been created (because this is what InitializeComponent does).

However, the form is also not yet visible in this phase. If you want to show a blank form, then I think a possible solution (I haven't tried that, but I think it should work) would be to call this.Show() (to display the form) and Application.DoEvents() to let WinForms process events and display the form.

波浪屿的海角声 2024-10-01 12:05:30

您可以尝试在表单的构造函数中执行昂贵的操作,以便在显示表单时,它已经拥有需要呈现的数据。另请查看 SuspendLayout/ResumeLayout 方法。

但这些解决方案都不会像使用不同的线程执行昂贵的操作那样优雅。

You could try doing your expensive operations in the form's constructor, so that when it's time to show the form, it already has the data it needs to render. Also look into SuspendLayout/ResumeLayout methods.

But none of these solutions will be as graceful as using a different thread to perform expensive operations.

假情假意假温柔 2024-10-01 12:05:30

我不确定这是否有帮助,但 Move 事件在 Load 之前调用。

I am not sure if this will help or not, but the Move event is called before Load.

可爱暴击 2024-10-01 12:05:30

Shown 事件对此有好处。您的表单将完全显示,然后触发 Shown 事件。这将为用户提供一个干净的屏幕,在加载数据时不会出现部分绘制的字段。

在 Shown 事件处理程序中,打开沙漏,完成工作,然后关闭沙漏。

The Shown event is good for this. Your form will be completely displayed, then the Shown event is fired. This will give the user a clean screen without partially drawn fields while you load your data.

In your Shown event handler, turn on the hourglass, do your work and then turn off the hourglass.

诗化ㄋ丶相逢 2024-10-01 12:05:30

ComboBox 具有 BeginUpdate()EndUpdate(),可以在向控件添加大量数据或缓慢数据时调用它们。表单上的 SuspendLayout() 和 'ResumeLayout()` 也可能有助于解决重绘问题。

如果您只想阻止用户单击该控件,您还可以禁用该控件。如果禁用表单本身,所有包含的控件也将被禁用。

如果您使用后台线程,则必须确保在启动线程之前从主 UI 线程调用这些线程,并在后台工作完成后再次从主 UI 线程调用这些线程。

The ComboBox has a BeginUpdate() and EndUpdate() that can be called when adding large amounts of data or slow data to the control. SuspendLayout() and 'ResumeLayout()` on the form may also help with your redraw issues.

You can also disable the control, if all you want is to prevent the user from clicking it. If you disable the form itself, all contained controls will also be disabled.

If you're using background threads, you'll have to make sure you call these from the main UI thread before starting the thread, and again from the main UI thread when the background worker is complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文