0

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 different color than before. That is what I want.

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 most importantly, 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?

Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
Ryan
  • 3
  • 2
  • 7

2 Answers2

2

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
The Blue Dog
  • 2,475
  • 3
  • 19
  • 25
  • Can you tell me what "Static" does? And m_Rnd? And Color.FromArgb? I want to know what I'm doing, so I can use them again if I need to. – Ryan Feb 14 '15 at 21:27
  • From the MSDN - [Static](https://msdn.microsoft.com/en-us/library/z2cty7t8.aspx), [Color.FromArgb](https://msdn.microsoft.com/en-us/library/cce5h557%28v=vs.110%29.aspx), `m_Rnd` is just a variable. Not trying to be rude at all, but the MSDN has all the answers you need. – The Blue Dog Feb 14 '15 at 21:41
  • As to why the random number generator is declared as Static, see the second answer [here](http://stackoverflow.com/questions/18676/random-int-in-vb-net). – The Blue Dog Feb 14 '15 at 21:51
  • Thank you for your help. I appreciate it greatly. I'll look into finding out what each thing I'm typing means on MSDN. Currently reading [this](https://msdn.microsoft.com/en-us/library/z2cty7t8.aspx) article. Thanks again for the assistance. :) – Ryan Feb 14 '15 at 23:02
-2
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

  • Does this do the same thing as the other suggestion? Changes the labels color to a random color? I don't understand why two different codes would do the same thing. If anyone wants to explain exactly what these codes do step by step, I would greatly appreciate it. I'm trying to learn as much as I can. That way I can type my own code, and so I can answer questions for others by showing them what to do. I don't know what Dim does, what mybrush means, what Brushes.Black does, what ColorDialog() means, or what As New does. And I'm trying to learn. On MSDN the definitions are still complicated. – Ryan Feb 15 '15 at 22:34
  • it does exactly the same but you can see all see the colour chart ,before you pick a color,,run to see for yourself.... so.keyword''' dim'' tells the computer you are about to 'declare' a 'variable',, a variable is piece of storage in the computer where you will store a value ,,in this case variable is called my brush ....you can call the variable anything u desire, instead you create a variable name that makes sense, that's why you write ,,dim mybrush = brushes.black ..brushes .black is predefined.http://www.vbtutor.net/vb2008book/vb2008me_preview.pdf – Barry Carroll Feb 17 '15 at 20:24
  • Thank you so much for that Barry. I appreciate it. :) I have another question if you want to give it a go. http://stackoverflow.com/questions/28551160/how-to-create-a-custom-button-specifically-the-exact-button-from-facebook-sign – Ryan Feb 17 '15 at 20:58