can Sub handles tabcontrol.drawitem in module?

cwfontan

Active member
Joined
Jan 9, 2009
Messages
35
Programming Experience
1-3
sub I use on all forms in my app I would like to put in one central place (of course right).
in vb.net
I put this sub in a module
it has a "Handles tabcontrol1.Drawitem" - I get an error saying handle requires with events variable defined in containing type or one of its base types.

also
when I refer to tabcontrol1 in a module it wants to know which form.. Can I use sender to define current calling form?


sub
VB.NET:
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
        Dim g As Graphics = e.Graphics
        Dim tp As TabPage = [B]sender.[/B]TabControl1.TabPages(e.Index)
        Dim br As Brush
        Dim sf As New StringFormat
        Dim r As New RectangleF(e.Bounds.X - 2, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)
       sf.Alignment = StringAlignment.Center
        Dim strTitle As String = tp.Text
        'If the current index is the Selected Index, change the color
        If sender.TabControl1.SelectedIndex = e.Index Then
            'this is the background color of the tabpage
            'you could make this a stndard color for the selected page
            br = New SolidBrush(tp.BackColor)
            'this is the background color of the tab page
            g.FillRectangle(br, e.Bounds)
            'this is the background color of the tab page
            'you could make this a stndard color for the selected page
            br = New SolidBrush(tp.ForeColor)
            g.DrawString(strTitle, sender.TabControl1.Font, br, r, sf)
        Else
            'these are the standard colors for the unselected tab pages
            br = New SolidBrush(Color.WhiteSmoke)
            g.FillRectangle(br, e.Bounds)
            br = New SolidBrush(Color.Black)
            g.DrawString(strTitle, sender.TabControl1.Font, br, r, sf)
        End If
    End Sub
 
class with events

i Created a class, and raise the event on the forms. I send (sender, e) to the class.. <is that the correct way?

also in the class itself.. it says tabcontrol1 not defined.
I cant just explicitly supply form name because multiple forms are going to use this.. so how do I handle this?


VB.NET:
 Dim WithEvents myEvents As New myevents1()

 Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
        myEvents.TabControl1_DrawItem(sender, e)
    End Sub
VB.NET:
Public Class myevents1
    Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim g As Graphics = e.Graphics
        Dim tp As TabPage = TabControl1.TabPages(e.Index)
        Dim br As Brush
        Dim sf As New StringFormat
        Dim r As New RectangleF(e.Bounds.X - 2, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)
        sf.Alignment = StringAlignment.Center
        Dim strTitle As String = tp.Text
        'If the current index is the Selected Index, change the color
        If sender.TabControl1.SelectedIndex = e.Index Then
            'this is the background color of the tabpage
            'you could make this a stndard color for the selected page
            br = New SolidBrush(tp.BackColor)
            'this is the background color of the tab page
            g.FillRectangle(br, e.Bounds)
            'this is the background color of the tab page
            'you could make this a stndard color for the selected page
            br = New SolidBrush(tp.ForeColor)
            g.DrawString(strTitle, sender.TabControl1.Font, br, r, sf)
        Else
       End If
    End Sub
End Class
 
ok I have it working.. I took out the sender.TabControl1. and replaced with just sender.

in the class sub.. now just final question of.. is this the best way to do this for shared subs? or am i off?
 
Back
Top