1

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;
}
4
  • Ok if you created a button and double click that button it would bring you to code view so you can write in some code for what you want to happen when the button is clicked. In a different place in my project I'm trying to code "If that button is clicked" So in my if(statement above) I try to say if user clicks that button..do the following. Idk how to write the if(statement). I'm sorry i just don't know how else to ask. Commented Sep 29, 2011 at 23:35
  • Are you saying you just want to call AddNewCamper.btnNewSubmit_Click_1() directly, is that all? Commented Sep 29, 2011 at 23:38
  • Why not put your code in the AddNewCamper.btnNewSubmit_Click_1() event handler in AddNewCamper? Commented Sep 29, 2011 at 23:39
  • b/c it's to complicated to try and call everything in this eventhandler. In other words the listbox is on a page. that page is hosted on a frame that is on a tabcontrol. Then whatever is submitted by the button, it needs to take whats in that textbox and pass it onto another class. So i would access that with a get() method. Its just to much to call and is messy. Plus I tried it and I couldn't get anything to display on the listbox. Commented Sep 29, 2011 at 23:46

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hey thanks for showing me this. I believe I am close but when I hit submit the program blows up. lol It starts debugging and immediately points to what u have in the btnNewSubmit_Click1() method. I'm not sure why
Object reference not set to an instance of an object.
Have you pass the _parentForm properly when creating AddNewCamper instance? Check whether you have set that? Check what is Null?
btw what is "this" in AddNewCamper camp = new AddNewCamper(this); ?
this is the ParentForm. You have to send the instance of the calling form to the childForm. And then set that to the _parentForm (I am not sure what's your parentForm type"
|
0

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.

Comments

0

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);

2 Comments

yes this is what i was trying to do earlier. But I'm confused what the owner variable is?
It is a variable (type object) in the base class Form that your ParentForm is inherited from. Because it's an object type variable, you need to cast it back to a ParentForm type in order to use it. When you show the other form with camp.Show(this), the "this" is passing a reference to your ParentForm which is stored in the variable owner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.