VB.NET Custom ArcMap Tool:  Won't tab!

1615
3
06-09-2010 10:17 PM
RuchiraWelikala
Occasional Contributor
Hi All,
Just curious to see if anyone else has run into this problem.  I've created many forms with many textboxes and other controls which deploy perfectly in ArcMap (using VB.NET).  However, it won't let me tab through the controls on any particular form.  Is this some kind of bug in ArcMap with .NET or is it just me?  My other forms (Developed in VBA) Tab perfectly, but the issue seems to lie with the ones created in .NET.

Any info on this would be helpful. 
Thanks
0 Kudos
3 Replies
NeilClemmons
Regular Contributor III
If you show the dialog modal (form.ShowDialog()) then everything works as expected.  If you show the form modeless (form.Show()) then the form does not process keystrokes as you would expect.  Follow the link in my signature to my company blog and you'll find a couple of articles that address this issue.
0 Kudos
AlexanderGray
Occasional Contributor III
This is what I did to work around this problem.  I make a call to WorkaroundTabIndexDefect when my control becomes enabled (on start editing for me.)  You will need to remove those handlers at some point too.

  Private Sub WorkaroundTabIndexDefect()
    For Each ctr As Control In Me.Controls 
      SetKeyDownHandlers(ctr)
    Next
  End Sub

  Private Sub SetKeyDownHandlers(ByVal ctr As Control)
    Dim txtCtr As TextBox = TryCast(ctr, TextBox)
    If txtCtr IsNot Nothing Then
      txtCtr.AcceptsTab = True
      txtCtr.AcceptsReturn = True
      Dim evh As KeyEventHandler = New KeyEventHandler(AddressOf WorkaroundKeyDown)
      AddHandler txtCtr.KeyDown, evh
    Else
      ctr.TabStop = False
    End If
    If ctr.Controls.Count > 0 Then
      For Each subCtr As Control In ctr.Controls
        SetKeyDownHandlers(subCtr)
      Next
    End If
  End Sub

  Private Sub WorkaroundKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    Try
      If Me.ActiveControl IsNot Nothing Then
        Select Case e.KeyCode
          Case Keys.Return
            Dim btnCtr As Windows.Forms.Button = TryCast(Me.ActiveControl, Windows.Forms.Button)
            If btnCtr IsNot Nothing Then
              btnCtr.PerformClick()
            End If
          Case Keys.Tab
            If e.Shift Then
              Me.SelectNextControl(CType(sender, Control), False, True, True, True)
            Else
              Me.SelectNextControl(CType(sender, Control), True, True, True, True)
            End If
        End Select
      End If
    Catch ex As Exception
      Trace.WriteLine(ex)
    End Try
  End Sub
0 Kudos
RuchiraWelikala
Occasional Contributor
Thanks for your responses Alexander and Neil. 
I went did the showdialog option last night and got it to work.  It actually seems a lot more practical.  I only have one non-modal form form and there is no user input so, it doesn't need to be tabbed. 

Thanks again guys!
0 Kudos