locked
listview cannot find items when method was called from another class!??? RRS feed

  • Question

  • Hi all,

    I have a very weird problem for me (that may come easy and known issue to you)

    I have a void method in my form1 code side. it's working without problem. Here is the code : 

            public void revertPainters(short tip)
            {
                string statusDatex = "";
                foreach (ListViewItem li in this.lvpa.Items )
                {
                     anycode...
                }
            }
    

    PS : there are existing items in lvpa listview

    3rd line (this.lvpa.items part) shows some items when i directly call this method inside form1, but it finds no items (itemscount = 0) when i call  this method from another form's (Form2) class.

    Could you help me to find the answer???

     

    Thanks

    Tuesday, January 31, 2012 12:39 PM

Answers

  • In your current code you declared a nf local variable inside t_Tick. Instead you could add a public nf property to Form2 (public Form1 nf {get;set;}).

    Then when Form2 is created you set its nf property to the instance of Form1 that creates Form2. Then from within Form2 when you use nf you refer to this property that is the instance of Form1 that created the form (I used nf because then you have just to suppress the "Form1 nf=new Form1();" line and the existing nf.revertPainters(2); line will now refer to this new property, thinking it would made clear that this is a replacement for your current nf local variable inside t_Tick, feel free to rename it as needed).

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Edited by Patrice ScribeMVP Wednesday, February 1, 2012 7:23 PM
    • Marked as answer by mcccbup Wednesday, February 1, 2012 7:29 PM
    Wednesday, February 1, 2012 7:21 PM

All replies

  • Hi,

    The simplest explanation would be that you don't have any items inside your listview. Are you 100% you are using the same instance of Form2 ? Perhaps you are using another fresh instance for which the listview has not been populated ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Tuesday, January 31, 2012 12:45 PM
  • How and when do you retrieve the data? Immediately after the first form is closed? Is the form1 modal?

    Is it possible that the list is empty since you have not yet initialized the form? Please post the source code from the declaration of local variables for form1 and form2 until the line where the error ocurrs.


    Toni Petrina
    My blog: Programming ramblings
    Tuesday, January 31, 2012 12:45 PM
  • here is the codeblocks in Form2:

     

     // timer (named t) tick event
    
            private void t_Tick(object sender, EventArgs e)
            {
                if (cnt > 0)
                {
                    cnt--;                
                }
                else
                {
                    t.Stop();
                    t.Enabled = false;
                    Form1 nf = new Form1();
                    nf.revertPainters(2);
                    trueExit();
                }
            }
    

     


    and here is the main form (Form1) that includes the revertPainters(..) method

    public void revertPainters(short tip)
            {
                string statusDatex = "";
                foreach (ListViewItem li in this.lvpa.Items )
                {
                     //there are some codes were hit if lvpa has any items
                }
            }
    

    and here is Form2 load event :

            private void Form2_Load(object sender, EventArgs e)
            {
                t.Interval = 1000;
                t.Enabled = true;
                t.Start();       
            }
    


    and here is the time line :

    1 - Form1 loads and lvpa (Listview in Form1) is populated

    2 - When clients doesnt click any item for 1 minute, Form 2 is created from Form1 with the following code :

                    if (lvpa.Items.Count > 0)
                    {
                        Form2 uf = new Form2();
                        uf.Show();                    
                        uf.Controls["myID"].Text = lvpa.Items[0].Name;
                    }
    


    3 - When client does not enter the correct code into the textbox, a method (named as revertPainters which is in Form1) runs 

    First code block at the top.

    I also means, there are absolutely items in listview in Form1 but the same method cannot see them if it is called from another form.

     

     

    Tuesday, January 31, 2012 3:54 PM
  • When you write

    Form1 nf = new Form1();

    As the keyword suggests, you are creating a new Form1, you're not using the one from which you created the Form2. You didn't show the code where lvpa is populated, so I can't be sure, but it's probably in a part of the code which is never executed for this new Form1.

    Tuesday, January 31, 2012 4:05 PM
  • lvpa is being populated by a method which is called by a timer in every 1 minute.

    when Form1 starts and user logs in, timer is enabled and started. In every 1 minute following method populates the lvpa listview :

    // when user logs in :
            private void button1_Click(object sender, EventArgs e)
            { 
                 generalCounter.Interval = 10000;
                 generalCounter.Enabled = true;
                 generalCounter.Start();
             }
    
    
    //and general counter timer works as following :
    
    private void generalCounter_Tick(object sender, EventArgs e)
            {
                checkReverteds();        
                checkRevertedOnes();
                datashow.Text = "Last Up:"+DateTime.Now.ToString();
             }
    
    // and lvpa is being populated
    private void checkReverteds()
            {
                lvpa.Items.Clear();
                revertedOnes.Items.Clear();
                DataSet acl = new DataSet();
                acl = connectip(..............);
                ....
                ......
                }
            
    //all the code above is in Form1
    
    


    when i check if any items in lvpa inside Form1, i can see the items, but it cannot find anything once the same code called from Form2. As you told , i create a new Form1 from Form2 and that's why i cannot see any items. On the other hand, there must be a way to find and reach existing Form1 without creating a new instance. This will be  the answer if we can find :)

    Thanks

    Wednesday, February 1, 2012 9:48 AM
  • Hi,

    So with your current design you have to wait one minute before the listview being populated. Meanwhile Form2 won't retrieve anything as it was not yet populated.

    For example you could pass to Form2 the instance it should work with. For example your code is currently :

     Form2 uf = new Form2();
    uf.Show();                    
    uf.Controls["myID"].Text = lvpa.Items[0].Name;

    It could be :

     Form2 uf = new Form2();
    uf.nf=this; // Pass Form1 instance to Form2
    uf.Show();                    
    uf.Controls["myID"].Text = lvpa.Items[0].Name;

    Of course you'll have to add a "nf" property to the Form2 form and you'll have to work with this property in your Form2 code.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Proposed as answer by Louis.fr Wednesday, February 1, 2012 2:12 PM
    Wednesday, February 1, 2012 10:33 AM
  • Hi Patrice,

    I could not understand what "nf" property is. Also visual studio could not locate or find any property nf ?! Could you please clarify about the nf property???

    Wednesday, February 1, 2012 3:31 PM
  • In your current code you declared a nf local variable inside t_Tick. Instead you could add a public nf property to Form2 (public Form1 nf {get;set;}).

    Then when Form2 is created you set its nf property to the instance of Form1 that creates Form2. Then from within Form2 when you use nf you refer to this property that is the instance of Form1 that created the form (I used nf because then you have just to suppress the "Form1 nf=new Form1();" line and the existing nf.revertPainters(2); line will now refer to this new property, thinking it would made clear that this is a replacement for your current nf local variable inside t_Tick, feel free to rename it as needed).

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Edited by Patrice ScribeMVP Wednesday, February 1, 2012 7:23 PM
    • Marked as answer by mcccbup Wednesday, February 1, 2012 7:29 PM
    Wednesday, February 1, 2012 7:21 PM
  • This's really a great answer Patrice! Thanks a lot...
    Wednesday, February 1, 2012 9:48 PM