3 Replies - 10137 Views - Last Post: 08 May 2012 - 04:38 PM

#1 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

ContextMenuStrip ToolStripMenuItem.Click Fires multiple times?

Posted 06 May 2012 - 07:21 PM

I'm dynamically creating ContextMenuStrip's and adding them to rows of my DataGridView. My delete button Click event seems to fire multiple times (usually 2-4) on one click though. I would assume my play button is doing the same, though it isn't as much of a problem. Am I setting up the ContextMenuStrip wrong by chance?

        private void AddContextMenu()
        {
            song_context_play.Text = "Play";
            song_context_play.Click += new System.EventHandler(song_context_play_Click);
            song_context_delete.Text = "Delete";
            song_context_delete.Click += new System.EventHandler(song_context_delete_Click);
            ContextMenuStrip song_context = new ContextMenuStrip();
            foreach (DataGridViewRow row in song_grid.Rows)
            {
                row.ContextMenuStrip = song_context;
                row.ContextMenuStrip.Items.Add(song_context_play);
                row.ContextMenuStrip.Items.Add(song_context_delete);
            }
        }

This post has been edited by Ntwiles: 06 May 2012 - 07:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ContextMenuStrip ToolStripMenuItem.Click Fires multiple times?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3649
  • View blog
  • Posts: 11,416
  • Joined: 05-May 12

Re: ContextMenuStrip ToolStripMenuItem.Click Fires multiple times?

Posted 07 May 2012 - 11:47 PM

You seem to be re-adding the same items to the song_context over and over again for each row found. I think what you want is to add the times just once outside the for loop, and then just assign the context menu string to all your rows.
Was This Post Helpful? 0
  • +
  • -

#3 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: ContextMenuStrip ToolStripMenuItem.Click Fires multiple times?

Posted 08 May 2012 - 02:43 PM

Thanks for the response! My function looks like this now:

        private void AddContextMenu()
        {
            song_context_play.Text = "Play";
            song_context_play.Click += new System.EventHandler(song_context_play_Click);
            song_context_delete.Text = "Delete";
            song_context_delete.Click += new System.EventHandler(song_context_delete_Click);

            ContextMenuStrip song_context = new ContextMenuStrip();
            song_context.Items.Add(song_context_play);
            song_context.Items.Add(song_context_delete);

            foreach (DataGridViewRow row in song_grid.Rows)
            {
                row.ContextMenuStrip = song_context;
            }
        }


This still hasn't fixed my problem. I am noticing a strange pattern now though. Each time I click the ContextMenuStripItem which fires the event, the number of times the event hander is called for that click doubles. (first click: 1x, second click: 2x, third click: 4x, fourth click: 8x, etc);
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3649
  • View blog
  • Posts: 11,416
  • Joined: 05-May 12

Re: ContextMenuStrip ToolStripMenuItem.Click Fires multiple times?

Posted 08 May 2012 - 04:38 PM

If you set a breakpoint on your AddContextMenu() method, how many times does it get called?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1