Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 28, 2009 9:11 PM | 1 vote
Is it possible to have a click event on a particular control handled by an event handler method in a different class? I have a checkbox in a main form, and I would like to have it's "Click" event handled by a method defined in another class (windows form) in my program. Can I set the handler to be direct to that class method at all?
All replies (4)
Tuesday, July 28, 2009 10:10 PM âś…Answered | 2 votes
Handle the checkbox event locally on the main form. Create a public method on the popup. Hold a reference to the popup in the main form. If the popup isn't null and isn't disposed, call the method from within the checked event.
MainForm:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class MainForm : Form
{
private PopupForm _popupForm;
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_popupForm = new PopupForm();
_popupForm.Closed += _popupForm_Closed;
_popupForm.Show();
}
private void _popupForm_Closed(object sender, EventArgs e)
{
_popupForm = null;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (_popupForm != null)
_popupForm.DoSomething();
}
}
}
PopupForm:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class PopupForm : Form
{
public PopupForm()
{
InitializeComponent();
}
internal void DoSomething()
{
// something
}
}
}
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
Tuesday, July 28, 2009 9:15 PM
Technically, yes, it can be done. Simply add an event handler to another class, and then use the "+=" operator to attach it to the event.
Design-wise, should it be done, however? The answer is maybe, and probably not. It completely depends on what you're trying to accomplish. Should you add a click event handler within a business object and have the business object react to the click? Probably not. The business object's purpose is not to interact with the UI, but to hold properties and methods that are consistent with the business domain. (You should be able to switch the UI and leave the business objects untouched).
In some other patterns, it might make sense to do this, but you might want to check your architecture before you make any final changes. :)
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
Tuesday, July 28, 2009 9:28 PM
I see what you mean.... perhaps not the best way to implement my solution. My situation is this: I have a main form, which opens a popup form. Then, focus is directed back to the main form where there is a checkbox. When this checkbox is clicked, I would like to fire an event handler method in the popup form (the reason the event handler is defined in the popup form is because there is a checkbox in that form as well, which is supposed to do the exact same thing as the checkbox in the main form).
What's your best-practice suggestion for this?
Tuesday, July 28, 2009 10:12 PM
Alright, that's what I thought I might be needing to do. I was trying to take the easy way and save some of the typing required, etc. I'll give that a shot, thx.