How to Update a Listbox on Another Form?
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!
Anwar BuchooPosted Feb 22, 2006, 3:13 AM
Rgds.
ABPosted Feb 22, 2006, 1:35 AM
Anwar BuchooPosted Feb 21, 2006, 4:55 AM
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.