|
VB.NETManage the Shortcuts
Listing 1. Visual Basic 6 handled conflicting shortcuts properly on different tab pages without extra work on your part, but Visual Basic .NET is a little more, let's say, "flexible" in this area. You need to manage the conflicting shortcuts yourself. Removing and re-adding shortcut indicators (ampersand characters) as needed works well. Public Class ShortcutTabControl
Inherits System.Windows.Forms.TabControl
Private shortcutLookup As New _
System.Collections.SortedList
Private lastTab As Windows.Forms.TabPage
Public Sub InitShortcuts()
' ----- Make note of all of the shortcuts.
Dim scanTabs As Windows.Forms.TabPage
Dim scanControl As Windows.Forms.Control
' ----- Scan through all tabs and controls.
shortcutLookup.Clear()
For Each scanTabs In Me.TabPages
For Each scanControl In _
scanTabs.Controls
' ----- Assumes "&" equals shortcut.
If (InStr(scanControl.Text, _
"&") > 0) Then
' ----- Found control with "&".
shortcutLookup.Add( _
scanControl.Name, _
scanControl.Text)
' ----- If this is an inactive
' tab, remove shortcut now.
If Not (Me.SelectedTab Is _
scanTabs) Then _
scanControl.Text = _
Replace(scanControl.Text, _
"&", "")
End If
Next scanControl
Next scanTabs
' ----- Keep track of the last used tab.
lastTab = Me.SelectedTab
End Sub
Protected Overrides Sub _
OnSelectedIndexChanged( _
ByVal e As System.EventArgs)
' ----- Clear shortcuts from last tab.
Dim scanControl As Windows.Forms.Control
Dim matchEntry As Object
If Not (lastTab Is Nothing) And _
(MyBase.DesignMode = False) Then
For Each scanControl In lastTab.Controls
' ----- Remove & from shortcuts.
matchEntry = shortcutLookup.Item( _
scanControl.Name)
If Not (matchEntry Is Nothing) Then
' ----- Remove the shortcut.
scanControl.Text = Replace( _
scanControl.Text, "&", "")
End If
Next scanControl
' ----- Active tab: restore shortcuts.
lastTab = Me.SelectedTab
For Each scanControl In lastTab.Controls
' ----- See if we stored the caption
' for this control.
matchEntry = shortcutLookup. _
Item(scanControl.Name)
If Not (matchEntry Is Nothing) Then
' ----- Restore the caption.
scanControl.Text = _
CStr(matchEntry)
End If
Next scanControl
End If
' ----- Call the core control's routine.
MyBase.OnSelectedIndexChanged(e)
End Sub
End Class
|