Click to See Complete Forum and Search --> : TabPage Image + DrawItem


sonia.sardana
June 14th, 2009, 03:00 PM
hi frnds, I want to add cross button image on the right hand side of tabPage...I m creating tabpages at Runtime....
Mine code is added image to the left...I want to add it to the right....I m also adding text to the tabPage...

& second Probs is dat..On button click suppose i add the first tab Page,Image is aded to the first Tab Page...When i click on button to create the second Tab,Image & text of First Tab Page is clear & so on...

PLZ help me on this matter
Mine code below-

Public Class Form2
Dim TabPageIndex As Integer
Dim tab As TabPage = New TabPage("SONIA SARDANA")
Public Sub New()
InitializeComponent()
TabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
End Sub

Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
Try

Dim recBounds As Rectangle = Me.TabControl1.GetTabRect(TabPageIndex)
Dim tabTextArea As RectangleF = _
CType(Me.TabControl1.GetTabRect(TabPageIndex), RectangleF)

Dim img As Image = ImgIcons.Images(0)

Dim nLeftMargin As Integer = 8
Dim nRightMargin As Integer = 2

Dim rimage As Rectangle = New Rectangle(recBounds.X + _
nLeftMargin, recBounds.Y + 1, img.Width, img.Height)
Dim nAdj As Single = CType((nLeftMargin + _
img.Width + nRightMargin), Single)
rimage.Y += (recBounds.Height - img.Height) / 2
tabTextArea.X += nAdj
tabTextArea.Width -= nAdj

e.Graphics.DrawImage(img, rimage)
Dim stringFormat As StringFormat = New StringFormat
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center

Dim br As Brush
br = New SolidBrush(Color.Black)
e.Graphics.DrawString(tab.Text, Me.Font, Brushes.Black, tabTextArea, stringFormat)


Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Static lTabIndex As Integer
lTabIndex = lTabIndex + 1
tab.Name = "Tab" & lTabIndex
TabControl1.TabPages.Add(tab)
TabPageIndex = TabControl1.TabPages.Count - 1
AddHandler TabControl1.DrawItem, AddressOf TabControl1_DrawItem

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

sonia.sardana
June 14th, 2009, 03:29 PM
UPDATED QUESTION,cz there is no edit opetion in the site-
hi frnds, I want to add cross button image on the right hand side of tabPage...I m creating tabpages at Runtime....
Mine code is added image to the Right along with the text at the left...

I want to just ask that is that possible to write the code the image...so dat i m able to close tab when i click on the image
PLZ help me on this matter
Mine code below that is adding tab at runtime along with image-


Public Class Form2
Dim TabPageIndex As Integer
Dim tab As TabPage = New TabPage("SONIA SARDANA")
Public Sub New()
InitializeComponent()
TabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
End Sub

Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
Try


Dim img As Image = ImgIcons.Images(0)

Dim _imageLocation As Point = New Point(15, 5)
Dim _imgHitArea As Point = New Point(13, 2)


Dim r As Rectangle = e.Bounds
r = Me.TabControl1.GetTabRect(e.Index)
r.Offset(2, 2)

Dim TitleBrush As Brush = New SolidBrush(Color.Black)
Dim f As Font = Me.Font

Dim title As String = Me.TabControl1.TabPages(e.Index).Text
e.Graphics.DrawString(title, f, TitleBrush, New PointF(r.X, r.Y))
e.Graphics.DrawImage(img, New Point(r.X + (Me.TabControl1.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y))

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Static lTabIndex As Integer
lTabIndex = lTabIndex + 1
tab.Name = "Tab" & lTabIndex
TabControl1.TabPages.Add(tab)
TabPageIndex = TabControl1.TabPages.Count - 1
AddHandler TabControl1.DrawItem, AddressOf TabControl1_DrawItem

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

dglienna
June 14th, 2009, 03:52 PM
Click on a post, and you can edit it as many time as you want.

You need to download the code samples found here (http://code.msdn.microsoft.com/vbsamples/)