Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
C#
public void finishedPrivate()
{
    if (Private_listView.Items.Count > 0)
        MessageBox.Show("I contain Items");
    else
        MessageBox.Show("I DON'T contain Items");
}


The button code: (this button is on the same form as the listview, and works as expected)
C#
private void This_Button_works_Click(object sender, EventArgs e)
{
    finishedPrivate();
}


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)
C#
private void Im_Calling_the_function_Click(object sender, EventArgs e)
{
    Add_Entry x = new Add_Entry();
    x.finishedPrivate();
}


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

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!
 
Share this answer
 
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:
C#
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();
    }
}
 
Share this answer
 
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
 
Share this answer
 
Comments
Espen Harlinn 18-Jul-11 8:36am    
NIce reply, my 5
Sergey Alexandrovich Kryukov 18-Jul-11 11:04am    
Thank you, Espen.
--SA
if it's a child form of the form you are wanting to call the method on you could use

((Add_Entry)this.ParentForm).finishedPrivate();
 
Share this answer
 
Ah cool thanks for the replies guys.
I only started playing around with multiple forms the other day, so it's all a bit new to me. (no new pun intended) :P

Working code now:
Main form:
C#
private void Button_that_opens_the_new_form_Click(object sender, EventArgs e)
{
    upload = new Upload(this);
    upload.Show();
}


Code on new form:
C#
public partial class Upload: Form
    {
        Add_Entry myParent = null;
        public Upload(Add_Entry myParent)
        {
            InitializeComponent();
            this.myParent = myParent;
        }
        private void Im_Calling_the_function_Click(object sender, EventArgs e)
        {
            ((Add_Entry)this.myParent).finishedPrivate();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900