[英]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.