Results 1 to 8 of 8

Thread: [RESOLVED] Color Change Question

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Resolved [RESOLVED] Color Change Question

    I'm working on a program that requires an object to change colors each time a user clicks it.

    The object I am trying to edit is a "Panel".
    So, what I've done so far:

    I've double clicked the panel, loading the code in the code window.
    I set the declaration to "MouseClick", and added the following code:
    Panel1.BackColor = Color.Blue

    This works without a flaw - the program loads with the panel being the default color I chose.
    Once clicked, the panel changes to blue - perfect.

    The only problem is, I cant figure out how to add additional colors to the equation.

    I've got about 15 colors I would like to use.
    Each time the user clicks the panel, I would like it to move to the next color on the list.
    When the last color is reached, I would like the list to start back over.

    So, I'm not sure how to add multiple colors, and I'm not sure how to create the loop that will restart the set once the last color is reached.

    Any help would be greatly appreciated!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,381

    Re: Color Change Question

    The way that I would do it is to store those colors in a general list(of t) at the form load then declare an integer at the form level and increment it by one every time the panel is clicked:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private color_list As New List(Of Color)
        Private int As Integer = 0
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            color_list.AddRange({Color.Blue, Color.Red, Color.Green, Color.Yellow, Color.Orange})
            Panel1.BackColor = color_list.Item(0)
        End Sub
    
        Private Sub Panel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel1.Click
            If int < color_list.Count - 1 Then
                int += 1
            Else
                int = 0
            End If
    
            Panel1.BackColor = color_list.Item(int)
        End Sub
    End Class
    Last edited by dday9; Jan 22nd, 2013 at 03:59 PM. Reason: added msdn link
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Color Change Question

    Thank you so much for your reply!

    I'll give this a shot when I get home this evening.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Color Change Question

    Slight modification to previous post. I added the color from the designer and changed declaration of the idx into the color list.

    Code:
        Private color_list As New List(Of Color)
    
        Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
            color_list.Add(Panel1.BackColor) 'add color from designer
            'add other colors
            color_list.AddRange({Color.Blue, Color.Red, Color.Green, Color.Yellow, Color.Orange})
        End Sub
    
        Private Sub Panel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel1.Click
            Static idxCL As Integer = 0 'index into the color list
            idxCL += 1
            If idxCL >= color_list.Count Then
                idxCL = 0
            End If
            Panel1.BackColor = color_list.Item(idxCL)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Color Change Question

    dbasnett -

    I greatly appreciate you taking the time to reply to my thread.

    If you have a free minute, I'd love to know a bit more about the difference between your code and dday9's.
    I can see that its a bit more light weight in terms of code - but im not really sure whats going on here...

    I follow the first part - where you are creating a list of colors.
    The loop is where it is getting a bit confusing for me.

    I aplogize - Im new to vb, so the If Loop syntax is new to me.
    (I've been trying to learn C++ as well, this looks similar to the C++ if loop, but the functions look a bit different).

    I assume the "+=1" is the same as "++" in C++?

    the "If idxCL >= color_list.Count Then idxCL = 0 End If...."
    So, this is saying that if "idxCL" is greater than or eaqual to the number of colors in the list, that it will reset the variable to 0?
    Which I assume is what sets the panel color back to its original state.

    What I dont understand, is where we told the program that 0=orignal color.
    Is this because it is the first item in our list?

    Sorry in advance for the noob questions

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Color Change Question

    Quote Originally Posted by codenewbie View Post
    dbasnett - ...I follow the first part - where you are creating a list of colors.
    The loop is where it is getting a bit confusing for me....

    I assume the "+=1" is the same as "++" in C++?

    the "If idxCL >= color_list.Count Then idxCL = 0 End If...."
    So, this is saying that if "idxCL" is greater than or eaqual to the number of colors in the list, that it will reset the variable to 0?
    Which I assume is what sets the panel color back to its original state.

    What I dont understand, is where we told the program that 0=orignal color.
    Is this because it is the first item in our list?

    Sorry in advance for the noob questions
    The first item in the list is the color you define for the panel in the designer, which may or may not be what you want. If it isn't then just remove the .Add statement. The first item in the list is at index 0.

    +=1 is the same as ++, a shame that VB didn't adopt the same syntax.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Color Change Question

    Thank you very much for your help!
    Works like a charm.

    If I knew how to rate comments - I would

  8. #8
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Color Change Question

    If you want to give rep to dbasnett (or anyone else for that matter), you can do so very easily. If you look at his post, follow his name directly down. The next thing you will see is a star and a triangle with an exclamation mark in it. Click on the star to give him extra rep. That doesn't rate his post but it rates his help and increases his standing on the forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width