Results 1 to 10 of 10

Thread: screwing around with backcolor [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    screwing around with backcolor [RESOLVED]

    I was just fooling around and wanted to make a button change the backcolor of a form and have a label show the color name. I figure the best way to do this is to have an index # of all colors in the system.drawing.color property and make a loop to change that everytime the click event is fired. then convert the index # to a string and change the text of the label to that color name. what is the best approach here?
    Last edited by Andy; Feb 11th, 2004 at 09:56 PM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    made this for you , hope it helps
    VB Code:
    1. Dim x As Integer = 0
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim t As Type = GetType(Color)
    5.         Dim strColors As String() = t.Attributes.GetNames(GetType(System.Drawing.KnownColor))
    6.         ListBox1.Items.AddRange(strColors)
    7.     End Sub
    8.  
    9.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    10.         If x < ListBox1.Items.Count Then
    11.             Me.BackColor = Color.FromName(ListBox1.Items(x))
    12.             x += 1
    13.         End If
    14.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Talking

    hey sys, that's great!! Thanks. not exactly what I was after but definetly cool!

    One problem, and I'm sure this is because of an RGB thing or something, but some of the colors listed in the box either show as white or dark, dark blue and not as the color listed (ie Tomato shows white) could that be that my monitor doesn't display that color? Or could it be the fact (and I'll experiment) that I'm using my Studio over a remote desktop connection?

    AND could you explain how you came up with that code? I'd like to know your step-by-step process of coming up with answers. I don't mind so much getting answers to problems but I'm one who likes to be taught HOW to GET the answers and I'm sure teachers appreciate that.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Unhappy

    ah, I see what it's doing now. no matter WHAT color is selected, button2 simply goes through a series of colors. it doesn't show the color selected at all (unless you cycle through and up to it). When the cycle gets to a transparent color, the app crashes.

    What I'd really like to do is have a label display the color of the background that button1 cycles through (and skips transparent). but here's the catch, I was only wanting the colors supported by system.drawing.color namespace.

    any ideas on THIS one?


  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This ?
    VB Code:
    1. Me.Label1.BackColor = Color.FromName(ListBox1.Text)

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    dynamic_sysop , do you know how to iterate through KnownColor enum using foreach loop . I did it a month ago but forgot how exactly .

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    sorry , been busy all day with xmas aproaching fast
    VB Code:
    1. Dim strColor As String
    2.         For Each strColor In GetType(Drawing.KnownColor).Attributes().GetNames(GetType(Drawing.KnownColor))
    3.             Console.WriteLine(strColor)
    4.         Next
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Merry Christmas.

    Actually , it's not that what I mean . It's more shorter than this and doesn't use attributes I believe . It's ok

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    have you tried type.getfields? along with reflection.Fieldifo. eg:
    VB Code:
    1. Dim strColors As Reflection.FieldInfo() = GetType(KnownColor).GetFields
    2.         Dim strCol As Reflection.FieldInfo
    3.         For Each strCol In strColors
    4.             Console.WriteLine(strCol.Name)
    5.         Next
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's cool . This is what I'm talking about .......
    Code:
    KnownColor kn;
    				for ( kn=KnownColor.AliceBlue ;kn<=KnownColor.YellowGreen;kn++)
    				{
    					this.listBox1.Items.Add(kn.ToString());
    				}

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