0

I created a COM Interop object that includes visual studio windows forms for Microsoft Access to run. In other words, I am able to open my Windows Forms inside Microsoft Access, after registering my dll and then converting it into tlb.

Everything works fine except when the form opens the tab control (giving focus to controls by pressing TAB) functionality or pressing ENTER when a button has focus does not work.

When I run my COM object in another C# application everything works fine. Only when I try to run it in Microsoft Access I have this problem.

enter image description here

My Windows form has 4 text boxes and a button with nothing else. And all of my controls TabStop property is set to TRUE and TabIndex values are set as well. When I go to View -> Tab Order I can see everything is set correctly. But yet, the TAB button wont work.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0014
  • 893
  • 4
  • 13
  • 40

1 Answers1

0

I believe it is a kind of bug, therefore I wrote a function that dynamically searches for the next tab index and gives that control focus after user presses on the TAB button.

Here is the snipped code;

private Control _control;
private int _tabIndex;

public Form1()
{
    InitializeComponent();

    KeyDown += FormEvent; // subscribe to event
}

private void FormEvent(object sender, KeyEventArgs e)
{
    // if pressed key is not tab, or there is no active form return
    if (e.KeyCode != Keys.Tab || ActiveForm == null) return;

    // get the control which currently has focus and get its tab index
    _control = ActiveForm.ActiveControl;
    _tabIndex = _control.TabIndex;

    // iterate through all the controls on the form
    for (var i = 0; i < Controls.Count; i++)
    {
        // continue until the next control which has to gain focus is found
        if (Controls[i].TabIndex != _tabIndex + 1) continue;

        // control found but its not suppose to gain focus on tab,
        if (!Controls[i].TabStop)
        {
            i = -1; // start from the beggining of the loop
            _tabIndex++; // search for the next control
        }
        // control found, focus on it
        else
            ActiveForm.ActiveControl = Controls[i];
    }
}
0014
  • 893
  • 4
  • 13
  • 40