Showing posts with label Color. Show all posts
Showing posts with label Color. Show all posts

Thursday, July 12, 2012

How to generate random color in C#?


In some of the case, we have a requirement to generate Random color for controls, this can be done by using Random class, Random class allows us to generate random number from given range, as we know that we can generate color using number combination, like (256,256,256).

Color is generated using combination of (256,256,256) numbers, these numbers represent RGB colors means RED, GREEN, BLUE colors. Every color is combination of RED, GREEN, BLUE colors only. So to generate random color we have to generate random combination of these numbers (256,256,256).

 Here I am explaining - How to generate random color in C#?
 
In this example, I have used Color.FromArgb() method, which accepts these parameters for color combination. This method returns generated color using given parameters, but we have to generate random color so we have to pass random color combination.


Here is code-

   private void button1_Click(object sender, EventArgs e)
        {
            //Generating object for Random class 
            Random objRand = new Random();
        
            //Generating object for Color class
             Color objColor = Color.FromArgb(objRand .Next(256),objRand .Next(256),objRand .Next(256));

            //Assigning newly generated color to Button1 

            button1.ForeColor = objColor ; 
        }

In this code, first I have defined object of Random class, which will help us to generate random number.  After that using Color class object I am generating color combination-

   Color objColor = Color.FromArgb(objRand .Next(256),objRand .Next(256),objRand .Next(256));

This line of code will return color which will be in format of color call object so we need any color class object to hold this color. Here I have declared one object by named objColor which is representing color class object. 

Finally I am assigning generated color to Button1.

   button1.ForeColor = objColor ;

Here objColor is object of color class which holds generated random color using  Color.FromArgb() function.


Thanks

Friday, June 15, 2012

How to change GridView row color on mouseover?


GridView doesn't have any built-in functionality to change color row on mouse-over, for this functionality we need to explicitly write code. This can be done easily using client side code after binding client side functionality to mouse-over and mouse-out events.


Here I am explaining - How to change GridView row color on mouse-over?
 
For this we require mouse-over and mouse-out functionality in client side. I have written following client side functions for this-
 

Client side functions-

<head runat="server">
     <script type="text/javascript">
        //Function to change color of grid row on mouseover
        function mouseIn(row) {
            row.style.backgroundColor = '#D3DFF8';
        }
        //Function to change color of grid row on mouseout
        function mouseOut(row) {
            row.style.backgroundColor = '#FFFFFF';
        }
    </script>
</head>


In above code, I have defined 2 client side functions.
 

1. mouseIn ()
2. mouseOut () 

 
mouseIn() - This function is taking one parameter, using this parameter I am passing row object,
In this function, I am setting style for row object, here I am setting row color value as- #D3DFF8 .

mouseOut() - This function is also taking one parameter, but in this function, I am setting row color value as- #FFFFFF.


Now to make it work, I have bounded these client side functions to GridView row using RowDataBound() event.
 

Here is server side code-

protected void GridEmpData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Binding client side functions to GridView row
                e.Row.Attributes.Add("onmouseover", "mouseIn(this);");
                e.Row.Attributes.Add("onmouseout", "mouseOut(this);");
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }


Now in server side code, I am binding client side functions- mounseIn and mouseOut to GridView's
Row. For this I have used RowDataBound() event of GridView. Here "this" is object which is referring to row of GridView.


Thanks