Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a usercontrol, which will have only a picturbox with an image, that has the same functionality of the buttons. So when I put this usercontrol on the form and access its properties window, this window should have the same properties of the buttons and when I click on some property, such as the Text property, the text should appear on top of this picturbox, or when I access the BackColor property the background color should be changed, the same for all other properties.
Does anyone know how I do it? Thanks in advance
Posted

Create a custom control derived from Control or Button. For goodness sake, don't even think about using PictireBox — it is designed for very different purposes and won't give you any additional benefits over rendering right in the surface of the control, will only consume memory, CPU time and your development time without any purpose.

Instead, override OnPaint; in this method, use its event arguments parameter, get an instance of System.Drawing.Graphics and use it to render appropriate image, depending on control's status (pressed, mouse hover, default, has keyboard focus…). Change status handling control's event. When the status is changed, don't forget to trigger re-rendering of the control. This is done using Control.Invalidate.

See also: How do I clear a panel from old drawing[^].

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^],
http://msdn.microsoft.com/en-us/library/598t492a.aspx[^],
http://msdn.microsoft.com/en-us/library/8dtk06x2.aspx[^],
http://msdn.microsoft.com/en-us/library/wtzka3b5.aspx[^].

As to the button samples, CodeProject is a good place to search in.

Watch this:
http://www.codeproject.com/search.aspx?q=Custom+button+%22VB.NET%22&doctypeid=1[^],
http://www.codeproject.com/search.aspx?q=Custom+button+%22C%23%22&doctypeid=1[^].

Now, pay attention: 7 articles in VB.NET, 47 in C#. Yes, to find good samples and any general help in .NET you really need to understand at least some of C#.

—SA
 
Share this answer
 
v3
Comments
Edson Rodrigues da Silva 6-Aug-11 17:56pm    
Sorry but I am new to programming. I do not know how I do OnPaint methods for each of the properties that the buttons have. Do you know any link or any article that I can inform me about this?
Sergey Alexandrovich Kryukov 6-Aug-11 18:19pm    
What do you think I did not tell you about? Just do it. This is pretty much elementary staff, so I don't know any specific articles -- my solution is the article (updated); your links are MSDN. Please see, everything is explained. Ask you you have some particular problems.
--SA
Edson Rodrigues da Silva 6-Aug-11 20:21pm    
Thanks for helping me, I will analyze all of these links.
Don't create a UserControl. Instead, create a class that inherits from the Button control. Enable Owner drawn mode and handle the drawing yourself.
 
Share this answer
 

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