简体   繁体   English

如何使用按钮更改表单的背景颜色

[英]How to change background color of form with a button

So I am in VB and I'm trying to create some buttons so when you click them, it changes the background of my form.所以我在 VB 中,我正在尝试创建一些按钮,所以当你点击它们时,它会改变我的表单的背景。 Instead of this happening, the background of the button changes.按钮的背景不会发生这种情况,而是发生了变化。 How do I fix this?我该如何解决?

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.BackColor = System.Drawing.Color.Black;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button2.BackColor = System.Drawing.Color.Green;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.button3.BackColor = System.Drawing.Color.Red;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.button4.BackColor = System.Drawing.Color.Blue;
        }
    }
}

You can try the following code with just a single button press to achieve your requirement:您只需按一下按钮即可尝试以下代码来满足您的要求:

Private Sub RandomColor()
        Dim rand As New Random
        BackColor = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0,
            256))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RandomColor()
End Sub

Hope it helps you.希望它可以帮助你。

随机背景颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM