0

Right, so for a project at my work experience placement, I have to make in vb.net a game in the style of the classic "Lights Out" (www.ebaumsworld.com/games/play/1111).

I'm still getting used to vb.net, and I've come across a very basic problem that I need a bit of help with, I'm needing so that when I click a button, e.g. Button1, that button changes from purple to black, and then when it is clicked again, it changes back to purple.

Here's how I've tried to do it:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        If Button1.BackColor = Drawing.Color.DarkViolet Then
            Button1.BackColor = Drawing.Color.Black
        ElseIf Button1.BackColor = Drawing.Color.Black Then
            Button1.BackColor = Drawing.Color.DarkViolet
        End If

    End Sub

When the page loads the button is DarkViolet, when I click it it turns black, but if I click it again it just stays back.

Again I will reiterate that I'm an absolute beginner. =]

Halp?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by codeorder
0

You probably do not have the correct colors somewhere.
Try adding the the following code and see if you get the same results:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.BackColor = Drawing.Color.DarkViolet
    End Sub

Since you will probably need help accessing more controls than one, see if the following helps.
Pre-requisites: 8 Buttons.

Public Class Form1

    '// handle more controls from one event.
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click
        '// get the button clicked.
        Dim btnClicked As Button = CType(sender, Button)
        '// change only the clicked button's backcolor.
        If btnClicked.BackColor = Drawing.Color.DarkViolet Then
            btnClicked.BackColor = Drawing.Color.Black
        ElseIf btnClicked.BackColor = Drawing.Color.Black Then
            btnClicked.BackColor = Drawing.Color.DarkViolet
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// change all the buttons backcolor.
        For Each btn As Button In Me.Controls
            btn.BackColor = Drawing.Color.DarkViolet
        Next
    End Sub
End Class

Edited by codeorder: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.