在 C# 中更改任何打开的窗体运行时的背景颜色
根据这个 主题
我有一个 C# 菜单条(win app) 项目,单击其中的每个项目,都会打开一个表单! 正如我所说,我有 37 个表单,我想这样做:每当通过单击菜单条的项目在 MainForm (Form1) 中打开表单时,我想设置表单的背景颜色,但我不想一一执行此操作(方便),有办法吗?!谢谢 。
according to this topic
i have a menustrip in C# (win app) project , that for clicking each item of it, a form opens!
as i said, i have 37 forms and i want to do this : whenever a form opens inside the MainForm (Form1) by clicking menustrip's items, i want to set form's back color, but i don't want to do this one by one(handy),is there a way ?! thanks .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种可能性是创建一个基本表单类,从现在开始,您可以在所有表单中使用它。
如果您不想更改所有表单的背景颜色,则不要让您的表单继承自 Form,而是让它们继承自 MyFormBaseClass。
没有干净的方法来了解您从什么表单打开表单,除非您在执行 myform.Show(this); 时指定了 owber 表单;
在 MyFormBaseClass 中,重写 OnLoad 并放置如下内容:
您也可以使用它来添加更复杂的操作,这些操作对于所有表单都是通用的。
您可以在所有项目中通过搜索和替换来替换所有表单基类。
您必须将 MyFormBaseClass 创建为普通表单才能使设计器正常工作。
按CTRL+H,选择在所有项目中替换,将“:Form”替换为“:MyFormBaseClass”,按Next直至完成,即37次。
如果需要,您也可以将 .Show() 替换为 .Show(this) 。
A possibility is to create a base form class that you can use in all your forms, from now on.
If you don't want to change your background color for all forms, instead of making your forms inheriting from Form, make them inherits from MyFormBaseClass.
There is no clean way to understand from what form you are opening a form, except you specify the owber form when you do myform.Show(this);
In MyFormBaseClass, override OnLoad and put something like this:
You can use this to add more complex operations too, all common to all your forms.
You can replace all form base classes with a search and replace in all your project.
You must create MyFormBaseClass as a normal form to keep the designer working.
Press CTRL+H, select replace in all project, replace " : Form" with " : MyFormBaseClass", press Next until you are done, that is, 37 times.
You can replace also .Show() with .Show(this) if you need.
您必须有像这样继承 Form 类的 BaseForm 类
和像这样继承 BaseForm 类的 MainForm 类。
所有类都应该继承 BaseForm 类而不是 Form 类,就像我在 MainForm 类中所做的那样“公共部分类 MainForm : BaseForm”
You must have BaseForm Class like this that is inheriting Form Class
and MainForm class like this which is inheriting BaseForm Class.
All classes should inherit BaseForm Class instead of Form Class like i did in MainForm class here "public partial class MainForm : BaseForm"
你是这个意思吗? :
在菜单条中单击事件处理程序,您实际上要在其中拍摄表单。
显示表单后...
Do you mean this ? :
In menu strip click event handler, where you actually going to shot the form..
After show your form...
首先,您必须将所有打开的表单存储在某种集合中。例如:
每次打开表单时,都会将其添加到此集合中并添加 关闭 处理程序,在关闭时将其删除:
现在您可以使用它
来更改每个处理程序的颜色。
PS:如果将它们添加为 MDI-Children,则可以不用集合和
this.MdiChildren
来代替:first you have to store all the open forms in some kind of collection. For example:
every time you open a form you add it to this collection and add a closed handler to remove it when it is closed:
Now you can use
to change the color of each of them.
PS: If you add them as MDI-Children you can do without the collection and your
this.MdiChildren
instead:目前尚不完全清楚您的意思是在 MainForm(Form1) 中打开表单。如果要将 Form 添加到 Form1 的控件,则可以使用 Form1 的 ControlAdded 事件,如下所示:
如果您的应用程序是 Mdi 应用程序,则可以改用 MdiChildActivate 事件。
编辑:
当然,只有当您将内部表单添加到主表单的控件中时,这种方法才有效。
不管怎样,我不认为应用程序的设计方式应该由“我不想改变 37 种方法”和寻找技巧和解决方法来驱动。对我来说,在这种情况下更好、更干净的解决方案是避免子类化、事件等,只需在主窗体中实现一个像这样的新方法:
那么您当然必须修改调用它的菜单项的所有方法:
这样你就必须修改 37 个方法(我猜不到 10 分钟的工作时间),但要保持你的应用程序干净,对未来的更改和扩展持开放态度。其他一切都可以很有趣,并且尝试起来很有趣,但我不会在生产代码中使用它。
It's not completely clear what you mean with a form opens inside of MainForm(Form1). If you are adding your Form to Form1's Controls, you can use the ControlAdded event of Form1 with something like this:
If yours is an Mdi app, you could use the MdiChildActivate event, instead.
EDIT:
Of course this approach will work only IF you add the inner forms to the main form's controls.
Anyway, I don't think that the way an application is designed should be driven by "I don't want to change 37 methods" and looking for tricks and workarounds. To me the better and cleaner solution in this case would be to avoid subclassing, events and whatever and simply implement a new method like this one in your main form:
Then you will of course have to modify all the methods for the menu items calling it:
In this way you have to modify 37 methods (less than 10 minutes work, I guess), but keep your application clean, open to future changes and extensions. Everything else can be fun, and interesting to experiment with, but I would not use it in production code.