C# 使用动态控件刷新主窗体
我正在开发一个 Windows 窗体应用程序,其主窗体上的控件可从 mySQL 数据库检索其值。当数据库更新时,我需要表单来重新绘制和刷新控件。任何人都可以推荐一个解决方案吗?在子表单上,我实例化了表单的一个新实例并处理了旧实例,但我无法对主表单执行此操作。感谢您考虑我的问题。
I'm developing a windows form application with controls on the main form that retreive their values from a mySQL database. When the database gets updated I need the form to repaint and refresh the controls. Can anyone reccommend a solution to this? On sub forms I've instantiated a new instance of the form and dispose of the old one, but I can't do that with the main form. Thank-You for considering my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是否正确理解了您的问题,但您可以将控件放在用户控件中并在主窗体中使用它。然后,您可以执行与处理表单相同的操作:处理并创建一个新表单。
这应该可以解决您的问题,但是,您可能会遇到滚动条和表单大小调整方面的一些问题。
I'm not sure if correctly understood your question, but you could put your controls inside of a user control and use-it in the main form. Then, you could do the same you did with the forms: dispose and create a new one.
This should solve your problem, however, you may have some problems with scrollbars and the resizing of your form.
如果是我,我不会将一个用户界面与另一个用户界面一起处理,因为这是用户关注的焦点。这会有点激怒用户,特别是如果更新是自动的。
假设您有诸如 gridview 之类的只读控件或使用实时值更新的标签,那么您可以设置一个时间间隔并显示秒滚动条,清楚地告诉用户何时将发生更新。
您应该考虑在所有控件(或具有所有控件的一个用户控件)上使用一种更新方法,您可以触发该方法以按时间间隔更新控件。
If it were me, I wouldn't dispose of a UI with another, that is in focus for the user. That would kind of irritate the user especially if the update is automatic.
Suppose you have view-only controls like gridview or labels updating with live values, then you can put a time interval and display a seconds ticker clearly telling the user when an update is going to happen.
You should consider having an update method on all your controls (or one usercontrol having all the controls) which you can trigger to update the controls at timed intervals.