vb.net 创建一个按钮,每次单击都会更改标签前景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28520339/
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-17 18:48:25  来源:igfitidea点击:

Creating a button that changes Label ForeColor with each click

vb.netbuttoncolorslabel

提问by Ryan

Here's what I'm trying to do. I have a label named "lblWelcome", and a button named "btnTextColor". What I want the button to do is change the labels forecolor each time the button is clicked. Each time the button is pressed the forecolor of the label will change to a differentcolor than before. That is what I want.

这就是我想要做的。我有一个名为“lblWelcome”的标签和一个名为“btnTextColor”的按钮。我希望按钮做的是每次单击按钮时更改标签前景色。每次按下按钮时,标签的前景色都会更改为与以前不同的颜色。这就是我想要的。

Here is what I've tried. Side note: I only put Red, Blue, and Black just as a start, so I could try the button and see if it works. My first hope was to get the button to choose a random color each time it's clicked. That would be perfect. Otherwise, just going through a list of colors one by one would be fine as well.

这是我尝试过的。旁注:我只把红色、蓝色和黑色作为开始,所以我可以试试这个按钮,看看它是否有效。我的第一个希望是让按钮在每次点击时选择一种随机颜色。那将是完美的。否则,只需一个一个地浏览颜色列表也可以。

Private Sub btnTextColor_Click(sender As Object, e As EventArgs) Handles btnTextColor.Click
    lblWelcome.ForeColor = Color.Red
    lblWelcome.ForeColor = Color.Blue
    lblWelcome.ForeColor = Color.Black

End Sub

With the above code, when I run the program, the button changes the labels forecolor to black. It just follows the last line of code. So, I went to the internets, looked for a solution, some bit of code I'm missing here. I found something called a "string", but it wasn't in reference to forecolor and I wasn't sure what to think or do. I'm just kind of stuck, I need to know what to add to make this button work the way I intended it to. But mostimportantly, I want to know how the solution I find works. For example, when someone tells me what to type, I'll fix my button, but I haven't learned the meaning of what I typed. I want to learn. So please, explain it a little bit when you reply to this. Just a little bit, that's all I ask. What command am I looking for? How do I use it? What does it do?

使用上面的代码,当我运行程序时,按钮将标签前景色更改为黑色。它只是在最后一行代码之后。所以,我去了互联网,寻找解决方案,我在这里遗漏了一些代码。我找到了一个叫做“字符串”的东西,但它不是指前色,我不知道该怎么想或做什么。我只是有点卡住了,我需要知道要添加什么才能使此按钮按我预期的方式工作。但重要的是,我想知道我找到的解决方案是如何工作的。例如,当有人告诉我要键入什么时,我会修复我的按钮,但我还没有了解我键入的内容的含义。我想学习。因此,请在回复此内容时稍微解释一下。一点点,这就是我要问的。我在寻找什么命令?我该如何使用它?它有什么作用?

回答by The Blue Dog

This should work for you.

这应该对你有用。

Private Sub btnTextColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTextColor.Click
    Static m_Rnd As New Random
    lblWelcome.ForeColor = Color.FromArgb(255, m_Rnd.Next(0, 255), m_Rnd.Next(0, 255), m_Rnd.Next(0, 255))
End Sub

回答by Barry Carroll

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
     Dim mybrush = Brushes.Black
    Dim cDialog As New ColorDialog()
    cDialog.Color = Label1.BackColor ' initial selection is current color.

    If (cDialog.ShowDialog() = DialogResult.OK) Then
        Label1.BackColor = cDialog.Color ' update with user selected color.
    End If


End Sub

hope this helps as a second option

希望这有助于作为第二个选择