简体   繁体   English

从其他形式调用按钮

[英]Calling the Button from other form

Good day to all. 祝大家有美好的一天。 i am facing another problem. 我正面临另一个问题。 i have 2 forms, 1 form is the main form where the listview(show my data from database), refresh button(refreshing listview) and add new item button ( where to add new item). 我有2种形式,其中1种是主要形式,其中listview(显示数据库中的数据),刷新按钮(刷新listview)和添加新项目按钮(在其中添加新项目)。 when i pressed add new item, another form showing. 当我按下添加新项目时,显示另一种形式。 so my problem is how can i refresh my listview when im done adding new item using the btn_Refresh_Click(object sender, EventArgs e) or how can i access the btn_Refresh_Click 所以我的问题是当我完成使用btn_Refresh_Click(object sender,EventArgs e)添加新项目时如何刷新列表视图,或者如何访问btn_Refresh_Click

private void btn_Refresh_Click(object sender, EventArgs e)
    {
        ShowItems("B.S. in Information Technology", 1, 18);
        ShowItems("B.S. in Entrepreneurial Management", 19, 36);
        ShowItems("B.S. in Industrial Engineering", 37, 54);
        ShowItems("B.S. in Electronics Engineering", 55, 72);
        ShowItems("Technical-Vocational Courses", 73, 90);
    }

ShowItems is a method for sql select statement to show the items to listview. ShowItems是sql select语句将项目显示到listview的方法。 thank you in advance. 先感谢您。

Create a public member function for refreshing: 创建一个公共成员函数以刷新:

public void Refresh()
{
    ShowItems("B.S. in Information Technology", 1, 18);
    ShowItems("B.S. in Entrepreneurial Management", 19, 36);
    ShowItems("B.S. in Industrial Engineering", 37, 54);
    ShowItems("B.S. in Electronics Engineering", 55, 72);
    ShowItems("Technical-Vocational Courses", 73, 90);
}

And call it from your button handler: 并从按钮处理程序中调用它:

private void btn_Refresh_Click(object sender, EventArgs e)
{
    Refresh();
}

And now you can also call it from anywhere else! 现在,您还可以从其他任何地方调用它!

You can either do something like this: 您可以执行以下操作:

MyAddForm fmAdd = new MyAddForm();
fmAdd.ShowDialog();
btn_Refresh_Click(null,null);

OR

MyAddForm fmAdd = new MyAddForm();
fmAdd.ShowDialog();
RefreshData()

Take your logic out of the refresh button click and add it to a new method called RefreshData and make sure the button click calls RefreshData 从刷新按钮单击中删除逻辑,然后将其添加到名为RefreshData的新方法中,并确保按钮单击调用RefreshData

If you dont want to create a new method change the access of btn_Refresh_Click event to public and call that using 如果您不想创建新方法,请将btn_Refresh_Click事件的访问权限更改为public,然后使用

btn_Refresh_Click(null,null);

and when you call this from other form use the object of form2 and call this event 当您从其他表单调用此函数时,请使用form2的对象并调用此事件

Let f2 is the object of Form2 令f2为Form2的对象

f2.btn_Refresh_Click(null,null);

If you want to be more good programmer try to use delegates 如果您想成为更好的程序员,请尝试使用委托

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

相关问题 在其他框架中触发按钮时从框架中调用方法 - Calling method from a frame when a button is triggered in other frame 从其他表单文本框调用功能不变 - Calling function from other form textbox doesn't change 单击登录按钮从一个表单重定向到另一个表单 - Redirecting from one form to other form on clicking login button 从form1单击按钮时从form2调用按钮 - Calling a button from form2 when clicking a button from form1 从另一个表单调用一个表单上的方法,如果单击Form2上的一个按钮,则在Form1上填充组合框 - calling method on one form from another form, fill combobox on Form1 if a button on Form2 is clicked mdi形式->>子形式1(button->>子形式2)c#来自其他某个子形式的按钮的子形式 - mdi form-->> child form 1(button-->> child form2) c # child form from button of some other child form 在按钮事件中推广表单调用 - Generalizing the form calling in a button event 从其他表单上的按钮单击事件处理程序刷新控件或重新加载表单 - Refresh the control or reload form from button click event handler on other form MVC Razor 表单按钮未调用指定的控制器 - MVC Razor form button not calling controller specified 从表单应用程序调用控制台应用程序的“Main”方法(通过按钮单击事件) - Calling `Main` method of a console application from a form application (by a button click event)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM