vb.net 用于在所有形式中更改背景颜色的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24986849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:33:51  来源:igfitidea点击:

Button to change background color in all forms

vb.netvisual-studio

提问by maollies

I only studied a bit of visual basic at school and I'm trying to do something on my own now, sorry if it's a dumb question.

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

I created a few buttons which change the background color using this:

我创建了几个按钮,它们使用此更改背景颜色:

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

Yet, I don't know how to make it change the color on other forms. That's the only one where you have the option to choose, so, if, for example, you chose blue, the following forms would have a blue background too.

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

The only way I can think of is writing in the beginning of each form an if clause stating that if that button was clicked on the previous form, then it should have that color, but that seems a lot of code... can anyone help? I'm sorry if you don't understand what I said or I said it incorrectly...

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

回答by yu_ominae

I would set a global variable called BackgroundColorsomewhere in a module, so it can be accessed by all forms. When you click the button, you set the value of the variable. Either true falce, if you just toggle between two colours, or to the actual value of your new background colour Then when you load a new form, have it set it's bakground colour from the value of the variable.

我会BackgroundColor在模块中的某处设置一个全局变量,以便所有表单都可以访问它。当您单击按钮时,您设置了变量的值。如果你只是在两种颜色之间切换,要么是真正的幻觉,要么是新背景颜色的实际值然后当你加载一个新表单时,让它根据变量的值设置它的背景颜色。

It's the simple option. There are more complicated ones... I can elaborate a couple of other ways if you want, just let me know.

这是简单的选择。还有更复杂的......如果你愿意,我可以详细说明其他几种方法,让我知道。

回答by Hyman Gajanan

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

在模块中创建一个公共属性,所有表单都将使用该属性在加载事件中设置初始背景颜色。当您设置此属性的颜色时,更改所有打开表单的背景颜色

Property in Module

模块中的属性

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

set background color for each from in its load event

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

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

on button click to set background color

在按钮上单击以设置背景颜色

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

回答by Hyman Gajanan

In VB, that's really easy. You just have to reference your other form. Instead of just saying BackColor = Color.DeepSkyBlueyou reference that property by adding the form name...so YourMainForm.BackColor = Color.DeepSkyBlue.

在VB中,这真的很容易。你只需要参考你的其他表格。而不是仅仅说BackColor = Color.DeepSkyBlue您通过添加表单名称来引用该属性...so YourMainForm.BackColor = Color.DeepSkyBlue

回答by Suji

Add a module item from Project--> add new item --> common items--> modulewrite the code in the module

Project中添加一个模块项-->添加新项-->常用项-->模块模块中编写代码

Module Module1
    Public Sub form_background(ByVal frm As Form)' pass current form as system.controls.form
        frm.BackColor = Color.Azure ' Set the back ground color as .Azure
    End Sub
End Module

Call the function in each page you are adding, like the following

在您添加的每个页面中调用该函数,如下所示

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    form_background(Me)
End Sub

回答by Simon Dolby

In the button click code, you could just do the following

在按钮单击代码中,您可以执行以下操作

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

回答by Mohamed Adam

To change background colorsof all forms:

要更改所有表格的背景颜色

  • Create a table to store color names.
  • In a module, create a global variable and give it a value that is selected from the table using a combo to use more than one color.
  • Call any form to load the variable and set the form background color properties to that variable.
  • 创建一个表来存储颜色名称。
  • 在模块中,创建一个全局变量并为其指定一个值,该值使用组合从表中选择以使用多种颜色。
  • 调用任何表单来加载变量并将表单背景颜色属性设置为该变量。