Problem With Alignment of Image on TabControl
I'm using a c# winform TabControl
. I've created an ImageList
, associated it with the TabControl
and populated it with a few images. The problem is, when I set the ImageIndex
or ImageKey
for a particular tab the image shows up on the tab, but it covers the text that is on the tab. To add to my confusion, one TabPage
in particular renders the image in the correct location (image followed by text). The page that has the image in the correct location is whatever TabPage
I set the ImageIndex
property on first...
I've googled around and found a post on another forum where someone described this problem and everyone said they cou开发者_StackOverflow中文版ldn't recreate the problem. Any ideas what the problem could be? I really don't know what to try.
Edit
Sorry for the confusion. I have a TabControl
object with it's ImageList
property associated with an ImageList
that has two images. When I set the ImageIndex
property on a TabPage
in the TabControl
(from what I read) it is supposed to render the actual tab on the TabControl
as image followed by text. This is what happens when I set the image for the first TabPage
in my TabControl
but every tab following that will be rendered with the image covering the text that is on the tab. So the text appears behind the image. I use a for loop to set the image for each Tab
:
for (int i = 0; i < tabControl.TabPages.Count; i++) {
tabControl.TabPages[i].ImageIndex = SOME_IMG_INDEX;
}
The TabPage
at index 0 of the TabControl.TabPages
collection will look normal (image followed by text). The others will have the image over top of the text. Even if I were to start the for loop iteration at index 2, index 2 would render correctly but any after that would have the image over the text. I hope this helped make the question clearer.
The only way I've found to recreate your issue was when I changed the SizeMode
property on the TabControl
.
When I changed it to Fixed
, the images and text were commingled. Changing it back to `Normal' rendered everything correctly again.
This answer talks about removing the tabs and adding them back in as a hack that works around this issue, posted at MSDN. I tried the hack and it worked.
Update:
As Steve_Overflow pointed out, this is a simple work-around to update the tabs correctly:
tabControl1.SizeMode = TabSizeMode.Fixed;
for (int i = 0; i < tabControl1.TabPages.Count; i++)
tabControl1.TabPages[i].ImageIndex = SOME_IMG_INDEX;
tabControl1.ItemSize = tabControl1.ItemSize;
If you want Fixed-Size Tabs , use theses codes together (Don't know why, but this will solve the problem)
tabControl.SizeMode = TabSizeMode.Normal;
tabControl.SizeMode = TabSizeMode.Fixed;
精彩评论