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.
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.
Related
Is there a way to add a Click Event Listener to any Button?
I want to fire a method when any button is clicked from any form in the solution.
I know i can add an event listener for each button on its own but it will be quite redundant in my case.
Thanks
i'm just wondering what the difference is between Control.OnClick and Control.InvokeOnClick methods.
the second thing i wonder ise, when it is asp.net they talk about 3 componenets: Click event, OnClick method, and button1_click eventhandler. but when it is desktop appcliatons they talk about only 2 componenets, Click event and its eventhandler. why is the onclik(invokeonclick) not mentioned at all?
OnClick allows you to add a handler for when the Click event occurs on the control.
InvokeOnClick will "Raise the Click event for the specified control".
So if you have assigned a handler to OnClick and then call InvokeOnClick on that control then your handler will be called.
Web is different. This answer may help you for invoking click on a 'web' button.
I have two different pages in my Windows Phone 8.1 Store application, say Page_1 and Page_2. I have two different event handlers for the Phone's Back Press event on the two pages. Strangely when I'm on Page_2 and I press the Back button, the event handler on Page_1 is getting invoked. Can someone help me out with this issue? I want the event handler of the page in which I am to be invoked when I press the back button from that page.
If you navigate from Page_1 you should remove the event handler which you added. And on Page_2 simply add the handler for that page.
you have add back pressed event in page2 onNavigateTo method add conditions you want to execute on that page and remove back pressed event in onNavigateFrom .
Sometimes when you accidently double click on events it generates the events event method and add and event handler in the InitializeComponent() of the form.
So when I want to delete the event, I have to delete code in two places, the event method it self and the event handler in to InitializeComponent(). But is there away to do this without deleting code manually?
If you have have not edited the event handler body, you can simple remove the event using VS Designer. For this select Events in Properties window and remove the handler that you don't need.
However, if you have edited the event handler (i.e. the body contains some code or comments). You can still remove the event using above step. But from the cs file you have to manually delete the method. This helps to refrain from designer generated code (and possibility of messing it up).
I am adding event handlers to a button like this:
btn.Click += new EventHandler(btn_Click);
However the btn_Click function is not being called (never hits the breakpoint in it) and the button just reloads the page. In my past experience, asp buttons usually perform the click code before reloading the page, so how do I get that to happen when the event is dynamically added?
I also set CausesValidation = false, although there's no validation on the page so I don't think that would have influence anyway.
The event handler needs to be bound for every request regardless of whether or not the page is being posted back. The binding of the event handler is lost at the start of each page request. Event handlers for buttons are typically bound in Page_Load.
You have to set event handlers on Load event (or before). If you do it after Load, it won't be executed since by the time the handler for the event is evaluated it won't be there.
Check this msdn article in relation to page life cycle. I think it will help you to understand. See that event handling occurs inmediatly after Load