How can I make sure that when I click on the ToolStripMenuItem, only one form opens?

  Kiến thức lập trình

There are several ToolStripMenuItem elements that open different forms when clicked. The problem is that if I click N times on one of the ToolStripMenuItem elements, then after closing the new form it will open several more times (N-1). How can I fix this if I use Form.ShowDialog()?

private void DataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        contextMenu1.Show(MousePosition);
        contextMenu1.Items[0].Click += ContextMenuItem0_Click;
    }
}

private void ContextMenuItem0_Click(object? sender, EventArgs e)
{
    int numberRow = dataGridView1.CurrentCell.RowIndex;
    idRow = Convert.ToInt32(dataGridView1.Rows[numberRow].Cells[0].Value);
    FeaturesOrder featuresOrder = new(idRow);
    featuresOrder.ShowDialog(); //HERE
}

I found a solution where I need to check if there is a form in the collection of open forms so that no duplicates are created. But it’s the usage that I need Form.ShowDialog().

New contributor

Елизавета Левина is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT