Abei Villafane
Abei Villafane

Reputation: 83

C# Use instance of a class made in Form 1 in Form 2

I want to Use an instance of a class made in Form 1 in Form 2 (i changed it to a list for simplicity of example code:

Not only that, I want Form 2 to be able to modify it (Clear it at some point). The advice I got was this, although I was not told how due to "no spoonfeeding allowed"

namespace automationControls.FileTime
{
    public class Form_Main : Form
    {
        public List<string> folderList; //<---- i want to access this.....

        private void button_showForm2_Click(object sender, EventArgs e)
        {
            Form_Log ConfirmBoxForm = new Form_Log(this);
            ConfirmBoxForm.Show();
        }
}

//form_Main opens form_Log


namespace automationControls.FileTime
{
    public partial class Form_Log : Form
    {
        public Form_Log(Form_Main _f1)
        {
           InitializeComponent();
        }

        private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
        {
            How.Do.I.AccessForm_Main.folderList.Clear();//<---- ............. in this function
        }
     }
}

Answered:In the constructor of Form_Log, store the reference to _f1 somewhere you can access it from elsewhere in Form_Log

Upvotes: 0

Views: 161

Answers (4)

Jaume
Jaume

Reputation: 763

I don't know how advanced is your project but in this situation i would use delegates. Here is how i would do it:

public delegate void ModifyCollectionHandler(string parameter);
public delegate void ClearCollectionHandler();
public partial class Form1 : Form
{
    public List<string> folderList;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form = new Form2()
        form.ClearItem+=form_ClearItem;
        form.AddItem+=form_AddItem;
        form.DeleteItem+=form_DeleteItem;
    }

    void form_DeleteItem(string parameter)
    {
        if (folderList == null)
            return;
        folderList.Remove(parameter);
    }

    void form_AddItem(string parameter)
    {
        if (folderList == null)
            folderList = new List<string>();
        folderList.Add(parameter);
    }

    void form_ClearItem()
    {
        if (folderList != null)
            folderList.Clear();
    }
}
public partial class Form2 : Form
{
    public event ModifyCollectionHandler AddItem;
    public event ModifyCollectionHandler DeleteItem;
    public event ClearCollectionHandler ClearItem;
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (ClearItem != null)
            ClearItem();
    }
}

I hope I helped you :) Best regards

Upvotes: 1

iJay
iJay

Reputation: 4293

Try This,

public class Form_Main : Form
{
    public List<string> folderList; //<---- i want to access this.....

    private void button_showForm2_Click(object sender, EventArgs e)
    {
        Form_Log ConfirmBoxForm = new Form_Log(this);
        ConfirmBoxForm.Show();
    }
}

Form log :

public partial class Form_Log : Form
{
    private Form_Main _mainForm;
    public Form_Log(Form_Main _f1)
    {
       InitializeComponent();
       _mainForm = _f1;
    }

    private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
    {
      _mainForm.folderList.Clear();
    }
 }

Upvotes: 1

Youness
Youness

Reputation: 1495

in the Form1 put this :

public static List<string> folderList;

you can simply call it from any form ex:Form2 like this :

From1.folderList.Clear(); 

Upvotes: -2

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101731

Why don't you use the constructor that you have already added your form?

private Form_Main _mainForm;
public Form_Log(Form_Main _f1)
{
    InitializeComponent();
    _mainForm = _f1;
}

private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
    var myList = _mainForm.folderList;
}

Upvotes: 2

Related Questions