how to get Accelerator Keys working in vb.net tabs

narsingraoonlin

New member
Joined
Jul 15, 2006
Messages
1
Programming Experience
Beginner
3 steps to make accelerator key working in vb.net

Replace tab_Ulator with < your Tab name >

1 ) In form load

VB.NET:
Me.tab_Ulator.DrawMode = 
System.Windows.Forms.TabDrawMode.OwnerDrawFixed

2) Seperate function

VB.NET:
Protected Overrides Function ProcessMnemonic(ByVal charCode As Char) As
Boolean
For Each tp As TabPage In tab_Ulator.TabPages
If IsMnemonic(charCode, tp.Text) Then
tab_Ulator.SelectedTab = tp
Return True
End If
Next
Return MyBase.ProcessMnemonic(charCode)
End Function
3) Event

VB.NET:
Private Sub tab_Ulator_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles tab_Ulator.DrawItem
Dim f As Font
Dim foreBrush As SolidBrush
 
foreBrush = New SolidBrush(Color.Black)
If e.Index = Me.tab_Ulator.SelectedIndex Then
f = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Else
f = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 
CType(0,
Byte))
End If
 
Dim tc As TabControl = DirectCast(sender, TabControl)
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show
 
Dim r As RectangleF = New RectangleF(e.Bounds.X, e.Bounds.Y + 
4,
e.Bounds.Width, e.Bounds.Height - 4)
e.Graphics.DrawString(tc.TabPages(e.Index).Text, f, foreBrush, 
r,
sf)
sf.Dispose()
f.Dispose()
foreBrush.Dispose()
End Sub
 
Last edited by a moderator:
Back
Top