简体   繁体   English

调用按钮单击事件方法

[英]calling a buttons click Event method

I have a form (AddNewCamper), which contains a textbox and a submit button. 我有一个窗体(AddNewCamper),其中包含一个文本框和一个提交按钮。 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. 在按钮实际所在的窗口中,创建了一个click事件。 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). 所以我想我正在尝试在if语句(位于与click事件所在位置不同的窗口)内调用click事件。

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

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. 据我了解您的问题,似乎您想在提交按钮单击AddNewCamper表单时在父表单中显示一些内容。 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. 一旦从AddNewCamper单击“提交”,就将公共方法添加到ParentForm中以显示(或刷新)内容。

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. 将ParentForm实例传递给构造器中的AddNewCamper表单。

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. 从ParentForm如下创建一个AddNewCamper实例。

 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. 或者,您也可以以相同的方式在ParentForm中设置一个标志,以标识单击了Submit按钮。

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. 转到按钮的事件处理程序(在Visual Studio的“窗体”视图中,在属性网格中找到click事件,然后双击它旁边的框,这将带您到该事件[或如果尚未创建,则创建它已创建)]从此处,您需要在用户按下按钮时调用要执行的方法。

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. 查看您提供的代码,我假设您想要做的就是等待if语句,直到用户按下按钮为止。 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. 相反,您必须弄清楚用户按下按钮时要发生的情况,将其放入方法中并获取按钮事件处理程序以调用该方法。

When you have the "camp" form show, pass a reference to the parent form like this: 在显示“ camp”表单时,将对父表单的引用传递如下:

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: 然后,当有人单击“ camp”表单的“提交”按钮时,可以使用owner变量引用父表单,以在该表单上执行所需的操作,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM