event questions

how can i start an event from other event or other function also how can i cancel the events or stoping it from operation
i mean if i'm already creating a timer event and now i'm in a bottun click event , how can i start the timer event from my button event then cancel the timer event from other event or other function


Answer this question

event questions

  • TA123

    to raise Tick Event of Timer write this behind Button's Click event:

    timer1.Tick(timer1, EventArgs.Empty);

    To Stop it you can do many things:

    timer1.Stop();

    timer1.Enabled = false;

    timer1.Tick -= new EventHandler(timer1_Tick);

    Best Regards,



  • Tb2006

    thanks
    but i'm not talking about a timer specificly , i'm talking about the general events like mouse button clicking
    what i want is when a messagebox returns a retry key i want tostart a button click event handler

    buttom1_clicked()
    {
    ..
    ...
    if(messagebox.show(......,messageboxbuttons.retrycancel,..)==dialogresult.retry)
    //start this event again or other event
    }

  • syhzaidi

    you can very well call the event handler because event handler are just a function. But you cant raise the event, the difference is if the event is raised it is not the only function getting called when the event is raised but whoever the client registered for the event will also be called. So you cant say just calling the event handler function is similar to raising the event.

  • slimcode

    You don't have to activate timer event. Just call the timer tick handler method that is executed on timer tick from button click event. About stopping the timer event there are many solutions. First to stop the timer, then to have a variable that will be checked in timer tick event handler method and you will decide based on that variable does the code in that handler will be executed. And also you can detach the hanlder from timer tick event and attach it again when needed.

  • dydoria

    Ahmed, You can simplye add event handler to the event you want to raise put appropriate code in that and now you can call that handler passing values to it. See my example in last post its not specific to Timer, using that way you can call any event of any control passing the appropriate parameter matching specifically that event's delegate!

    Like you have a button with a click event handler like this:

    private void button1_Click(object sender, EventArgs e)

    {

    MessageBox.Show("Hello World!")

    }

    This will be called when u click button1 with mouse but this can also be invoked using!

    button1.Click(button1, EventArgs.Empty); // Parameter should always match to the delegate of the event

    I hope this is clear enough!

    Best Regards,

    Rizwan



  • erikkl2000

    well hopefully, if the timer is declared globally (where any method/anywhere in the class can access this object) then you can start() the timer from a button or Stop() the timer from a button. Example:

    //button start:

    this.theTimerObject.Start(); //replace the object name correctly with the object name you are using for the timer

    //button stop:

    this.theTimerObject.Stop(); //same as above in comments

    does this help Fortunately these methods will do just that, start and stop the timer for us.



  • BilalShouman

    do u want raise an event inside an event hadler

    if so I dont think it is possible unless the event is defined in your code.

    If you know the method which raises the event(Invokes the corresponding delegate) and the method is public you make call the method and make event is raised along with the function execution.



  • event questions