从子窗体在主窗体上调用函数 [英] Calling function on main form from child form

查看:89
本文介绍了从子窗体在主窗体上调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我有一个小问题,只是无法理解.
我有一个已添加项目的列表视图.当我按下按钮时,它会调用一个函数来对项目进行计数.
但是,如果我从另一种形式调用该函数,它将始终返回0个项目
函数:(当从当前表单上的按钮调用时,此函数按预期工作)

Hi all.
I''ve got a little problem I just can''t get my head around.
I have a listview that I''ve added items to. When I press a button it calls a function to count the items.
BUT, if I call the function from another form, it always returns 0 items
The function: (this works as expected, when called from the button on the current form)

<pre lang="cs">public void finishedPrivate()<br />
{<br />
    if (Private_listView.Items.Count > 0)<br />
        MessageBox.Show("I contain Items");<br />
    else<br />
        MessageBox.Show("I DON''T contain Items");<br />
}</pre><br />



按钮代码:(此按钮与列表视图具有相同的形式,并且可以正常工作)



The button code: (this button is on the same form as the listview, and works as expected)

<pre lang="cs">private void This_Button_works_Click(object sender, EventArgs e)<br />
{<br />
    finishedPrivate();<br />
}</pre><br />



子窗体上的按钮代码:(此按钮从新窗体中调用该函数就可以了,但项目计数始终为0)



The button code on the child form: (this button calls the function just fine from the new form but the item count is always 0)

<pre lang="cs">private void Im_Calling_the_function_Click(object sender, EventArgs e)<br />
{<br />
    Add_Entry x = new Add_Entry();<br />
    x.finishedPrivate();<br />
}</pre><br />





Can anybody see why it''s not working??

推荐答案

是的,线索在new关键字中.
当您说
Yes, the clue is in the new keyword.
When you say
Add_Entry x = new Add_Entry();

时,您是在创建表单的新实例,而不是访问现有实例.
就像您将手机放在汽车的杂物箱中一样.如果您随后购买了一辆新车,是否希望在该车的杂物箱中找到该手机?您需要访问父表单实例,而不是创建一个新的实例.或者最好在创建子代时将数字从父代传递给子代,以使子代不需要了解有关其父代的任何信息!

you are creating a new instance of the form, rather than accessing the existing instance.
It''s as if you put your phone in the glove box of your car. If you then bought a new car, would you expect to find the phone in the glove box of that? You need to access the parent form instance rather than create a new one. Or preferably, pass the number from the parent to the child when the child is created, so that the child doesn''t need to know anything about it''s parent!


它"的原因是您正在创建表单的新实例,而不是引用现有实例.新实例中的listview不包含任何项目,因此它返回零.要从另一个表单访问现有实例,您需要在另一个表单中保存对表单的引用.像这样的东西:
It''s because your are creating a new instance of the form instead of referencing the already existing instance. The listview in the new instance does not contain any items and so it returns zero. To access the existing instance from another form, you need to save a reference to your form within the other form. Something like this:
class MyOtherForm {
    private MyListViewForm _form;
    
    void OpenListViewForm() {
        _form = new MyListViewForm();
        _form.Show();
    }
    
    private void Im_Calling_the_function_Click(object sender, EventArgs e)
    {
        _form.finishedPrivate();
    }
}


首先:形式上没有(可操作的)子母关系(我不认为我不建议使用MDI,曾经).我强烈建议使用所有者和自有表格.另外,我建议务必使用一种形式.

现在,它看起来像是表单协作中的一个流行问题.最健壮的方法是在表单类中实现适当的接口.

请针对以下问题查看我的解决方案:如何以两种形式在列表框之间复制所有项目 [
First and foremost: there is not (operational) child-parent relationship in forms (I don''t consider MDI which I don''t recommend to use, ever). There are owner and owned forms which I highly recommend to use. Also, I recommend to use only one form by all means.

Now, it looks like a popular problem of form collaboration. Most robust way is implementing appropriate interface in the form class.

Please see my solution for the following question: How to copy all the items between listboxes in two forms[^]; see also other suggestions and all the discussion.

—SA


这篇关于从子窗体在主窗体上调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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