Welcome to Dream.In.Code
Become a VB Expert!

Join 137,233 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,789 people online right now. Registration is fast and FREE... Join Now!




BackColor Property

 
Reply to this topicStart new topic

BackColor Property, Changing the BackColor property of a Form

Qman69
14 Mar, 2007 - 02:40 PM
Post #1

New D.I.C Head
*

Joined: 14 Mar, 2007
Posts: 3


My Contributions
Hi,

I'm new to VB programming but used to program in Basic many years ago. My first attempt in VB 2005 is to create a form with Radio buttons on it which change the color of the Form when pressed. One of the Buttons is supposed to respond with a random color (well, one of 21 predefined ones!). I can't work out how to change the BackColor property with a single statement. All I can manage to do is have a Select Case structure with the 21 options predefined and choose which one to execute based on a random number like this --
CODE

Select Case objRandom.Next(0, 20)
            Case 0
                frmMainForm.BackColor = Color.Aquamarine
                strColourName = " Aquamarine"
            Case 1
                frmMainForm.BackColor = Color.Beige
                strColourName = " Beige"

     and so on down to .............

            Case 20
                frmMainForm.BackColor = Color.Transparent
                strColourName = " Transparent"
        End Select

Is there a way of replacing the "Color.whatever" with a strVariable? Then I could replace the whole thing with one or two statements.

Thanks for any help.

This post has been edited by Qman69: 14 Mar, 2007 - 02:42 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: BackColor Property
14 Mar, 2007 - 03:42 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,984



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Absolutely, but not with a String variable. Make an array of Color objects with all the colors that you want available.

Then randomly generate a number between 0 (the start of your array) and the total length-1 of your array.
CODE

        Dim rdnNum As New Random
        Dim colorArray() As Color = {Color.AliceBlue, Color.Brown, Color.Black, Color.Crimson, Color.DarkGoldenrod}

        frmMainForm.BackColor = colorArray(rdnNum.Next(0, 4))

User is online!Profile CardPM
+Quote Post

Qman69
RE: BackColor Property
16 Mar, 2007 - 12:30 AM
Post #3

New D.I.C Head
*

Joined: 14 Mar, 2007
Posts: 3


My Contributions
Hi,

Thanks for the help. It's now working a treat.

Qman69
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:39PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month