KansaiRobot
KansaiRobot

Reputation: 9912

What is the most elegant way to call an event of another Form?

Let's say I have a Form called Form1 which will somehow (how is not relevant) calls another form Form2

(Form1)

Form2 f2= new Form2();
f2.ShowDialog();

This Form2 has a button that will do some operation everytime the user clicks on this button.

However, I want that the first time i.e. when Form2 is just shown, the code in the button (some operation) gets executed.

In other words I have to be able to call the code in Form2's button_Click which is private.

Now I can think of some ways to make this possible(making the click event public etc) , but my question is what is the most elegant (or correct) way to do this?

Upvotes: -1

Views: 413

Answers (3)

pigeonhands
pigeonhands

Reputation: 3414

Move the code from the OnClick event into its own method (e.g. "DoWork"), then call that method from the OnClick event.

Either call it when you create the form

var frm = new demoForm();
frm.DoWork();
frm.Show();

Or call it in the forms constructor.

public partial class demoForm : Form {
        public demoForm() {
            InitializeComponent();
            DoWork();
        }

        private void button1_Click(object sender, EventArgs e) {
            DoWork();
        }
        public void DoWork() {
            //Code here
        }
    }

Upvotes: 2

Phil1970
Phil1970

Reputation: 2623

I would add a property to Form2 to tell the form I like to automatically executed an action.

class Form2
{
    public bool AutoExecuteSomeOperation { get; set; }
}

In Form1, you would set that property and in Form2 you would check and execute appropriate code if the property was set.

I would recommend that you refactor button_Click to call another method which you can also call for automatic execution. I like to keep event handler simple and executed only for the event on the control that served to name the event handler. Thus, you know that button_Click is an handler for a Click event on a control named button. It makes the code easier to maintain.

You can decide if you want to reset the property once the code is executed or you can add some validation that the property changes are valid. For exemple, you might want to ensure that the property is called before displaying the form.

In all cases, you should avoid having any reference to a control from an external form. Only Form1 itself should know that it contains a button. Any use from outside world should be done through a public property or public event of the form. That way, if you decide that the button should be replaced by an hyperlink, a menu item, a checkbox or anything else Form1 does not need to be updated. This is very similar to what should be done for UserControl. The less internal details leak, the easier it will be to make internal changes without having to update all caller.

Upvotes: 2

TheGeneral
TheGeneral

Reputation: 81493

The easiest approach is just make it public, however its not the bastion of great design.

Decoupled messaging is probably where you want to be, event aggregator or any pub sub method messaging system. This is a more modern and scalable approach, the participants need not know about each other allowing you to make the methods private and giving you a more maintainable decoupled solution, and keep your classes self consistent.

Unity, MvvmLight both have these sorts of messaging systems, however there are lots of them.

Example of how this might work

public Form1()
{
    InitializeComponent();
    EventPublisher.Instance.Subscribe<NewUserCreated>
        (n => listBoxUsers.Items.Add(n.User.Name));
}

...

// some other class
private void Form2()
{

    var user = new User()
               {
                   Name = textBoxUserName.Text,
                   Password = textBoxPassword.Text,
                   Email = textBoxEmail.Text
               };
    EventPublisher.Instance.Publish(new NewUserRequested(user));
}

Upvotes: 2

Related Questions