如何在 ms-access VBE-Add-in 中删除一行活动代码窗格后当前设置光标?

How to set the cursor currently after deleting a line of the active CodePane in an ms-access VBE-Add-in?

我为 ms-access 的 VBA-Editor 编写了一个 C# 插件。参见:

我的加载项搜索代码,对其进行标记,然后在第二次单击后将其删除。

活动模块里面有三行代码:

Option Compare Database
'Delete This
Option Explicit

这是我用的代码(当然是还原题):

int m_called = 0;
protected override void button_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
    if (m_called == 0)
    {
        m_VBE.ActiveCodePane.SetSelection(2, 1, 2, 13);
    }
    else
    {
        m_VBE.ActiveCodePane.CodeModule.DeleteLines(2, 1);
        m_VBE.ActiveCodePane.SetSelection(2, 2, 2, 2);
    }
    m_called++;
}

在 运行 此代码之后,字母:"Option Expli" 被选中。

这仅在通过 "SetSelection"(第一行)设置第一个选择时发生。相反,手动选择完全相同的部分会导致选择的正确位置(= 作为 "Option Explicit" 之前第二行开头的行光标)。

将光标设置到第 1 行即可。将它设置为第 2 行的第二个字符(2、2、2、2;在 "O" 和 "p" 之间)也有效。从字面上看,除第二行开头以外的所有其他位置都可以正常工作!

那么这个奇怪的错误有解决方法吗?

这是我试过的:

好的...您必须先将 Selection 设置到另一个位置,然后才能将其设置到第二行的开头。

所以这段代码对我有用:

int m_called = 0;
protected override void button_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
    if (m_called == 0)
    {
        m_VBE.ActiveCodePane.SetSelection(2, 1, 2, 13);
    }
    else
    {
        m_VBE.ActiveCodePane.CodeModule.DeleteLines(2, 1);
        m_VBE.ActiveCodePane.SetSelection(1, 1, 1, 1);
        m_VBE.ActiveCodePane.SetSelection(2, 1, 2, 1);
    }
    m_called++;
}