Skip to content

How to Update a Listbox on Another Form?

AB

Asked Feb 19, 2006, 2:10 PM EST3 answers
Share
LinkedInX
Hello. I have a Listbox, listBoxA (say), on FormA. Now, there are two other Forms, FormB and FormC, with textboxes textBoxB and textBoxC. The items in listBoxA are added to via textBoxB and textBoxC; either textbox can add an item. Can this be done? How can this be done? Here's what I could come up with: Write item from either textbox to a textfile and read the items from the textfile for the listbox. It works but if FormA (with the listbox) is open, I must close it and re-open to see new item(s). Anyone know of a better way, especially where I don't have to re-open the Form with the listbox? This is in C#. Thanks!

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Anwar Buchoo
    0

    Declare a static variable (FormA SingletonFormA;) of the FormA (within FormA) which is accessible to both FormB && FormC.

    Create a method e.g. CreateInstance(); within class FormA which has got something like:

    public static FormA CreateInstance()
    {
       if(SingletonFormA == null) // null check
       {
              SingletonFormA = new FormA(); // initialise it
       }
        
       return SingletonFormA;
    }

    Now create an object of FormA in each of classs FormB && FormC (ObjB, ObjC) and make a function call like:

    // class FormB
    ObjB = FormA.CreateInstance();

    // class FormC
    ObjC = FormA.CreateInstance();

    Then, you can call something like:

    ObjB.ListItem1.Add("testSingletonFromFormB", new ListItem("","")); 

    ObjC.ListItem1.Add("testSingletonFromFormC", new ListItem("","")); 

    The idea here is to use a shared variable amongst your forms.

    0
  • Anwar Buchoo
    0
    All the pleasure is mine :)

    Rgds.
    0
  • 1 short reply
    • 0
      Thanks Anwar, for your help!
      0