Click here to Skip to main content
13,150,867 members (40,672 online)
Click here to Skip to main content
Add your own
alternative version

Stats

118.3K views
4.9K downloads
90 bookmarked
Posted 8 Dec 2006

A Tab Control Similar to that of Internet Explorer 7

, 8 Dec 2006
Rate this:
Please Sign up or sign in to vote.
A (fairly) simple tab control with closeable tabs
Sample Image - TabPages.jpg

Introduction

Recently, several of my projects have required the ability to handle multiple documents. I figured IE7's tabs were a pretty good way of handling this, and here is the control that resulted.

Overview

This control is basically a managed collection of pages (or controls). It is event driven, indicating when a page has been added, removed, selected, or closed. The close event is cancellable, allowing you to prevent pages from being closed unless certain criteria are met.

I should probably add that there is no support for adding tabs via the IDE. I'd considered doing this, but it didn't really make sense. All of my projects are dealing with documents which will be opened and closed at runtime, and so I designed this control with that in mind.

Using the Control

Creating the Control

To create the control, add a reference to TabPages.dll, then create the TabPages.PageCollection control either programmatically or through the IDE.

Adding Pages

To add a page, call the Add method. This method takes a TabPage object as a parameter.  The TabPage object has text, a control (the control you wish to display when the tab is selected), and an optional toolTip string which will be displayed when the mouse hovers over the tab.

Removing Pages

To remove a page, call the Remove, RemoveAt, or Clear methods. These methods correspond to IList(Of TabPage) methods.

You can also call the Close method of the TabPage you wish to close (this will raise the PageClosing event, whereas the Remove methods will not).

Events

There are several events which will be of interest to users of the PageCollection control, and they are defined as follows:

Public Event PageAdded(ByVal page As TabPage)

Public Event PageRemoved(ByVal page As TabPage)

Public Event CurrentPageChanged_
	(ByVal currentPage As TabPage, ByVal previousPage As TabPage)

Public Event PageClosing(ByVal page As TabPage, ByRef cancel As Boolean)

The PageAdded event fires when a page is added to the collection.

The PageRemoved event fires when a page is removed from the collection.

The CurrentPageChanged event fires when the currently selected page changes (when a different tab becomes the active tab). This event gets passed the currently selected tab as well as the previously selected tab.

The PageClosing event fires when a page is attempting to close. This gets fired when the user clicks close, or when the TabPage's Close method is called. This does not fire when Remove, RemoveAt, or Clear are called. The page parameter indicates which page is attempting to close, and the cancel parameter allows cancellation of the close. If the close is not cancelled, the page will be removed from the collection and the PageRemoved event will fire.

Appearance

The appearance of this control could be more flexible, but for now, it is adequate. There are two properties of interest when customizing the PageCollection control's appearance.

The TabColor property specifies the color which will be used to generate the control's theme. All gradients, borders, and menu colors are generated from this color. Play around with it and see what you like. My favorite TabColors are LightGray and LightSteelBlue.

The TopMargin property specifies the difference between the height of the selected tab and the non selected-tabs.

Points Of Interest

As I said earlier, this control is pretty simple.  But there were two semi-challenges while developing it.

Flickering

The initial version flickered something awful, even though I was double buffering. I was able to get around this by passing the WM_SETREDRAW message to the SendMessage API method.  It's now smooth as... er... Bono or some similarly smooth person.

Private Const WM_SETREDRAW As Integer = &HB

Private Declare Auto Function SendMessage Lib "User32" Alias "SendMessage" _
	(ByVal hWnd As IntPtr, ByVal msg As Integer, _
	ByVal wParam As Integer, ByVal lParam As Integer) As Boolean

What was happening is each time I resized (or added/removed controls), I would regenerate the list of displayed tab controls. Each time the list was regenerated, each tab would invalidate. It was ugly. WM_SETREDRAW basically tells a handle (control) that you don't want it to paint any more until you say so. For more information on this, see the FlickerFreeControl in the project.

Menu Theme

The other thing I wanted was menus which matched the color scheme of the tabs. To accomplish this, I inherited from the ToolStripRenderer.  I'd never used this before, but what it allows you to do is customize the painting of a toolstrip object (in this case, my context menu). See the DropDownRenderer class in the source for the implementation.

Conclusion

I'm not going to repeat myself.  I hope you like this control. If you make significant improvements to it, please send them my way so I can capitalize on them. Peace.

History

  • 8th December, 2006: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

christophilus
Software Developer
United States United States
I'm a professional geek.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionIe8 Tab Pin
Gabee812-Sep-14 20:24
memberGabee812-Sep-14 20:24 
Generaltab pages Pin
Tsoperas18-Feb-11 2:27
memberTsoperas18-Feb-11 2:27 
QuestionHow can i use this "dll" in C# Project ? Pin
himan136515-Sep-10 4:24
memberhiman136515-Sep-10 4:24 
GeneralNew C# version Pin
prozomg2-Jan-10 4:54
memberprozomg2-Jan-10 4:54 
GeneralRe: New C# version Pin
Lloyd Atkinson15-Sep-10 6:43
memberLloyd Atkinson15-Sep-10 6:43 
Generalblank page after minimize application Pin
DonSalieri8617-Sep-09 5:57
memberDonSalieri8617-Sep-09 5:57 
hi when i minimize my application or i click on other application my tabpages become white and lose the form...why?
QuestionChange background color Pin
Radu_2020-Jan-09 5:30
memberRadu_2020-Jan-09 5:30 
GeneralAny suggestions Pin
eyanson14-Jan-09 8:24
membereyanson14-Jan-09 8:24 
QuestionHi Pin
Radu_2013-Jan-09 2:18
memberRadu_2013-Jan-09 2:18 
GeneralVS 2008 C# version Pin
wejaya2-Dec-08 8:20
memberwejaya2-Dec-08 8:20 
GeneralVS 2008 c# version Pin
sodevrom17-Nov-08 11:03
membersodevrom17-Nov-08 11:03 
GeneralRe: VS 2008 c# version Pin
Caglow22-Nov-08 9:15
memberCaglow22-Nov-08 9:15 
GeneralRe: VS 2008 c# version Pin
MicheleLaPietra17-Sep-09 1:08
memberMicheleLaPietra17-Sep-09 1:08 
QuestionC# implementation Pin
leyroyjenkins2-Oct-08 14:51
memberleyroyjenkins2-Oct-08 14:51 
AnswerRe: C# implementation Pin
Caglow19-Oct-08 11:15
memberCaglow19-Oct-08 11:15 
GeneralRe: C# implementation Pin
sodevrom10-Nov-08 3:55
membersodevrom10-Nov-08 3:55 
AnswerRe: C# implementation Pin
Osama Ibrahim15-Nov-08 1:07
memberOsama Ibrahim15-Nov-08 1:07 
GeneralRe: C# implementation Pin
sodevrom16-Nov-08 8:29
membersodevrom16-Nov-08 8:29 
GeneralWhy didn't you implement it as a subclassed control? [modified] Pin
Paul Owen2-Apr-08 0:56
memberPaul Owen2-Apr-08 0:56 
GeneralLicensing Pin
Sam N8-Feb-08 4:50
memberSam N8-Feb-08 4:50 
GeneralA very nice control - a couple of suggestions. Pin
gabriel6622-Oct-07 23:44
membergabriel6622-Oct-07 23:44 
GeneralRe: A very nice control - a couple of suggestions. Pin
Thord Johansson7-Mar-09 11:23
memberThord Johansson7-Mar-09 11:23 
GeneralUsage terms and conditions Pin
kumar431-Sep-07 14:38
memberkumar431-Sep-07 14:38 
GeneralNice Tab Control - One Suggestion Pin
viettho4-Jun-07 12:29
memberviettho4-Jun-07 12:29 
GeneralVisual Studio 2003 keeps throwing error... Pin
cdawg20-Apr-07 12:17
membercdawg20-Apr-07 12:17 
GeneralVery nice work Pin
Marek Bohac16-Mar-07 22:57
memberMarek Bohac16-Mar-07 22:57 
GeneralTab Flicker fix - simple Pin
niperumm11-Jan-07 8:38
memberniperumm11-Jan-07 8:38 
GeneralNice Control, One Issue Pin
Chris Nevill11-Jan-07 3:46
memberChris Nevill11-Jan-07 3:46 
GeneralCoooooooool Control Pin
Ebnez4-Jan-07 8:17
memberEbnez4-Jan-07 8:17 
QuestionVersion for use in IDE? Pin
Bob Ranck23-Dec-06 9:44
memberBob Ranck23-Dec-06 9:44 
GeneralMultiline tabs Pin
micmit_syd13-Dec-06 11:59
membermicmit_syd13-Dec-06 11:59 
GeneralRe: Multiline tabs Pin
christophilus16-Dec-06 14:19
memberchristophilus16-Dec-06 14:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 8 Dec 2006
Article Copyright 2006 by christophilus
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid