3

I'm having one ContextMenuStrip in that strip at runtime I'm adding one ToolStripMenuItem. And I added this ContextMenuStrip in the XtraGridView's MouseDown() event handler. And at the same time I've added the event handler for the newly inserted ToolStripMenuItem. And I have written one Event handler function for that ToolStripMenuItem. My problem of application is that when user right clicks on the XtraGridView it shows the required menu which I have added at runtime. And when I click on newly added ToolStripMenuItem it executes required event handler function but when I again do the same procedure the event handler function is executed for two times and so on...

Can anyone solve this problem?

Thanks.

Dulini Atapattu
  • 2,735
  • 8
  • 33
  • 47
Priyanka
  • 2,802
  • 14
  • 55
  • 88
  • At which point in code are you assigning event handler for ToolStripMenuItem? I think that you are assigning it multiple times, that's why the handler is executing multiple times. – Vale May 05 '11 at 09:13
  • @Val: I'm adding event handler at xtragridview's mouse down event. And while adding i've checked some condition. I don't think so. Because i checked code by placing breakpoint. I've solved my problem by removing handler of the same event which i've added at run time. But i don't think that is it because of adding the event handler more than one time. Isn't it? – Priyanka May 05 '11 at 09:50
  • That is because you added it in datagridview mouse down event and every time user presses mouse button new handler is added. That is why it fires multiple times. You should add it on load, or some other place. – Vale May 05 '11 at 09:56
  • Can you write down the events that you have applied to display the `contextmenustrip` – Developer May 05 '11 at 11:15

1 Answers1

4

You probably are subscribing to the Click event, in XtraGridView's click event. Each time the GridView's click event is raised causes you to subscribe to the click event handler again, so when user actually clicks on the ToolStripMenuItem all the handlers are called.

I suggest moving the subscription code to somewhere else.

Hadi Eskandari
  • 25,575
  • 8
  • 51
  • 65