Problem with TabControl Resizing

S

Simon Verona

I have a few hundred forms in my application. All are based on a custom base form class.

I decided that I wanted to globally change the look and feel of many of the controls in my application - including the tab control. I did this by writing a component that I attached to the form. The component has a "Start" function which takes the form as a paramater. This cycles through all the controls on the form, changes some of the properties and adds handlers to the paint control where required.

For the tabcontrol I've created handlers for the paint event on both the tabcontrol and the tabpages to use the following methods :

===============================================================================
Private Sub TabControlDrawTab(ByVal sender As Object, ByVal e As DrawItemEventArgs)

Dim tc As TabControl = sender

Dim rect As Rectangle = tc.GetTabRect(e.Index)

Dim text As String = tc.TabPages(e.Index).Text

If tc.SelectedIndex = e.Index Then

e.Graphics.FillRectangle(New SolidBrush(Color.DarkBlue), rect)

Else

e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, 180, 255)), rect)

End If

' Work out the text to draw

If tc.SelectedIndex = e.Index Then

e.Graphics.DrawString(text, tc.Font, New SolidBrush(Color.White), rect.X + 1, rect.Y + 1)

Else

e.Graphics.DrawString(text, tc.Font, New SolidBrush(Color.DarkBlue), rect.X + 1, rect.Y + 1)

End If





End Sub

Private Sub TabControlPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

Dim rect As Rectangle = e.ClipRectangle

'Dim tp As TabPage = sender

'Dim tc As TabControl = DirectCast(tc.Parent, TabControl)

'rect = tc.DisplayRectangle

e.Graphics.DrawRectangle(New Pen(Color.DarkBlue, 4), rect.X, rect.Y, rect.Width, rect.Height)

'e.Graphics.FillRectangle(New SolidBrush(Color.Red), rect) 'Color.FromArgb(180, 180, 255)), rect)

End Sub

======================================================================


This seems to work (the selected tab is dark blue and the other tabs are lighter). However, many of my tab controls are anchored to the edges of the form (by having the anchor set to top/bottom/left/right) - when I have my component running, the tab controls no longer resize. However, group boxes (which I do the same thing to) seem to resize fine.

Am I doing something conceptually incorrect ? What do I need to do to get the tab controls to resize correctly?

Thanks in advance.
Simon
 
S

Simon Verona

Sorry to add to my own post.. It's the "DrawItem" event I've picked up on the tabcontrol NOT the paint.

Regards
Simon

--
Simon Verona

I have a few hundred forms in my application. All are based on a custom base form class.

I decided that I wanted to globally change the look and feel of many of the controls in my application - including the tab control. I did this by writing a component that I attached to the form. The component has a "Start" function which takes the form as a paramater. This cycles through all the controls on the form, changes some of the properties and adds handlers to the paint control where required.

For the tabcontrol I've created handlers for the paint event on both the tabcontrol and the tabpages to use the following methods :

===============================================================================
Private Sub TabControlDrawTab(ByVal sender As Object, ByVal e As DrawItemEventArgs)

Dim tc As TabControl = sender

Dim rect As Rectangle = tc.GetTabRect(e.Index)

Dim text As String = tc.TabPages(e.Index).Text

If tc.SelectedIndex = e.Index Then

e.Graphics.FillRectangle(New SolidBrush(Color.DarkBlue), rect)

Else

e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, 180, 255)), rect)

End If

' Work out the text to draw

If tc.SelectedIndex = e.Index Then

e.Graphics.DrawString(text, tc.Font, New SolidBrush(Color.White), rect.X + 1, rect.Y + 1)

Else

e.Graphics.DrawString(text, tc.Font, New SolidBrush(Color.DarkBlue), rect.X + 1, rect.Y + 1)

End If





End Sub

Private Sub TabControlPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

Dim rect As Rectangle = e.ClipRectangle

'Dim tp As TabPage = sender

'Dim tc As TabControl = DirectCast(tc.Parent, TabControl)

'rect = tc.DisplayRectangle

e.Graphics.DrawRectangle(New Pen(Color.DarkBlue, 4), rect.X, rect.Y, rect.Width, rect.Height)

'e.Graphics.FillRectangle(New SolidBrush(Color.Red), rect) 'Color.FromArgb(180, 180, 255)), rect)

End Sub

======================================================================


This seems to work (the selected tab is dark blue and the other tabs are lighter). However, many of my tab controls are anchored to the edges of the form (by having the anchor set to top/bottom/left/right) - when I have my component running, the tab controls no longer resize. However, group boxes (which I do the same thing to) seem to resize fine.

Am I doing something conceptually incorrect ? What do I need to do to get the tab controls to resize correctly?

Thanks in advance.
Simon
 
S

Simon Verona

More information - this only seems to occur if the form is itself an MDI child of a main form..

Regards
Simon

--
Simon Verona

Sorry to add to my own post.. It's the "DrawItem" event I've picked up on the tabcontrol NOT the paint.

Regards
Simon

--
Simon Verona

I have a few hundred forms in my application. All are based on a custom base form class.

I decided that I wanted to globally change the look and feel of many of the controls in my application - including the tab control. I did this by writing a component that I attached to the form. The component has a "Start" function which takes the form as a paramater. This cycles through all the controls on the form, changes some of the properties and adds handlers to the paint control where required.

For the tabcontrol I've created handlers for the paint event on both the tabcontrol and the tabpages to use the following methods :

===============================================================================
Private Sub TabControlDrawTab(ByVal sender As Object, ByVal e As DrawItemEventArgs)

Dim tc As TabControl = sender

Dim rect As Rectangle = tc.GetTabRect(e.Index)

Dim text As String = tc.TabPages(e.Index).Text

If tc.SelectedIndex = e.Index Then

e.Graphics.FillRectangle(New SolidBrush(Color.DarkBlue), rect)

Else

e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, 180, 255)), rect)

End If

' Work out the text to draw

If tc.SelectedIndex = e.Index Then

e.Graphics.DrawString(text, tc.Font, New SolidBrush(Color.White), rect.X + 1, rect.Y + 1)

Else

e.Graphics.DrawString(text, tc.Font, New SolidBrush(Color.DarkBlue), rect.X + 1, rect.Y + 1)

End If





End Sub

Private Sub TabControlPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

Dim rect As Rectangle = e.ClipRectangle

'Dim tp As TabPage = sender

'Dim tc As TabControl = DirectCast(tc.Parent, TabControl)

'rect = tc.DisplayRectangle

e.Graphics.DrawRectangle(New Pen(Color.DarkBlue, 4), rect.X, rect.Y, rect.Width, rect.Height)

'e.Graphics.FillRectangle(New SolidBrush(Color.Red), rect) 'Color.FromArgb(180, 180, 255)), rect)

End Sub

======================================================================


This seems to work (the selected tab is dark blue and the other tabs are lighter). However, many of my tab controls are anchored to the edges of the form (by having the anchor set to top/bottom/left/right) - when I have my component running, the tab controls no longer resize. However, group boxes (which I do the same thing to) seem to resize fine.

Am I doing something conceptually incorrect ? What do I need to do to get the tab controls to resize correctly?

Thanks in advance.
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top