Click here to Skip to main content
15,898,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a color type array such that:



colorSet[0] = Color.DodgerBlue;
colorSet[1] = Color.YellowGreen;
colorSet[2] = Color.Crimson;
colorSet[3] = Color.HotPink;
colorSet[4] = Color.MediumPurple;
colorSet[5] = Color.MediumSeaGreen;
colorSet[6] = Color.MediumVioletRed;
colorSet[7] = Color.PaleVioletRed;
colorSet[8] = Color.Plum;
colorSet[9] = Color.SlateBlue;



i want to use Color[5] in place of Red in following statement:




DrawEllipse(Pens.Red, 50 * i, 50 * j, 8, 8);

what to do?????????

What I have tried:

i have tried :


DrawEllipse(Pens.colorSet[5], 50 * i, 50 * j, 8, 8);
Posted
Updated 14-May-16 21:13pm
Comments
Karthik_Mahalingam 15-May-16 2:58am    
what is the issue ?
Richard MacCutchan 15-May-16 3:02am    
And what happened?
Member 12404741 15-May-16 3:17am    
thanks for consideration problem has been solved

1 solution

That's more complex than you think: a Color and a Pen aren't the same thing, and a Pen is a scarce resource, so when you use your own, you are responsible for Disposing it correctly when you are finished.
C#
private void myPanel_Paint(object sender, PaintEventArgs e)
    {
    using (Pen p = new Pen(colorSet[5]))
        {
        e.Graphics.DrawEllipse(p, 50, 50, 8, 8);
        }
    }
 
Share this answer
 
Comments
Member 12404741 15-May-16 3:17am    
yeah it worked thanks
OriginalGriff 15-May-16 3:35am    
You're welcome!
If you've only get 10 of them, then it might be worth creating an array of Pen items instead, and Disposing them all when your app ends.
(But if you don't dispose them, your app can leak resources and that's not good at all!)
Member 12404741 15-May-16 6:03am    
kindly let me know how to dispose. actually i am new to c# so have less know how
OriginalGriff 15-May-16 6:24am    
Call the Dispose method of each object instance in turn:
for (int i = 0; i < colorPenSet.Length; i++)
{
colorPenSet[i].Dispose();
colorPenSet[i] = null;
}

Or the using block I show in the answer will do it, but it Disposes it automatically when it goes out of scope - which is no good if you are trying to keep an array of ten Pens live for the duration of your app as once an object is Disposed, you can't use it again.
Member 12404741 15-May-16 7:18am    
thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900