Solved

ASP.NET Random color selector algorithm

Posted on 2004-04-22
4
940 Views
Last Modified: 2008-02-01
In ASP.NET, there is a Color enumeration.

I would like to be able to programmatically randomly select a color name during a postback event, and use it to build into a string to create a "template" in a textbox.

When posted back, a panel control is made visible and inside the panel is a textbox. The textbox will come pre-filled in with :

color is [color]    where [color] is a randomly chosen color name from the Color enumeration.

I know one approach is the tedius way of making an array of all the cololr names and then using the random object to come up with a number. But what I am really look for from you experts is a more elegant and lazy approach.... THANKS..
0
Comment
Question by:gregasm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10896652
The KnownColor values are already in the enum, so you can create the array on the fly

Private Function GetRandomColor() As KnownColor

        Dim a As Array
        Dim r As Random = New Random
        Dim iElem As Integer

        a = [Enum].GetValues(GetType(KnownColor))

        iElem = r.Next(0, a.Length() - 1)
        Return a.GetValue(iElem)

    End Function
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10903557
Or for the truly lazy...

    Private Function GetRandomColor() As Color

        Dim a As Array
        Dim r As Random = New Random
        Dim iElem As Integer

        a = [Enum].GetValues(GetType(KnownColor))

        iElem = r.Next(0, a.Length() - 1)
        Return Color.FromKnownColor(a.GetValue(iElem))

    End Function
0
 
LVL 8

Author Comment

by:gregasm
ID: 10909591
Thanks for your excellent answer, dfiala13!

This is the c# equivalent::

private Color getRandomColor()
{
      Array a;
      Random r = new Random();
      int iElem;
      a = Enum.GetValues(typeof(KnownColor));
      iElem = r.Next(0, a.Length - 1);
      return Color.FromKnownColor((KnownColor)a.GetValue(iElem));
}
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10909876
Thanks.

You didn't mention what language you were interested in...
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question