Change Tab control image
- Hey,
I am trying to figure out how to add a background iamge/image to the tab control in Visual Basic 2005 express, I added the control into my form but it is the windows default color, so set the background image of it so it fits what I made my program look like using iamges, but the buttons are the same color, windows default, how do I add a image to the tabs so they are not the windows default color, and fit in with my program's coloring?
Thanks :)
Answers
I think it's in VS2003, but you'll get some ideas.
The op_Implicit keyword works fine in VS2003, but not having a copy, I can't say that it works in C# of VS2005, although I would expect it to be the same.
Since all the op_implicit keyword does is to ensure that the Rectangle is cast as a RectangleF you should change that line to:
e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, (RectangleF)ItemRect, sf);
- Did you read the following line before the code sample?
The first thing to do is to set the TabControls DrawMode to OwnerDrawFixed as shown above.
You will of course also need to assign the DrawItem event to the tabControl1_DrawItem() method. Click the lightning bolt in the properties window and after selecting the tabcontrol, click on the dropdown arrow of the DrawItem event so that you can make the association.
This will only color the tabs. The problem you have is that you want a specific appearance for tabs, and that is a lot of work.
Setting the TabControl to OwnerDraw will only allow you to custom Draw the Tab Content. The outline of the tabs will still be drawn by the control.
From what I understand, you wish to use bitmaps to draw the tabs. To achieve this you will need to use the second example on my site as a base, but there will still be a fair amount of work to do afterwards.
Other than the very basic example on my site, there are 2 others which I would reccomend that you examine:
All Replies
Hi,
MSDN says the BackGroundImage property for a TabControl is not meaningful.
You can instead add an imagelist to your TabControl.
To display an icon on the label part of a tab:
1.Add an ImageList control to the form.
2. Add images to the image list.
3. Set the ImageList property of the TabControl to the ImageList control.
4. Set the ImageIndex property of the TabPage to the index of an appropriate image in the list.
Hope that helps,
Regards,
Mamta
- Well, that only added a image into the tab control, but it did not make the whole tab that iamge...
Depending upon exactly what it is that you're after, this could be a lot of work.
Have a look at the examples on my site to see if any of them help:
http://www.dotnetrix.co.uk/tabcontrols.html- No, not really...
but there a cool,
I tryed using one but I got a few errors and I dont want to get back into that again here for a while.
What I am trying to do is take these 2 images:
tabbg.gif
tabbg2.gif
tabbg.gif is the image that will be for the selected tab, and tabbg2.gif is for the non selected tab.
I am trying to make those 2 images and make them the tab...
So instead of making the tab be the defualt, it is those images... - I think you want an OwnerDrawn Tab Control.
- What is that?
it is not in the containers list on the toolbox and it is not in the list of addons for the toolbox... - I mean, you will have to set the Tab Control as OwnerDrawn and draw the tabs manually.
- Oh, ok, so I got that part...but now it dose not even have text...
And is there supposed to be a option to make the images into the tab? I think it's in VS2003, but you'll get some ideas.
Sorry it took me a long time to reply,
ok, so I looked at the code, but, it did not help to much, I still can't get it to change the image of the tab, I fiddled around and got really close to getting it to do what I wanted, I did this:
public override Image BackgroundImage{
get{
return base.BackgroundImage;}
set{
base.BackgroundImage = SurfScape.Properties.Resources.Tab;}
}
but that only changed the background image of the part behind the tab, not the actual tab...
So is there a way like this that I could change the tab images?
Thanks :)
EDIT: ok I tryed one more time using Mick Doherty's code for OwnerDrawn tab control, but I got 1 error: 'System.Drawing.RectangleF.implicit operator System.Drawing.RectangleF(System.Drawing.Rectangle)': cannot explicitly call operator or accessor
It is in this code:private
void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e){
TabPage CurrentTab = tabControl1.TabPages[e.Index]; Rectangle ItemRect = tabControl1.GetTabRect(e.Index); SolidBrush FillBrush = new SolidBrush(Color.Red); SolidBrush TextBrush = new SolidBrush(Color.White); StringFormat sf = new StringFormat();sf.Alignment =
StringAlignment.Center;sf.LineAlignment =
StringAlignment.Center; //If we are currently painting the Selected TabItem we'll //change the brush colors and inflate the rectangle. if (System.Convert.ToBoolean(e.State & DrawItemState.Selected)){
FillBrush.Color =
Color.White;TextBrush.Color =
Color.Red;ItemRect.Inflate(2, 2);
}
//Set up rotation for left and right aligned tabs if (tabControl1.Alignment == TabAlignment.Left || tabControl1.Alignment == TabAlignment.Right){
float RotateAngle = 90; if (tabControl1.Alignment == TabAlignment.Left)RotateAngle = 270;
PointF cp = new PointF(ItemRect.Left + (ItemRect.Width / 2), ItemRect.Top + (ItemRect.Height / 2));e.Graphics.TranslateTransform(cp.X, cp.Y);
e.Graphics.RotateTransform(RotateAngle);
ItemRect =
new Rectangle(-(ItemRect.Height / 2), -(ItemRect.Width / 2), ItemRect.Height, ItemRect.Width);}
//Next we'll paint the TabItem with our Fill Brushe.Graphics.FillRectangle(FillBrush, ItemRect);
//Now draw the text.e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush,
RectangleF.op_Implicit(ItemRect), sf); //Reset any Graphics rotatione.Graphics.ResetTransform();
//Finally, we should Dispose of our brushes.FillBrush.Dispose();
TextBrush.Dispose();
}
here is the exact spot:
//Now draw the text.
e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush,
RectangleF.op_Implicit(ItemRect), sf);Thanks :)
The op_Implicit keyword works fine in VS2003, but not having a copy, I can't say that it works in C# of VS2005, although I would expect it to be the same.
Since all the op_implicit keyword does is to ensure that the Rectangle is cast as a RectangleF you should change that line to:
e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, (RectangleF)ItemRect, sf);
Ok, there are no errors now, thanks :)
But nothing new happens...
- Did you read the following line before the code sample?
The first thing to do is to set the TabControls DrawMode to OwnerDrawFixed as shown above.
You will of course also need to assign the DrawItem event to the tabControl1_DrawItem() method. Click the lightning bolt in the properties window and after selecting the tabcontrol, click on the dropdown arrow of the DrawItem event so that you can make the association.
This will only color the tabs. The problem you have is that you want a specific appearance for tabs, and that is a lot of work.
Setting the TabControl to OwnerDraw will only allow you to custom Draw the Tab Content. The outline of the tabs will still be drawn by the control.
From what I understand, you wish to use bitmaps to draw the tabs. To achieve this you will need to use the second example on my site as a base, but there will still be a fair amount of work to do afterwards.
Other than the very basic example on my site, there are 2 others which I would reccomend that you examine:
- Ok thanks :)


