views:

59

answers:

6

hi call event from form2 in form1?

for example :

The following code into form2 :

    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show("http://stackoverflow.com");
    }

What to write in a form1?

A: 

how about form2.Form2_Load(this, null)

Vivek
It's a private method. He can't call it from outside the class.
Oded
oops... my mistake...
Vivek
+2  A: 

Why are you wanting to call the event? Will you know the sender and the Event Args?

Why don't you just create a public method in Form2 that Form1 is able to see?

Miles
A: 

You can't call private members of a class from outside it.

You can change the accessibility to internal, which will make it visible within the assembly - if your form1 is in the same assembly.

Alternatively you can make it a public method, which would make it globally accessible.

However, you shouldn't call event handlers in such a manner - they are supposed to handle events that the declaring class raises.

For the sample code you gave, a better solution would be to create a public or internal method that can be called from this event handler:

private void Form2_Load(object sender, EventArgs e)
{
    MyMethod();
}

public MyMethod()
{
    MessageBox.Show("http://stackoverflow.com");
}

In order to call this method from form1, it needs to know about form2:

 // in form1
 Form frm2 = new Form2();
 frm2.MyMethod();
Oded
This is a principled way ?
ehsan_d18
What do you mean by "principled way"?
Oded
A: 

You can't raise an Event from outside a class. The convention is that you call a OnEventname method in the class. Usually this method is protected (can't only accessed from the class itself or others that inherit from it)

// in form1
private void Method1()
{
    using (var form2 = new Form2())
    {
        form2.Show();

        form2.RaiseLoadEvent(EventArgs.Empty);
    }
}

// Create this method in form2
public void RaiseLoadEvent(EventArgs e)
{
    OnLoad(this, e);
}


// The OnLoad method already exists in form 2 
// But it usually looks like this
protected void OnLoad(EventArgs e)
{
    var eh = LoadEventHandler;
    if (eh != null) 
    {
       eh(this, e); 
    }
}

But I don't suggest to raise the LoadEvent, because It is raised only once after the creation of the form. More usual is to react to the Load event to modify the form.

privat void Method1()
{
    using (var form2 = new Form2())
    {
        // Add Event Handler
        form2.Load += new EventHandler(form2_Load);
        form2.ShowDialog();
    }
    // Allways remove Event Handler to avoid memory leaks
    form2.Load -= new EventHandler(form2_Load);
} 

private void form2_Load(object sender, EventArgs e)
{
    form2.Text = "Hello from form1";
}
SchlaWiener
A: 

Form1 (the event publisher) should expose a separate, public event property for Form2 (the subscriber) to subscribe to.

For example: the form publishing the event will look like this:

public partial class Publisher : Form
{
    public event PostUpdateHandler OnPostUpdate;

    public Publisher()
    {
        InitializeComponent();

        new Subscriber(this).Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (OnPostUpdate != null)
        {
            OnPostUpdate(new PostUpdateArgs(textBox1.Text));
        }
    }
}

public delegate void PostUpdateHandler(PostUpdateArgs args);
public class PostUpdateArgs : EventArgs
{
    public string UpdateText;

    public PostUpdateArgs(string s)
    {
        UpdateText = s;
    }
}

The subscribing form looks like this:

public partial class Subscriber : Form { public Subscriber() { InitializeComponent(); }

    public Subscriber(Publisher publisher) : this()
    {
        publisher.OnPostUpdate += new PostUpdateHandler(publisher_OnPostUpdate);
    }

    private void publisher_OnPostUpdate(PostUpdateArgs args)
    {
        this.Form2_Load(null, null);
    }

    private void Subscriber_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Dispose();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show("http://stackoverflow.com");
    }
}

When the user presses button1 on the publishing form, the subscribing form will execute the code associated with the delegate, resulting in a message box popping up with the message http://stackoverflow.com.

code4life
A: 

I'm a fill datagridview When it opens the second form the value displayed will not

Why?

My code:

        // in form1
    private bool Method1()
    {
        using (var form2 = new frmMain())
        {
            form2.RaiseLoadEvent(EventArgs.Empty);

        }
        return (true);
    }

    private void frm1_Load(object sender, EventArgs e)
    {

           Method1();

        this.DialogResult = DialogResult.Cancel;

    }

// in form2

        public void RaiseLoadEvent(EventArgs e)
    {
        this.OnLoad(e);
    }

    private void OnLoad(EventArgs e)
    {
        this.con = new OleDbConnection(this.ConnectionString());

        string strquery = "select * from english";

        this.dba = new OleDbDataAdapter(strquery, con);

        this.dba.Fill(this.ds);

        this.dataGridView1.DataSource = this.ds.Tables[0].DefaultView;
    }

code in Program.cs :

 /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        frm1 frmsp = new frm1();

        if (frmsp.ShowDialog() == DialogResult.Cancel)
        {
            Application.Run(new frmMain());
        }

    }
ehsan_d18
If you have another question, ask another question. This is not an answer to your own question, so shouldn't be posted as an answer. Please post this as a new question and delete it as an answer.
Oded