vb.net - 用于更改所有表单背景颜色的按钮

标签 vb.net visual-studio

我在学校只学了一点视觉基础知识,现在我正在尝试自己做一些事情,如果这是一个愚蠢的问题,抱歉。

我创建了一些使用以下方法更改背景颜色的按钮:

Private Sub bgcblue_Click(sender As Object, e As EventArgs) Handles bgcblue.Click
        BackColor = Color.DeepSkyBlue
    End Sub

但是,我不知道如何让它改变其他表单上的颜色。这是您唯一可以选择的形式,因此,例如,如果您选择蓝色,则以下表单也将具有蓝色背景。

我能想到的唯一方法是在每个表单的开头编写一个 if 子句,说明如果在前一个表单上单击该按钮,那么它应该具有那种颜色,但这似乎有很多代码......有人可以帮忙吗?如果您不明白我说的话或者我说错了,我很抱歉......

最佳答案

Create a public property in module which will be used by all forms to set initial background color in there load event. when you set color of this property change background color of all open forms

模块中的属性

Module Module1
    Private m_FormBackgroundColor As Color
    Public Property FormBackgroundColor As Color
        Get
            Return m_FormBackgroundColor
        End Get
        Set(value As Color)
            m_FormBackgroundColor = value
            For Each Frm As Form In Application.OpenForms
                Frm.BackColor = m_FormBackgroundColor
            Next
        End Set
    End Property
End Module

为每个加载事件设置背景颜色

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.BackColor = FormBackgroundColor
    End Sub
End Class

点击按钮设置背景颜色

Private Sub bgcblue_Click(sender As Object, e As EventArgs) Handles bgcblue.Click
    FormBackgroundColor = Color.DeepSkyBlue
End Sub

关于vb.net - 用于更改所有表单背景颜色的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24986849/

相关文章:

c++ - 如何在 Visual Studio 中设置 MPI 中的进程数?

vb.net - 使用 LINQ 查询的 DataGridView 单元格搜索

vb.net - 类中属性的优点

javascript - Visual Studio ReSharper - 在外部模块中声明的 Typescript 自动导入类 - 使用 from 而不是 require

c++ - 调试汇编程序时返回 0 时在 visual studio 中抛出异常

c# - WPF 元素的可视化工具

xml - 根据兄弟节点的值选择 XML 节点

vb.net - VB.NET中一行代码替换字符串中的多个字符

vb.net - 如何在 TableLayoutPanel 中设置行高

sql-server - 在 Azure 上设置 Visual Studio 虚拟机时,SQL Server 的默认凭据是什么?