Solved

ASP.NET Random color selector algorithm

Posted on 2004-04-22
4
899 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now