Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
.Net CF 3.5

I have a form with several buttons on it, all are leading(if clicked) to form 2, in form 2 i need to find out witch button i clicked in the first form and to be able to use that event.

I tried to call the form.btn1_Click event from second form, but i get this :

An object reference is required for the non-static field, method, or property

I put all the forms and all the buttons events on public.

How do i do it.
Posted
Updated 26-Jun-10 8:57am
v2

1 solution

First of all, remove all the public stuff (event handlers and controls) that you've done as that is not the solution to your problem.

Why do you need to know which button was pressed? I can't imagine a scenario where that would be needed.

If you need a Button to Click in Form due to something happening in Form2 then Form2 should raise an event that Form subscribes to, and in the handler for that event it should either call the handler for the Button's Click or call the Button's PerformClick() method.
C#
// Form2.cs
public event EventHandler ReclickRequest;

private void WhateverItIsThatCausesThis()
{
    OnReclickRequest(EventArgs.Empty);
}

protected virtual void OnReclickRequest(EventArgs e)
{
    EventHandler eh = ReclickRequest;
    if(eh != null)
        eh(this, e);
}

C#
// Form1.cs
// In method that instanciates Form2
    Form2 form2 = new Form2();
    form2.ReclickRequest = form2_ReclickRequest;
//

    private void form2_ReclickRequest(object sender, EventArgs e)
    {
        // Call the button's Click event handler or it's PerformClick method
    }


See here[
 
Share this answer
 
Comments
Daniel262 26-Jun-10 15:41pm    
well, it is actually something like this :
form 2 is using the information from form 1 to do something in a form 3;

it like this, buttons Daniel, joe, Dave(form1)
filters(telephone, address)(form2)
results form3
DaveyM69 26-Jun-10 17:20pm    
Form2 should have a property (or properties) that can be set after instanciation from form1 (but before being shown) and then either...
form2 creates form3 and does the same
or
form2 raises an event and in the handler form1 creates & does it's thing with form3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900