在 C# 中更改任何打开的窗体运行时的背景颜色

发布于 2024-12-02 05:15:19 字数 262 浏览 0 评论 0原文

根据这个 主题

我有一个 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 技术交流群。

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

发布评论

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

评论(5

谢绝鈎搭 2024-12-09 05:15:19

一种可能性是创建一个基本表单类,从现在开始,您可以在所有表​​单中使用它。

如果您不想更改所有表单的背景颜色,则不要让您的表单继承自 Form,而是让它们继承自 MyFormBaseClass。

没有干净的方法来了解您从什么表单打开表单,除非您在执行 myform.Show(this); 时指定了 owber 表单;

在 MyFormBaseClass 中,重写 OnLoad 并放置如下内容:

protected override OnLoad(EventArgs e)
{
    if (this.Owner is MyOwnerFormClass) { this.BackColor = Color.Blue; }
    base.OnLoad(e);
}

您也可以使用它来添加更复杂的操作,这些操作对于所有表单都是通用的。

您可以在所有项目中通过搜索和替换来替换所有表单基类。

您必须将 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:

protected override OnLoad(EventArgs e)
{
    if (this.Owner is MyOwnerFormClass) { this.BackColor = Color.Blue; }
    base.OnLoad(e);
}

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.

三寸金莲 2024-12-09 05:15:19

您必须有像这样继承 Form 类的 BaseForm 类

 public partial class BaseForm : Form
 {
    protected override void OnLoad(EventArgs e)
    {
       Color colBackColor =Properties.Settings.Default.FormsBackgroundColor;
       BackColor = colBackColor;
    }
  }

和像这样继承 BaseForm 类的 MainForm 类。

public partial class MainForm : BaseForm
{
    private void button1_Click_1(object sender, EventArgs e)
    {
            ColorDialog colorDlg = new ColorDialog();
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.FormsBackgroundColor= colorDlg.Color;
                Properties.Settings.Default.Save();
                this.BackColor = colorDlg.Color;
            }
        }    
 }

所有类都应该继承 BaseForm 类而不是 Form 类,就像我在 MainForm 类中所做的那样“公共部分类 MainForm : BaseForm

You must have BaseForm Class like this that is inheriting Form Class

 public partial class BaseForm : Form
 {
    protected override void OnLoad(EventArgs e)
    {
       Color colBackColor =Properties.Settings.Default.FormsBackgroundColor;
       BackColor = colBackColor;
    }
  }

and MainForm class like this which is inheriting BaseForm Class.

public partial class MainForm : BaseForm
{
    private void button1_Click_1(object sender, EventArgs e)
    {
            ColorDialog colorDlg = new ColorDialog();
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.FormsBackgroundColor= colorDlg.Color;
                Properties.Settings.Default.Save();
                this.BackColor = colorDlg.Color;
            }
        }    
 }

All classes should inherit BaseForm Class instead of Form Class like i did in MainForm class here "public partial class MainForm : BaseForm"

别闹i 2024-12-09 05:15:19

你是这个意思吗? :
在菜单条中单击事件处理程序,您实际上要在其中拍摄表单。

Form myNewForm = new Form(); 
myNewForm .BackColor = Color.X;

显示表单后...

Do you mean this ? :
In menu strip click event handler, where you actually going to shot the form..

Form myNewForm = new Form(); 
myNewForm .BackColor = Color.X;

After show your form...

梦幻的味道 2024-12-09 05:15:19

首先,您必须将所有打开的表单存储在某种集合中。例如:

private List<Form> _childForms = new List<Form>();

每次打开表单时,都会将其添加到此集合中并添加 关闭 处理程序,在关闭时将其删除:

void AddForm(Form toAdd)
{
   _childForms.Add(toAdd);
   toAdd.Closed += (sender, e) => { _childForms.Remove(toAdd); }
}

现在您可以使用它

void SetBackgroundColorToAllChidForms(Color color)
{
   foreach(var form in _childForms)
      form.BackColor = color;
}

来更改每个处理程序的颜色。

PS:如果将它们添加为 MDI-Children,则可以不用集合和 this.MdiChildren 来代替:

void SetBackgroundColorToAllMdiChildren(Color color)
{
   foreach(var form in this.MdiChildren)
      form.BackColor = color;
}

first you have to store all the open forms in some kind of collection. For example:

private List<Form> _childForms = new List<Form>();

every time you open a form you add it to this collection and add a closed handler to remove it when it is closed:

void AddForm(Form toAdd)
{
   _childForms.Add(toAdd);
   toAdd.Closed += (sender, e) => { _childForms.Remove(toAdd); }
}

Now you can use

void SetBackgroundColorToAllChidForms(Color color)
{
   foreach(var form in _childForms)
      form.BackColor = color;
}

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:

void SetBackgroundColorToAllMdiChildren(Color color)
{
   foreach(var form in this.MdiChildren)
      form.BackColor = color;
}
倾城月光淡如水﹏ 2024-12-09 05:15:19

目前尚不完全清楚您的意思是在 MainForm(Form1) 中打开表单。如果要将 Form 添加到 Form1 的控件,则可以使用 Form1 的 ControlAdded 事件,如下所示:

this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Control_Added);

private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
    if (e.Control is Form)
        ((Form)e.Control.BackColor = Color.Blue;
}

如果您的应用程序是 Mdi 应用程序,则可以改用 MdiChildActivate 事件。

编辑:

当然,只有当您将内部表单添加到主表单的控件中时,这种方法才有效。

不管怎样,我不认为应用程序的设计方式应该由“我不想改变 37 种方法”和寻找技巧和解决方法来驱动。对我来说,在这种情况下更好、更干净的解决方案是避免子类化、事件等,只需在主窗体中实现一个像这样的新方法:

private void PrepareAndShow(Form aForm)
{
    aForm.BackColor = Color.Blue;
    // here you can add (even in the future) all the preprocessing you want
    aForm.Show();
}

那么您当然必须修改调用它的菜单项的所有方法:

Form aFrm = new Form2();
this.PrepareAndShow(aFrm); // instead of aFrm.Show();

这样你就必须修改 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:

this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Control_Added);

private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
    if (e.Control is Form)
        ((Form)e.Control.BackColor = Color.Blue;
}

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:

private void PrepareAndShow(Form aForm)
{
    aForm.BackColor = Color.Blue;
    // here you can add (even in the future) all the preprocessing you want
    aForm.Show();
}

Then you will of course have to modify all the methods for the menu items calling it:

Form aFrm = new Form2();
this.PrepareAndShow(aFrm); // instead of aFrm.Show();

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.

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