表单加载后立即调用方法的最佳方法是什么?
我有一个 C# Windows 窗体应用程序。按照我目前的设置方式,当 Form1_Load() 运行时,它会检查已恢复的未保存数据,如果找到一些数据,它会提示用户是否要打开该数据。当程序运行时,它可以正常工作,但会立即显示消息框,并且直到用户单击“是”或“否”后才会显示主程序窗体(Form1)。我希望先弹出Form1,然后再弹出消息框提示。
现在,为了解决这个问题,我在 Form1_Load() 方法中创建了一个计时器,在 Form1_Load() 方法中启动了计时器,然后在第一个计时器 Tick 事件中执行了检查和用户提示。这项技术解决了问题,但似乎可能有更好的方法。
你们有更好的想法吗?
编辑:我想我也使用了后台工作者来做类似的事情。经历调用方法返回表单线程的所有麻烦以及所有那些废话只是为了让它延迟几毫秒,这似乎有点愚蠢!
I have a C# windows forms application. The way I currently have it set up, when Form1_Load() runs it checks for recovered unsaved data and if it finds some it prompts the user if they want to open that data. When the program runs it works alright but the message box is shown right away and the main program form (Form1) does not show until after the user clicks yes or no. I would like the Form1 to pop up first and then the message box prompt.
Now to get around this problem before I have created a timer in my Form, started the timer in the Form1_Load() method, and then performed the check and user prompt in the first Timer Tick Event. This technique solves the problem but is seems like there might be a better way.
Do you guys have any better ideas?
Edit: I think I have also used a background worker to do something similar. It just seems kinda goofy to go through all the trouble of invoking the method to back to the form thread and all that crap just to have it delayed a couple milliseconds!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用 Form1_Shown()
I would use Form1_Shown()
使用所示< /a> 事件。它似乎适合您的需要,并且仅在第一次显示表单时显示。
Use the Shown event. It seems to suit what you need, and will only display the first time the form is shown.
尝试“显示”事件:
Form.Show 事件
Try the "Shown" event:
Form.Show Event
使用 Windows.Forms.Timer 是一种良好、稳定、众所周知且易于理解的技术,可以完成您想要的操作。我会避免使用任何其他计时器对象。
表单的
Shown
事件运行良好。重载/覆盖
Show
方法。 (我更喜欢采用更好的控制技术。)在这种方法中,我将进行所需的检查。准备好后,我将调用base.Show
方法,然后执行任何其他处理,例如消息框、提示、日志记录或其他任何操作。Using a Windows.Forms.Timer is a good, stable, well-known, and easily understood technique for doing what you want. I would avoid any other timer objects.
The form's
Shown
event works well.Overload / override the
Show
method. (My preferred technique for greater control.) In this method, I would do the checking needed. When ready, I would call thebase.Show
method, then do any other processing, such as message boxes, prompts, logging, or whatever.