savage305
savage305

Reputation: 45

refreshing treeview component from other form

I currently have a class that handles my treeview and other winForm components.

I want to use another form which act as my input and once I press the save button it should update my treeview component on the other form. So far what I tried has not worked.

here is my code:

*mainDisplay is my form which includes my component and stores my variable that holds the data

Here I load my date into the treeview

    public void mainDisplay_Load( TreeNode input)
    {
        treeView1.BeginUpdate();
        foreach (data x in mydata1)
        {
            Console.WriteLine(x.getName());
            if (x.getName() != null)
            {
                treeView1.Nodes.Add(input);
            }

        }
        treeView1.Refresh();

    }

here is my button action on the OTHER form:

    private void button1_Click(object sender, EventArgs e)
    {
        if (!(String.IsNullOrEmpty(getnamebox.Text))) ;
        {
            mainDisplay putdata = new mainDisplay();

            name = getnamebox.Text;
            pass = getpassbox.Text;
            url = geturlbox.Text;
            notes = getnotebox.Text;

            data newData = new data(name, pass, notes);
            mainDisplay.mydata1.Add(newData);

            TreeNode mytree = new TreeNode(name);
            putdata.mainDisplay_Load(mytree);

            this.Hide();

        }

Any tip would be appreciated.

Upvotes: 0

Views: 1185

Answers (1)

ZhiHeather
ZhiHeather

Reputation: 162

You just created a brand new main display form somewhere (in memory) and added a tree node to it.

You need to pass the reference of your main display forward (usually in an initialize function or trace back your second form's parentage depending on how your stuff was set up) and then use the reference to your actual main form to update the tree.

Upvotes: 1

Related Questions