TMan
TMan

Reputation: 4122

calling a buttons click Event method

I have a form (AddNewCamper), which contains a textbox and a submit button. In a different form, I'm trying to write:

if (submit button is clicked)
   do stuff

In the window that the button is actually in, I have a click event created. So I guess I'm trying to call that click event inside the if statement (which is in a different window from where the click event is located).

Here's what I have:

AddNewCamper camp = new AddNewCamper();
camp.Show();

// This is where I'm confused. How do I say if this button is clicked,
// or how do i call its click event that's located in AddNewCamper?
if (camp.btnNewSubmit_Click_1())
{
    Camper person = new Camper(camp.txtNewFirstName.Text);
    camp.txtNewFirstName.Text = person.getName();
    c.testListBox.Items.Add(person.getName());
    campersFrame.Content = c;
}

Upvotes: 1

Views: 2297

Answers (3)

Lee O.
Lee O.

Reputation: 3322

When you have the "camp" form show, pass a reference to the parent form like this:

camp.Show(this);

Then, when someone clicks on the submit button of the "camp" form, you can reference the parent form using the owner variable to do the things you want on that form like this:

((ParentForm)owner).SomeMethod(parametersToPassToParentForm);

Upvotes: 0

CharithJ
CharithJ

Reputation: 47570

As I understand your question, seems you want to display some content in the parent form when the submit button click on the AddNewCamper Form. Below is one way that you can do that.

Add a public method to the ParentForm to Show(or refresh) the content once Submit clicked from the AddNewCamper.

In the ParentForm

public RefreshCamper(string firstName)
{
    Camper person = new Camper(camp.txtNewFirstName.Text);
    camp.txtNewFirstName.Text = person.getName();
    c.testListBox.Items.Add(person.getName());
    campersFrame.Content = c;
    // ETC...
}

Pass the ParentForm instance to the AddNewCamper Form in the contructor.

private ParentForm _parentForm;

public AddNewCamper(ParentForm parentForm)
{
    _parentForm = parentForm;
}

private void btnNewSubmit_Click_1()
{
    _parentForm.RefreshCamper(txtNewFirstName.Text);
}

Create an AddNewCamper instance as below from the ParentForm.

 AddNewCamper camp = new AddNewCamper(this);
 camp.Show(); // Or ShowDialog if you want Model..

Or you can set a flag in the ParentForm in the same way to identify that the Submit button is clicked.

Upvotes: 1

T. Kiley
T. Kiley

Reputation: 2802

Go to the event handler of the button (in the Form view of visual studio, in the properties grid find the click event and double click on the box next to it, this will take you to it [or create it if it hasn't been created yet]) From here, you need to call the method you want to do when the user presses the button.

Looking at the code you have provided, I assume what you want to do is wait at the if statement until the user has pressed the button. Unfortunately, unless this code is on a separate thread, if you were to wait for the user to press the button, the program would hang. Instead, you have to work out what you want to happen when the user presses the button, put this in a method and get the button event handler to call that method.

Upvotes: 0

Related Questions