C# Windows Access 中的表单 Com Interop 对象 - 选项卡或输入按钮不起作用

C# Windows Form Com Interop Object in Access - tab or enter button not working

我创建了一个 COM Interop 对象,其中包含 visual studio windows Microsoft Access 表单 运行。换句话说,在注册我的 dll 然后将其转换为 tlb 之后,我可以在 Microsoft Access 中打开我的 Windows 表单。

一切正常,除了当窗体打开选项卡控件(通过按 TAB 使控件获得焦点)功能或按 ENTER 时有焦点的按钮不起作用。

当我 运行 我的 COM 对象在另一个 C# 应用程序中时,一切正常。只有当我在 Microsoft Access 中尝试 运行 时,我才会遇到这个问题。

我的 Windows 表单有 4 个文本框和一个按钮,没有其他任何东西。我的所有控件 TabStop 属性 都设置为 TRUE 并且 TabIndex 值已设置以及。当我转到 View -> Tab Order 时,我可以看到一切都已正确设置。但是,TAB 按钮无法使用。

我认为这是一种错误,因此我编写了一个动态搜索下一个选项卡索引并在用户按下 TAB 按钮后提供控件焦点的函数。

这是截取的代码;

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];
    }
}