Made a right-click menu for Datagridview . But I can not understand, I launched the application, I clicked 1 time, the menu does not appear, clicked a second time - it appeared and then the rules are already working, I close, open the form again, the same problem, the first time I’ll not open, then the rules work. What am I doing wrong?

  private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { try { ContextMenuStrip shift_my_menu = new System.Windows.Forms.ContextMenuStrip(); int position_x_y_mouse = dataGridView1.HitTest(eX, eY).RowIndex; if (position_x_y_mouse >= 0) { shift_my_menu.Items.Add("Редактировать").Name = "edit"; shift_my_menu.Items.Add("Удалить").Name = "del"; } shift_my_menu.Show(dataGridView1, new Point(eX, eY)); shift_my_menu.ItemClicked += new ToolStripItemClickedEventHandler(shift_my_menu_ItemClicked); shift_my_menu.Items["del"].Image = MSU.Properties.Resources.delete; shift_my_menu.Items["edit"].Image = MSU.Properties.Resources.edit; 
  • Is the window active at the time of the first click? Maybe the window is not active and you make the window active by the first click, and only then it works all the way. - iluxa1810 September
  • You should not create a context menu every time you click the mouse. It is better to create it once and continue to use. If necessary, hide / show individual menu items (see their Visible property). - Alexander Petrov
  • @AlexanderPetrov, tried this way and that, from the first time the menu does not open, while focusing on datagridview , I call the menu in the main window with the same method, there is no such problem. Could this be due to the fact that the window is extra? - Winteriscoming
  • @ iluxa1810 when you call the datagridview menu while active. - Winteriscoming
  • @AlexanderPetrov, tried everything I knew, does not help, I ask you tell me what is wrong all the same? The method of creating the menu is the same on the main form and on the second one, everything works fine on the main one, on all secondary ones it calls only after the second click and then it works normally until the form is restarted. - Winteriscoming

0