Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
sir!
can we add icons to panel ! which template should be use to create panel control.
please I've not idea of that....
Thanks in advance...
Posted
Comments
Sergey Alexandrovich Kryukov 16-May-12 11:38am    
Why icons? What are you going to do with them?
--SA
Clifford Nelson 16-May-12 12:10pm    
What is your environment: WinForm, ASP.NET, WPF?

1 solution

Icon is not a control, so you cannot add it to the panel the same way you would add a control. You can only draw an icon in any control's client area. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.icon.aspx[^].

Perhaps the closest to your requirements solution would be this: create some custom control which renders some icon in it. Insert instanced of such control in your group box. To draw something, including an icon, on a control, you need to handle its event Paint, or, better yet, override its virtual method OnPaint. In either way, do not create an instance of System.Drawing.Graphics (which is a very common mistake), but use the instance passed to you as an event arguments parameter of your event handler of overridden method. Use the method System.Drawing.Graphics.DrawIcon:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawicon.aspx[^].

However, I don't see cases where drawing icons in the client area would be useful. Icons carry specific functionality (multiple images with different size and color spaces) which are required to makes them application icons and windows icons, to be properly treated by the Windows UI, such as Shell. In all other cases you really need to use Bitmap or other Image and System.Drawing.Graphics.DrawImage:
http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage.aspx[^].

As this case with using image is very simple, you can use the ready-to-use control specially designed for showing images in simple cases. This is PictureBox:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx[^].

Please remember that this simple class is often abused. It is only good if you need something really simple (such as in your case). I explained it in my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

—SA
 
Share this answer
 
Comments
VJ Reddy 16-May-12 11:51am    
Very nice explanation. 5!
Sergey Alexandrovich Kryukov 16-May-12 12:06pm    
Thank you, VJ.
--SA

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