1

ToolStripButtons を備えた ToolStrip を含む WinForms アプリケーションがあります。一部のボタン アクションは、ボタン アクションの実行中にメイン フォームを無効にし、終了時に再度有効にします。これは、アクションの実行中にユーザーが他の場所をクリックしないようにするために行われ、WaitCursor も表示されますが、これは問題には関係ありません。

フォームが無効になっているときに、ユーザーがボタンをクリックしてマウス カーソルをその境界の外に移動すると、後でフォームを再度有効にしても、ボタンは強調表示されたまま (透明な青色) になります。その後、マウスがボタンに出入りすると、再び正しく表示されます。

次のコードを使用して MessageBox を表示することで、人為的に問題を再現できます (実際のアクションではメッセージ ボックスは表示されませんが、新しいフォームが開かれ、グリッドにデータが入力されますが、最終的な効果は同じです)。

問題を再現するためのコード スニペットを次に示します。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        // Disable the form
        Enabled = false; 

        // Some action where the user moved the mouse cursor to a different location
        MessageBox.Show(this, "Message");

        // Re-enable the form
        Enabled= true; 
    }
}
4

1 に答える 1

3

私は最終的に解決策を見つけました。

リフレクションを使用して、親ツールストリップでプライベート メソッド "ClearAllSelections" を呼び出すこの拡張メソッドを作成しました。

    public static void ClearAllSelections(this ToolStrip toolStrip)
    {
        // Call private method using reflection
        MethodInfo method = typeof(ToolStrip).GetMethod("ClearAllSelections", BindingFlags.NonPublic | BindingFlags.Instance);
        method.Invoke(toolStrip, null);
    }

フォームを再度有効にした後に呼び出します。

private void toolStripButton1_Click(object sender, EventArgs e)
{
    // Disable the form
    Enabled = false; 

    // Some action where the user moved the mouse cursor to a different location
    MessageBox.Show(this, "Message");

    // Re-enable the form
    Enabled= true;

    // Hack to clear the button highlight
    toolStrip1.ClearAllSelections();
}
于 2016-11-29T17:05:00.130 に答える