简体   繁体   English

ErrorText和ContextMenuStrip之间的交互

[英]Interaction between ErrorText and ContextMenuStrip

My application has a datagridview with a RowValidating event handler. 我的应用程序具有一个带有RowValidating事件处理程序的datagridview。 If I set ErrorText in RowValidating, it correctly disallows left-clicking on other rows and other controls, but if I have a ContextMenuStrip assigned to another control, the ContextMenuStrip remains active. 如果我在RowValidating中设置ErrorText ,则正确禁止在其他行和其他控件上单击ErrorText左键,但是如果我将ContextMenuStrip分配给另一个控件,则ContextMenuStrip仍处于活动状态。 The user can right-click on the control with the context menu, select a menu item, and the menu events fire. 用户可以右键单击带有上下文菜单的控件,选择一个菜单项,然后触发菜单事件。

I've tried all sorts of event handling, but the RowValidating event doesn't fire before the menu is presented, so I can't just disable the context menu in RowValidating. 我已经尝试了各种事件处理,但是在显示菜单之前不会触发RowValidating事件,因此我不能只禁用RowValidating中的上下文菜单。 I could perhaps disable the context menu whenever the user is touching any part of the grid, but that has other pitfalls... 每当用户触摸网格的任何部分时,我都可能禁用上下文菜单,但这有其他陷阱。

I've looked and I don't see any existing writeup on a Microsoft defect or a workaround, or even anyone else encountering the error, so I'm not sure what I could be doing wrong. 我已经查看过,但没有看到关于Microsoft缺陷或解决方法的任何现有文章,甚至看不到任何其他遇到此错误的人,因此我不确定自己可能做错了什么。

Simplified example: 简化示例:

 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");
    }

For reference, these are the events fired in order, note validation does not occur prior to menu opening: 作为参考,这些是按顺序触发的事件,在打开菜单之前不会进行注释验证:

dataGridView1_Enter
dataGridView1_RowEnter
dataGridView1_CellEnter
dataGridView1_CellBeginEdit
contextMenuStrip1_Opening

Handle ToolStripDropDown.Opening event, determine if the context menu should showup or not by setting e.Cancel . 处理ToolStripDropDown.Opening事件,通过设置e.Cancel确定上下文菜单是否应该显示。 Or you can disable the menu item instead. 或者,您也可以禁用菜单项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM