hi guys,

was tinkering around with the tab control and came up with a problem. for your information, i have relied heavily on the example shown here:
http://www.codeproject.com/KB/tabs/firefoxtabcontrol.aspx
basically what i'm doing is docking forms onto different tab pages.

my problem is that after i override OnDrawItem, the xp stlye is replaced with a pretty ugly control style.

i encountered my first problem when i wanted to add icons to the tabpages, as seen in the example provided in the link, OnDrawItem wipes the slate clean, so even if i assign an image key to the particular tab, it would not display the image (note that when i open different forms, the icons displayed should be different). my solution was moving the OnDrawItem event from the control itself onto the main form thus allowing me to manually draw the icons based on the forms selected.

my questions are:
1. is there a way to pass variables into the control's OnDrawItem(i don't think moving the code into the main form is a good thing to do and since its an event, i don't know how i can pass in variables)? OR is there a way to still enable the image key to display the icon even after using OnDrawItem?

2. obviously i would want the xp style to remain on the tabs (plus the close button and corresponding icon), but i really don't want to redraw and repaint everything from scratch. if i want the style, i'd have to set the Draw mode to OwnerDrawFixed but if i do that, i won't be able to display the close boxes when i add new tabs since the main point of using OnDrawItem is to display the close boxes WHEN the tabpages are drawn, removing it would mean an empty tab with no icon.

really hope you guys can help me. i'm pulling off my hair now.

regards.

Recommended Answers

All 5 Replies

Sure, define your own class descending from the original, and then you can

a) allow the original to draw as was and draw over the top
b) add properties and data of your own

yes, i've defined my own class, the problem is if i've set the DrawMode of the tabcontrol to OwnerDrawFixed to facilitate the use of OnDrawItem. the reason i'm doing this is that i have three icons, (each represending different states, over, up, leave) that i want to draw onto the tabs when they are draw, therefore using the OnDrawItem.

i have to use OnDrawItem because i want the icons to be displayed on the right hand side when a new tab is added. the other events only paint the boxes on the tabs when they happen (mouse over, mouse leave, etc). i would really like not to use OwnerDrawFixed but i cant see how setting the drawmode to normal would enable me to draw something on the tabcontrol when it adds a new tab.

anyway i tried overriding OnControlAdded event as it also fires when a new tab is being added. problem is the icons are not painted on the tabpage eventhough the event is successfully fired.

i may be missing something here. please help.

anyway this is the segment of codes i used for the OnControlAdded event:

protected override void OnControlAdded(ControlEventArgs e)
        {
            Graphics g = CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
            RectangleF tabTextArea = RectangleF.Empty;
            for (int nIndex = 0; nIndex < this.TabCount; nIndex++)
            {
                if (nIndex != this.SelectedIndex)
                {
                    a = Assembly.GetExecutingAssembly();
                    stream = a.GetManifestResourceStream("testing.images.inactive.bmp");

                    tabTextArea = (RectangleF)this.GetTabRect(nIndex);

                    using (Bitmap bmp = Bitmap.FromStream(stream) as Bitmap)
                    {
                        g.DrawImage(bmp,
                            tabTextArea.X + tabTextArea.Width - 16, 5, 13, 13);
                    }

                }
                else
                {
                    a = Assembly.GetExecutingAssembly();
                    stream = a.GetManifestResourceStream("testing.images.up.bmp");

                    tabTextArea = (RectangleF)this.GetTabRect(nIndex);

                    using (Bitmap bmp = Bitmap.FromStream(stream) as Bitmap)
                    {
                        g.DrawImage(bmp, tabTextArea.X + tabTextArea.Width - 16, 5, 13, 13);
                    }
                }
            }
            g.Dispose();
            base.OnControlAdded(e);
        }

anyone else out there who can help me? i think the only way to do this is to use the OnControlAdded event. it does fire! and i've seen the box drawn (flicker) on the tabpage then vanish as the form was docked into the tab. any other methods to 'make' the box stay and not be overwritten?

i really cant use ondrawitem or ownerdrawfixed setting because i need to add icons into the tabpages. i noticed icons assigned using ImageKey to the tabs would not display if i use ondrawitem and set the draw mode to ownerdrawfixed.

please help!!!

well anyway i got to solving this problem by using OnControlAdded event and adding the icons to a dictionary then drawing them during the OnDrawItem event. guess i gotta use OwnerDrawFixed then. anyway thanks guys for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.