14 Replies - 817 Views - Last Post: 02 June 2011 - 10:12 PM Rate Topic: -----

#1 R3cuRsioN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 19-April 10

Making a control subscribe to another control's event

Posted 01 June 2011 - 02:20 AM

Say I have two controls - a button and textbox. Would it be possible to have the textbox control subscribe to the button control's click event? For instance, if I have a button on a form, I can define an event handling function in the form class and register that function to my button's click event like this:

 this.button1.Click += new System.EventHandler(this.button1_Click);  


(the above code is normally auto generated by visual studio in the form.designer.cs file)

So I want to do this same sort of thing but I want the textbox control itself to register an event handling method with the button's click event.

I have a feeling that this can only be achieved by creating a custom control that extends textbox. Is this correct?

Is This A Good Question/Topic? 0
  • +

Replies To: Making a control subscribe to another control's event

#2 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 02:50 AM

As far a I know click only uses System.EventArgs.

so if you have an event:
	
private void EventName_Click(object sender, EventArgs e)
{
    //Your code
}



you can then subscribe to that event from your textbox and button.

this.button1.Click += new System.EventHandler(this.EventName_Click);
this.textbox.Click += new System.EventHandler(this.EventName_Click);



now they both fire the same event.

Edit: if you're using visual studio you can also use the IDE to set the click events.

This post has been edited by Servatis: 01 June 2011 - 02:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5520
  • View blog
  • Posts: 11,829
  • Joined: 02-June 10

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 05:41 AM

*
POPULAR

Servatis is right, they can both subscribe to the same event handler.

It's easy enough to do in the designer. Look at the properties pallet. See that yellow lightening bolt? That's the events.

So all you have to do is set the handler for the button.
Then point the textbox to the same handler.

Attached Image

Attached Image

But I have to suggest you stop and think about whether that really makes sense.
I can't imagine a situation where you would have a button and a textbox both on the same handler. First of all, think about if you want the action to take place as soon as someone clicks IN a textbox so they can input text. Will that method work when the textbox is blank?

What is the method actually for, what is it meant to do, when either a button or a textbox is clicked?

It's more common to have several of the same type controls go to the same handler. For example, a group of checkboxes. When any of 7 checkboxes for Mon, Tue, Wed, Thu, Fri, Sat, Sun are CheckChanged then the same method is called. It becomes obvious they are related and all belong to DayOfWeek_CheckChanged(object sender, eventargs e)

Secondly, as a rule I try to avoid having work actually take place in the event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.

btnSave(object sender, eventargs e)
{
    SavePreferences();
}

SaveMenuItem(object sender, eventargs e)
{
    SavePreferences();
}

SaveContextMenu(object sender, eventargs e)
{
    SavePreferences();
}

Form1_Closing(object sender, eventargs e)
{
    if (IsDirty) SavePreferences();
}


Here you see how a button, a menu option, a context menu option and a programmatic response to the form closing while unsaved preferences exist, can all call the SavePreferences() method.

This is a more readable and thus more manageable way to code than to put the actual work in the GUI control's handler.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Bulding an application - Part 1
Building an application - Part 2

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

Was This Post Helpful? 5
  • +
  • -

#4 R3cuRsioN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 19-April 10

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 07:53 PM

I think you guys have misunderstood my question.

i don't want the textbox and the button to use the same event handler, I want the textbox control to respond to the button controls click event by calling an event handler that it has registered with the button's click event.

I understand that this isn't at all practical, it's for learning purposes only.
Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 08:37 PM

So when you click the button its event also fires the textbox's event?

This post has been edited by Nakor: 01 June 2011 - 08:38 PM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4489
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 09:00 PM

One control (or object) can't trigger the event of another.. Think of an event as a list that holds methods. If what you were trying did work (and it can't, btw), you'd actually be assigning all the subscribers from one event to the other, not triggering one event from another. There's no syntax that allows you to call that event. That's the opposite of how events work.

No, the best way to handle something like this is to create a common method that both use, and call it from each handler. That's how you can share functionality. But you can't trigger the event of another control.
Was This Post Helpful? 0
  • +
  • -

#7 R3cuRsioN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 19-April 10

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 09:28 PM

View PostNakor, on 01 June 2011 - 08:37 PM, said:

So when you click the button its event also fires the textbox's event?


No. When I click the button an event handler that belongs to the textbox (and was registered with the buttons click event) gets called. In much the same way that my form class has an event handler (called button1_Click, or w/e) that handles the button's click event.

Perhaps it will be clearer if I explain it in a different manner:

If I want my form to respond to the button's click event, i have to write a method in the form class to handle the event and then associate that method with the button's click event.

Now, I also want my textbox to respond to the button's click event (in the same way my form does - i.e. write event handler, associate event handler with click event).

But since this would require adding a method to the textbox class (the method that I want to handle the buttons click event), the only way it would be possible is by extending the functionality of textbox class.

View PostCurtis Rutland, on 01 June 2011 - 09:00 PM, said:

One control (or object) can't trigger the event of another.. Think of an event as a list that holds methods. If what you were trying did work (and it can't, btw), you'd actually be assigning all the subscribers from one event to the other, not triggering one event from another. There's no syntax that allows you to call that event. That's the opposite of how events work.

No, the best way to handle something like this is to create a common method that both use, and call it from each handler. That's how you can share functionality. But you can't trigger the event of another control.


I don't want the textbox to trigger the button's click event, I want the textbox to respond to the button's click event.

This post has been edited by R3cuRsioN: 01 June 2011 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4489
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 09:40 PM

I guess I don't understand, then. Maybe if you described what you actually want to happen, rather than using generic terms. Like, do you want to change the text on click, or something like that?

If I do understand what you want to do, the only way is to allow the form itself to handle the interaction logic, since the form is the thing the two have in common. So if you wanted to do something to (or with, i guess) the text box when the button is clicked, you make a method that does what you want, then call that method from the button click event handler.

Other than that, I can't figure out what you're trying to do.

I think you have a misconception of what an event handler actually is. All it is is a method, nothing more. The event is a list that holds methods, and all are called in sequence when the event triggers. The event handler doesn't belong to the button at all. It belongs to the form. The event belongs to the button.
Was This Post Helpful? 0
  • +
  • -

#9 R3cuRsioN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 19-April 10

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 10:59 PM

View PostCurtis Rutland, on 01 June 2011 - 09:40 PM, said:

I think you have a misconception of what an event handler actually is. All it is is a method, nothing more. The event is a list that holds methods, and all are called in sequence when the event triggers. The event handler doesn't belong to the button at all. It belongs to the form. The event belongs to the button.


I understand this. In the form class, I create an event handling method that is called when the button raises it's click event. The event handling method is defined in the form class (and obv belongs to the form class). After I've defined the method, i need to tell the button that my form is interested in it's click event. So i tell the button to add the event handling method i've defined in my form class to it's list of click event handlers:

this.button1.Click += new System.EventHandler(this.button1_Click);


Okay, done. Now whenever the button fires it's click event, the event handler defined in my form will be called.

Now, I also want my textbox control to be interested in the button's click event. So I would need to create an event handling method that belongs to the textbox control, and then add that method to the button's list of click event handlers (just like i did for the form).

But since i didn't write the code for the textbox class, i can't define additional methods for it. So I will need to create a custom textbox class that extends the functionality of the default textbox class. Then i can add all the additional methods that i want to it.

Hopefully that's a bit clearer :)
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4489
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 11:16 PM

That, in my opinion, is completely unnecessary. You don't need to make the handler part of the textbox. The text box and the button are both controls on the form. Put the handler on the form, and have the form manipulate the textbox. That's how it's done.

If you're looking to make this something you can reuse, make a user control, and put the interaction logic in the user control.

Technically, you could make a new class, like MyTextBox, and extend TextBox That'll allow you to effectively add code to a text box. But then you'd need to add a reference to the new text box class to the button, and then subscribe in it's code to the button's event. Unless you actually need access to the internal or protected members of TextBox, that's not necessary.

Basically, it's all about relationships. The textbox belongs to the form. The button belongs to the form. The textbox and the button don't have any other direct relationship between them. So you put the logic where they share common ground. The button tells the form "I've been clicked.' The form says, "Ok, now I'll do some stuff to the text box."

This post has been edited by Curtis Rutland: 01 June 2011 - 11:17 PM

Was This Post Helpful? 2
  • +
  • -

#11 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Making a control subscribe to another control's event

Posted 01 June 2011 - 11:43 PM

Is this what you are trying do?


            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.button1.Click += new System.EventHandler(this.textbox1_button1_Click);




        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button 1 click Event");
           
        }

        private void textbox1_button1_Click(object sender, EventArgs e)
        {            
            this.textBox1.Text = "Button1 Clicked";
        }




DisClaimer:
Please do not use this code in any real world application. It is posted for an example.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5520
  • View blog
  • Posts: 11,829
  • Joined: 02-June 10

Re: Making a control subscribe to another control's event

Posted 02 June 2011 - 06:34 AM

Quote

i don't want the textbox and the button to use the same event handler, I want the textbox control to respond to the button controls click event by calling an event handler that it has registered with the button's click event.


I don't want scrambled eggs.
I want to have eggs that are mixed in a violent fashion making them all scrambled up, then cooked.


Dude: What is it you are trying to achieve? NOT how do you want to get there? NOT what do you think is the best way to do it. Because the *how* of what you are trying is just all flavors of fraking goofy. Several times now someone has said "I can't figure out what you're trying to do". That should tell you something.

What is the intended end goal you are trying to achieve so we can teach you a better way to do it?
Was This Post Helpful? 1
  • +
  • -

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Making a control subscribe to another control's event

Posted 02 June 2011 - 07:00 AM

You can call an event just like any other method.

private void Button1_Click(object sender, EventArgs e)
{
    // do something
    TextBox1_Click(null, null);

    // or you could pass actual values for the sender and event args.
}

private void TextBox1_Click(object sender, EventArgs e)
{
    // do something
}



This means that when the button is clicked, it will also execute the click event for the TextBox.

Is that what you mean?
Was This Post Helpful? 0
  • +
  • -

#14 R3cuRsioN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 19-April 10

Re: Making a control subscribe to another control's event

Posted 02 June 2011 - 09:19 PM

View PostCurtis Rutland, on 01 June 2011 - 11:16 PM, said:

That, in my opinion, is completely unnecessary.


Indeed.

But like i said, this is only for learning purposes. Doing something with my textbox in response to the button's click event is trivial. Like you said, the handler doesn't need to be part of the textbox itself. I just wanted to know if it was possible.

Anyway, I really appreciate your replys :)
Was This Post Helpful? 0
  • +
  • -

#15 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Making a control subscribe to another control's event

Posted 02 June 2011 - 10:12 PM

If you have not studied delegates remember this when you get to them because if you start peeping under the hood of events understanding delegates is very important.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1