ErrorText 和 ContextMenuStrip 之间的交互

Interaction between ErrorText and ContextMenuStrip

我的应用程序有一个带有 RowValidating 事件处理程序的数据网格视图。如果我在 RowValidating 中设置 ErrorText,它会正确地禁止左键单击其他行和其他控件,但如果我将 ContextMenuStrip 分配给另一个控件,则 ContextMenuStrip 将保持活动状态。用户可以右键单击带有上下文菜单的控件,select 一个菜单项,然后会触发菜单事件。

我已经尝试了各种事件处理,但 RowValidating 事件不会在菜单出现之前触发,所以我不能只禁用 RowValidating 中的上下文菜单。每当用户触摸网格的任何部分时,我也许可以禁用上下文菜单,但这还有其他缺陷...

我已经看过,但我没有看到任何关于 Microsoft 缺陷或解决方法的现有文章,甚至没有其他人遇到该错误,所以我不确定我可能做错了什么。

简化示例:

 private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.menuItemToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.contextMenuStrip1.SuspendLayout();
        this.SuspendLayout();
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column1});
        this.dataGridView1.RowValidating += new System.Windows.Forms.DataGridViewCellCancelEventHandler(this.dataGridView1_RowValidating);
        this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.menuItemToolStripMenuItem});
        this.menuItemToolStripMenuItem.Text = "Menu Item";
        this.textBox1.ContextMenuStrip = this.contextMenuStrip1;
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.dataGridView1);

        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.contextMenuStrip1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
    private System.Windows.Forms.ToolStripMenuItem menuItemToolStripMenuItem;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.DataGridViewTextBoxColumn Column1;


    private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (!RowValid(dataGridView1.Rows[e.RowIndex]))
        {
            dataGridView1.Rows[e.RowIndex].ErrorText = "Value must be 5";
            e.Cancel = true;
        }
        else
        {
            dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
            e.Cancel = false;
        }
    }

    private bool RowValid(DataGridViewRow row)
    {
        return string.Equals(row.Cells[0].Value, "5");
    }

供参考,这些是按顺序触发的事件,注意在菜单打开之前不会进行验证:

dataGridView1_Enter
dataGridView1_RowEnter
dataGridView1_CellEnter
dataGridView1_CellBeginEdit
contextMenuStrip1_Opening

处理 ToolStripDropDown.Opening 事件,通过设置 e.Cancel 确定上下文菜单是否应该显示。或者您可以改为禁用菜单项。