Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class named Documents
I have a collection named class StoreObjects:List<Documents>

I am entering data into the collection from a Form1

"this is part of the code in Form1"

StoreObjects so = new StoreObjects();

d = new Documents(txtName.Text, txtSurname.Text, txtTitle.Text);
so.Add(d);

so far I managed to enter data and even display it.

What I want now is from Form2, I want to be able to search by title, if I do it from Form1 this works but from Form2 does not work

C#
so.SearchByTitle(txtTitle.Text);
            Console.WriteLine(so.foundTitle);
            if (so.foundTitle == false)
            {
                Console.WriteLine("Not Found");
            }



this is what i have in StoredObject

C#
public bool SearchByTitle(string aTitle)
        {
            foreach (Documents d in this)
            {
                if ((d != null) && (d.Title == aTitle))
                {
                    foundTitle = true;
                    Console.WriteLine(d.GetData());
                }
            }
            return foundTitle;
        }



I think that it is because of this declaration in Form 2
StoreObjects so = new StoreObjects();

how can I manage to get the results I want
Posted
Updated 11-Jan-11 7:38am
v3

You should probably make a static globals class that contains data that you want to make accessible from any point in your program.

-OR -

If form2 is created by form1, you can simply pass form1 as a parameter in form2's constructor, and you can accessso (as long as it's public, of course) from form 2.

 
Share this answer
 
v3
Comments
datt265 11-Jan-11 13:36pm    
I have tried making static, and getting errors. I am thinking that since I have to declare StoreObjects so = new StoreObjects(); again in Form2 I am created a new instance and thus I get no results. But I dont know how to make it in another way
datt265 11-Jan-11 13:42pm    
Form2 is launched from Form1 when clicking a button. can you help me out on how to pass a parameter please?
Example of a static class:

C#
pubic static class Globals
{
    public static StoredObjects StoredObjects { get; set; }

    private Globals()
    {
        StoredObjects = new StoredObjects();
    }
}


You call it like so:

C#
if (Globals.StoredObjects != null)
{
   ...
}


 
Share this answer
 
Comments
fjdiewornncalwe 11-Jan-11 14:28pm    
As much as I hate globals.... If it needs to be done, this is how to do it.
this is an example to pass a listview and a combobox control;
on your click event;
{
   Form2 form2 = new Form2(lvTest1, cmbTest2);
   form2.ShowDialog(this);
}
under the form2.cs
public partial class Form2 : Form
    {
        ListView lv = new ListView();
        ComboBox cb = new ComboBox();
        public Form2(ListView lvParents, ComboBox cbParent)
        {
            InitializeComponent();
            lv = lvParents;
            cb = cbParent;
        }
     }


so you can modify your code, based on given example.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 11-Jan-11 14:27pm    
Never, ever pass the UI controls from one form to the other. What should be passed is the data that the control is displaying. UI should only be for displaying/collection of information, but not for storing information.
Orcun Iyigun 11-Jan-11 16:51pm    
Thanks for the warning. I will keep that in mind.
What I have done know is passing the value of 'so' from Form1 as a ref to form2 and is working. Thanks to all for the hints and the quick reply.
 
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