如何从表单1调用函数并从表单2调用它? [英] How do I call a function from form 1 and call it from form 2?

查看:79
本文介绍了如何从表单1调用函数并从表单2调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我在父表单中有这个函数,可以从数据库表中检索数据。现在,我的第二个表单用作添加表单。我希望当我按下第二个表单的添加按钮时,它会调用该函数或执行一个代码,该代码将刷新/重新检索数据到表单1的列表视图中。



Basically, I have this functions in my parent form that retrieves data from a database table. Now, my second form functions as an add form. I want that when I press my second form's add button, it will call the function or executes a code that will refresh/re-retrieve the data into the form 1's listview.

private void loadtbl()
        {
            
                supplier_tb.Items.Clear();
                cn.Open();
                cmd.CommandText = "select * from SupplierTable";
                cmd.Connection = cn;
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    ListViewItem item = new ListViewItem(dr["Supp_ID"].ToString());
                    item.SubItems.Add(dr["Supp_Name"].ToString());
                    item.SubItems.Add(dr["Supp_Address"].ToString());
                    item.SubItems.Add(dr["Supp_CPerson"].ToString());
                    item.SubItems.Add(dr["Supp_TelNo"].ToString());
                    item.SubItems.Add(dr["Supp_FaxNo"].ToString());
                    item.SubItems.Add(dr["Supp_Email"].ToString());
                    item.SubItems.Add(dr["Supp_TIN"].ToString());
                    item.SubItems.Add(dr["Supp_Status"].ToString());
                    item.SubItems.Add(dr["Supp_Term"].ToString());
                    item.SubItems.Add(dr["Supp_Category"].ToString());
                    supplier_tb.Items.Add(item);

                }
                cn.Close();

   }





我的尝试:



到目前为止还没有尝试过任何东西,因为我对如何处理它感到困惑。



What I have tried:

Have not tried anything so far, 'cause I'm confused as to how to approach it.

推荐答案

很简单到do:只使用一个事件:在两个表单之间传递信息,第2部分:Child to家长 [ ^ ]
Pretty simple to do: just use an Event: Transferring information between two forms, Part 2: Child to Parent[^]


作为 Eric Lynch [ ^ ]评论问题,你必须分开数据访问层 [ ^ ]和业务逻辑层 [ ^ ]分为不同的类。这是基础。
As Eric Lynch[^] mentioned in the comment to the question, you have to separate Data Access Layer[^] and Business Logic Layer[^] into different classes. It's basics.


这篇关于如何从表单1调用函数并从表单2调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆