调用按钮单击事件方法

发布于 2024-12-07 13:48:49 字数 662 浏览 0 评论 0原文

我有一个表单(AddNewCamper),其中包含一个文本框和一个提交按钮。以不同的形式,我尝试编写:

if (submit button is clicked)
   do stuff

在按钮实际所在的窗口中,我创建了一个单击事件。所以我想我正在尝试在 if 语句中调用该单击事件(与单击事件所在的窗口位于不同的窗口中)。

这是我所拥有的:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情魔剑神 2024-12-14 13:48:49

据我了解您的问题,当提交按钮单击 AddNewCamper 表单时,您似乎想在父表单中显示一些内容。以下是您可以做到这一点的一种方法。

将公共方法添加到 ParentForm,以便在从 AddNewCamper 单击“提交”后显示(或刷新)内容。

在 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...
}

ParentForm 实例传递给构造函数中的 AddNewCamper Form。

private ParentForm _parentForm;

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

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

从 ParentForm 创建一个 AddNewCamper 实例,如下所示。

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

或者,您可以以相同的方式在 ParentForm 中设置一个标志,以标识单击了“提交”按钮。

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.

写下不归期 2024-12-14 13:48:49

转到按钮的事件处理程序(在 Visual Studio 的“表单”视图中,在属性网格中找到单击事件,然后双击它旁边的框,这将带您到它[或者如果没有,则创建它尚未创建])从这里,您需要在用户按下按钮时调用您想要执行的方法。

查看您提供的代码,我假设您想要做的是等待 if 语句,直到用户按下按钮。不幸的是,除非此代码位于单独的线程上,否则如果您等待用户按下按钮,程序将挂起。相反,您必须弄清楚当用户按下按钮时想要发生什么,将其放入方法中并获取按钮事件处理程序来调用该方法。

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.

少钕鈤記 2024-12-14 13:48:49

当您显示“camp”表单时,请像这样传递对父表单的引用:

camp.Show(this);

然后,当有人单击“camp”表单的提交按钮时,您可以使用所有者变量引用父表单来执行操作你想要这样的表格:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文