Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello!

I'm using a tabcontrol in a Compact Framework application and I want to add images in the different tabpages.

As Compact Framework does not allow to include the images via design properties, I want to know how I can add different images.

Using "Override OnPaint" I can change the background image, but I can´t change the tabpages background images.

Thanks in advance for the help.

C#
private Bitmap backgroundImage = null;	

void TabPage1Paint(object sender, PaintEventArgs e)
{
    backgroundImage = new Bitmap(folderpath+ "\\backgroundImage.png");
    e.Graphics.DrawImage(backgroundImage, this.ClientRectangle, new Rectangle(0, 0, this.Width, this.Height), GraphicsUnit.Pixel);   
}   
Posted
Updated 1-Jun-15 1:34am
v3
Comments
Sergey Alexandrovich Kryukov 28-May-15 11:01am    
Let's start from the very beginning, say "TabPage" type. Which one? Full type name, please. Say, "System.Windows.Forms.TabPage"? or anything else? Always specify it.
Why do you think you cannot use OnPaint? You certainly can.
—SA
Member 11423970 28-May-15 13:39pm    
Thanks for replyng. I'm quite new with programming so forgive my lack of vocabulary.

I have created two typepages in the main form using the designer ("System.Windows.Forms.TabPage1" and "System.Windows.Forms.TabPage2") and I want to change de background image of both of then. I searched throw the net how to add a background image to the main form, and I guess that to do the same with the tabpages I have to use the Onpaint method, but I don't know how and I find nothing in the net.

1 solution

Be careful with background images; the can do content poorly readable. Better prefer uniform color, or only slightly textured near-uniform backgrounds.

Normally, it's https://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.backgroundimage%28v=vs.110%29.aspx[^].

It does not say it's supported in Compact Framework, so you can override OnPaint or handle Paint event. In both cases, you use the instance of System.Drawing.Graphics passed in event arguments and use it to draw an image: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

[EDIT]

To use the tab page of your own type (which would be required if you use OnPaint, because you can only override method in your derived class), use this method of adding a page:
https://msdn.microsoft.com/en-us/library/68d9sedf%28v=vs.110%29.aspx[^].

If you handle Paint, you can add the handler each time you add a new page.

—SA
 
Share this answer
 
v2
Comments
Member 11423970 28-May-15 15:29pm    
I know how to use System.Drawing.Graphics to draw an image in the main form, but I don't know how to do it in a tabpage. I don't know the the code line to refer to that specific object. Can you put me an example or a link to a page that talks about that?
Sergey Alexandrovich Kryukov 28-May-15 15:37pm    
Please, what's the problem?
—SA
Sergey Alexandrovich Kryukov 28-May-15 15:41pm    
Let me guess...
Is your problem making TabPage custom? I added some explanation to the answer, after [EDIT]. Any other problems?
—SA
Member 11423970 29-May-15 3:55am    
Hello again.
I added my code. I have created tabpages using the information you provided me, but I an unable to add an image to the tabpage using Onpaint method or any other method.
Sergey Alexandrovich Kryukov 29-May-15 10:16am    
Why two different rectangles? Why two images? One on top of another? Checkup your drawing on something like a panel first; the drawing is strange. Your drawing assumes that the image is of the size of ClientRectangle or more.
Don't create images inside even; create them before the control is shown, create them in constructor.
—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