Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,424 members, 7,808,519 topics. Date: Thursday, 25 April 2024 at 12:59 PM

Help changing the background color of a form in Vb.net - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help changing the background color of a form in Vb.net (2808 Views)

How Do I Change The Color Of A Grid In Bootstrap / How To Deploy Application With Access Database In Vb.net 2008 / How Do I Connect A Form To Mysql Database (2) (3) (4)

(1) (Reply)

Help changing the background color of a form in Vb.net by Sylvain1: 11:57pm On Apr 04, 2012
I have a problem with a Pong application i want to make in VS2010. The problem is I want to change the colors of the background of the form, the rectangles and the ellipse everytime a timer ticks. My code seems logic, however, when I try to run, my form turns immediately black. The code is shown below.


'needed for dispatchertimer

Imports System.Windows.Threading

Class MainWindow

'private declarations

Private WithEvents tmrTimer As New DispatcherTimer
Private clrColorBackground As New Color
Private clrColorRectangle As New Color
Private clrColorBall As New Color
Private rndRandom As New Random

Private iBr, iBg, iBb, iRr, iRg, iRb, iOr, iOg, iOb As Byte


Private Sub tmrTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
'subroutines are called within a VS2010 generated method, it's my style of programming
toggleColor()
setColor()
End Sub


Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

'subroutines are called within a VS2010 generated method

defaultColors()

initTimer()

End Sub

Private Sub initTimer()
tmrTimer.Interval = TimeSpan.FromMilliseconds(1000)

tmrTimer.Start()
End Sub

Private Sub defaultColors()
'set default background color

clrColorBackground.R = rndRandom.Next(35, 221)
clrColorBackground.G = rndRandom.Next(35, 221)
clrColorBackground.B = rndRandom.Next(35, 221)

'set default rectangle color

clrColorRectangle.R = rndRandom.Next(35, 221)
clrColorRectangle.G = rndRandom.Next(35, 221)
clrColorRectangle.B = rndRandom.Next(35, 221)

'set default ball color

clrColorBall.R = rndRandom.Next(35, 221)
clrColorBall.G = rndRandom.Next(35, 221)
clrColorBall.B = rndRandom.Next(35, 221)
End Sub

Private Sub toggleColor()

'set red property background
'the following code is repeated for every rgb-part of each control(background, rectangles, ball)

If clrColorBackground.R > 35 And iBr Mod 2 = 0 Then
clrColorBackground.R -= 1
End If

If clrColorBackground.R = 35 Then
iBr += 1
End If

If clrColorBackground.R < 220 And iBr Mod 2 = 1 Then
clrColorBackground.R += 1
End If

If clrColorBackground.R = 220 Then
iBr += 1
End If

'set green property background

If clrColorBackground.G > 35 And iBg Mod 2 = 0 Then
clrColorBackground.G -= 1
End If

If clrColorBackground.G = 35 Then
iBg += 1
End If

If clrColorBackground.G < 220 And iBg Mod 2 = 1 Then
clrColorBackground.G += 1
End If

If clrColorBackground.G = 220 Then
iBg += 1
End If

'set blue property background

If clrColorBackground.B > 35 And iBb Mod 2 = 0 Then
clrColorBackground.B -= 1
End If

If clrColorBackground.B = 35 Then
iBb += 1
End If

If clrColorBackground.B < 220 And iBb Mod 2 = 1 Then
clrColorBackground.B += 1
End If

If clrColorBackground.B = 220 Then
iBb += 1
End If

'set red property rectangle

If clrColorRectangle.R > 35 And iRr Mod 2 = 0 Then
clrColorRectangle.R -= 1
End If

If clrColorRectangle.R = 35 Then
iRr += 1
End If

If clrColorRectangle.R < 220 And iRr Mod 2 = 1 Then
clrColorRectangle.R += 1
End If

If clrColorRectangle.R = 220 Then
iRr += 1
End If

'set green property rectangle

If clrColorRectangle.G > 35 And iRg Mod 2 = 0 Then
clrColorRectangle.G -= 1
End If

If clrColorRectangle.G = 35 Then
iRg += 1
End If

If clrColorRectangle.G < 220 And iRg Mod 2 = 1 Then
clrColorRectangle.G += 1
End If

If clrColorRectangle.G = 220 Then
iRg += 1
End If

'set blue property rectangle

If clrColorRectangle.B > 35 And iRb Mod 2 = 0 Then
clrColorRectangle.B -= 1
End If

If clrColorRectangle.B = 35 Then
iRb += 1
End If

If clrColorRectangle.B < 220 And iRb Mod 2 = 1 Then
clrColorRectangle.B += 1
End If

If clrColorRectangle.B = 220 Then
iRb += 1
End If

'set red property ball


If clrColorBall.R > 35 And iOr Mod 2 = 0 Then
clrColorBall.R -= 1
End If

If clrColorBall.R = 35 Then
iOr += 1
End If

If clrColorBall.R < 220 And iOr Mod 2 = 1 Then
clrColorBall.R += 1
End If

If clrColorBall.R = 220 Then
iOr += 1
End If

'set green property ball

If clrColorBall.G > 35 And iOg Mod 2 = 0 Then
clrColorBall.G -= 1
End If

If clrColorBall.G = 35 Then
iOg += 1
End If

If clrColorBall.G < 220 And iOg Mod 2 = 1 Then
clrColorBall.G += 1
End If

If clrColorBall.G = 220 Then
iOg += 1
End If

'set blue property ball

If clrColorBall.B > 35 And iOb Mod 2 = 0 Then
clrColorBall.B -= 1
End If

If clrColorBall.B = 35 Then
iOb += 1
End If

If clrColorBall.B < 220 And iOb Mod 2 = 1 Then
clrColorBall.B += 1
End If

If clrColorBall.B = 220 Then
iOb += 1
End If

End Sub

Private Sub setColor()
Dim br As New SolidColorBrush

'fill background

br.Color = clrColorBackground
Me.Background = br

'fill ball

br.Color = clrColorBall
ellBall.Fill = br

'fill rectangle

br.Color = clrColorRectangle
rctP1.Fill = br
rctP2.Fill = br

End Sub
End Class
Re: Help changing the background color of a form in Vb.net by BizBooks(m): 1:08pm On Apr 09, 2012
All your "If Statements" immediately end with an "End If". That may
cause only one statement to be executed. Try including all the arguments
in one "If...Else If" statement eg

If clrColorBackground.R > 35 And iBr Mod 2 = 0 Then
clrColorBackground.R -= 1
Else If
clrColorBackground.R = 35 Then
iBr += 1
Else If
clrColorBackground.R < 220 And iBr Mod 2 = 1 Then
clrColorBackground.R += 1
Else If
clrColorBackground.R = 220 Then
iBr += 1
Else DO SOMETHING ELSE
End If

Remember to close the statement with the "Else" syntax to take care of
conditions not stipulated in the code.

(1) (Reply)

Rivers State University Annual Jean Carnival / I Still Can't Believe This Site Is Powered By Wordpress... Best So Far.. / Other Than Linkedin, Please How Do You Get Tech Jobs

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.