调用按钮单击事件方法
我有一个表单(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解您的问题,当提交按钮单击 AddNewCamper 表单时,您似乎想在父表单中显示一些内容。以下是您可以做到这一点的一种方法。
将公共方法添加到 ParentForm,以便在从 AddNewCamper 单击“提交”后显示(或刷新)内容。
在 ParentForm 中将
ParentForm 实例传递给构造函数中的 AddNewCamper Form。
从 ParentForm 创建一个 AddNewCamper 实例,如下所示。
或者,您可以以相同的方式在 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
Pass the ParentForm instance to the AddNewCamper Form in the contructor.
Create an AddNewCamper instance as below from the ParentForm.
Or you can set a flag in the ParentForm in the same way to identify that the Submit button is clicked.
转到按钮的事件处理程序(在 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.
当您显示“camp”表单时,请像这样传递对父表单的引用:
然后,当有人单击“camp”表单的提交按钮时,您可以使用所有者变量引用父表单来执行操作你想要这样的表格:
When you have the "camp" form show, pass a reference to the parent form like 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: