|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionA project I have been working on required a tabbed user interface, and for some time, we had been using the excellent tab control from Infragistics. However, I wanted to use the native .NET When VS.NET 2005 was released, I was hoping that the So, having decided to subclass the native There was a third part to this, I had to add the tab closer and the tab navigation buttons to the top right corner of the control. I did that myself, but the solution is a bit flaky so I have left that out of this article. Using the codeThe solution I came up with is shown in my demo application, and consists of the sub classed To use the control in your own applications, just copy the two classes into your code. I used SharpDevelop 2.0 to create the solution, and it works fine from VS.NET 2005 as well. I have included both a C# version and a VB.NET version in the solution, though the article code snippets are from the VB.NET version. It should be possible to do the same thing in .NET 1.0 or 1.1, but I am working exclusively in .NET 2.0 now. So, how did I solve the problems? The SDK explains how you can set the So, I found that you can set the control style to Me.SetStyle(ControlStyles.UserPaint, True)
Me.ItemSize = New Size(0, 15)
The simplest solution to getting rid of the grey border turned out to be to set the control to have a transparent background and set the colour of the containing control. Unfortunately, invoking the Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Then, you override the Protected Overrides Sub OnPaintBackground( _
ByVal pevent As System.Windows.Forms.PaintEventArgs)
If Me.DesignMode Then
' If this is in the designer let's put a nice gradient on the back
' By default the tabcontrol has a fixed grey background. Yuck!
Dim backBrush As New LinearGradientBrush( _
Me.Bounds, _
SystemColors.ControlLightLight, _
SystemColors.ControlLight, _
Drawing2D.LinearGradientMode.Vertical)
pevent.Graphics.FillRectangle(backBrush, Me.Bounds)
backBrush.Dispose()
Else
' At runtime we want a transparent background.
' So let's paint the containing control (there has to be one).
Me.InvokePaintBackground(Me.Parent, pevent)
Me.InvokePaint(Me.Parent, pevent)
End If
End Sub
Painting the tabs is a little trickier. Most of the time, only the currently selected tab and the previously selected tab repaint. The clip rectangle of the I have broken down the tab painting into a number of stages. See my code for details.
Points of interestI found that most people must have tried the .NET SDK way of painting the tabs and found it didn't work. They then proceeded to write their own tab controls, with all the problems of getting the design time experience just right, and so on. Although it took me a while to stumble across this solution, I think it is much cleaner (but then, I am biased in that respect). I retained the design time experience and the functionality of the underling .NET control, along with the speed gain to the application due to using the native control. And, I made it paint just the way I wanted. The only part I failed to fully replicate from the VS 2005 IDE was that their selected tab seems to slightly overlap the tab to the left. Try as I might, I could not get that to work as the Also, I mentioned that I had a way to add the tab close and navigation buttons. You can paint stuff up there, but it won't respond to mouse events, at least not without writing your own mouse handling code! So, my solution was to make the custom tab control dynamically add these buttons to the containing control and manually position them over the top of the tab control at run time. Unfortunately, the docking and anchoring in .NET 2.0 doesn't work properly, or at least not in the way it did in 1.1, and so my nicely not docked, or anchored buttons would keep stretching and moving in response to events from the .NET docking manager! In the very controlled environment I created for our project, this was not a problem; however it's not the sort of flaky solution you want to publish. History
|
||||||||||||||||||||||