Two Questions (yes I have googled it)

1) Is it possible to create a custom event Like when something happens, ex. when textbox1.text equals Dumbo, raise an event, with the ability for an event handler that will handle that event

2) In Visual Basic, you can use handles button1.click, button2.click etc. etc. to handle multiple events with one handler, what's the C# equivalent



Answer this question

Two Questions (yes I have googled it)

  • Bizzdreamer

    What about the 2nd question


  • George Waters

    1) yes you can. Example, delcare an event and delegate at the top of the form:

     

    public delegate MyEvent TheEventDelegate();

    public event TheEventDelegate OnEventSomething;

     

    //create a handler:

    this.OnEventSomething += new MyEvent(Form1_OnEventSomething); //Example, we are creating an event in Form1, after pressing += it will automatically populate the right handside

     

    void Form1_OnEventSomething()

    {

       //this is made automatically - this is how you create an event handler

    }

     

    //to raise the event:

     

    if (this.OnEventSomething != null)

    {

       OnEventSomething(); //will raise the event

    }

     

    http://msdn2.microsoft.com/en-us/library/awbftdfh.aspx

    Hope this helps, the link above has further in depth explaination



  • jeffrycalhoun

    it would be the sameish - meaning when you do the += and it automatically comes up with the proper event declaration, just retype to a different already (same type delegate) event. example, textbox TextChanged event:

    2 textboxes, 1 TextChangedEvent:

    this.theTextBox1.TextChanged += new EventHandler (theTextBox1_TextChanged);

    this.theTextBox2.TextChanged += new EventHandler (theTextBox1_TextChanged);

    void theTextBox1_TextChanged(object sender, EventArgs e)

    {

    //do stuff

    }

    Whenever the 2nd textbox text changes, it will raise the textbox1 textchanged event (should do)

    does this help really you should be using the eventhandlers for a specific event, not "redirecting" them to another.



  • julial77

    I already know that. But I need to create the event first...


  • TuanTuan

    ahmedilyas gave the code view method. From the designer, select the control. Go to the properties panel, and select the Events icon. From the list of events, you can double-click the event name for VS to automatically create a stub event handler -or- you can click the [...] button, and it will list the methods with the proper signature for you to select from.

  • Two Questions (yes I have googled it)