0

I'm not an programmer but i tried it to simplify my actual work. I have a code like below :

public partial class Mainform : Form
{
    public List<string> List1 = new List<string>{"nom1","nom2"};
    void Method1()
    {
        //Get data of items from List1
    }
}

public partial class Setting : Form
{
    // Here, i want to modify the List1 by adding the new item
    Mainform ListToModif = new Mainform();
    void MethodToModify()
    {
        //Modify List1
    }
}

Now, i want to return to Mainform class with the List1 which is modified in Setting class to take data.

Can you tell me how i should to do ?

MDN
  • 9
  • 2
  • 1
    Are you going to open `Setting` form from `Mainform`? If it is so then just create constructor for `Setting` form accepting `Mainform` instance and then do what you need to do with that instance. – Samvel Petrosov Apr 27 '18 at 14:00
  • Thanks for your attention, Perhaps i wasn't explain clearly. 1. In class Mainform I have 2 buttons, the first is Setting button (which link to class Setting and open another windown form) After this setting, i will close this windown form (Here i have a list modified by class Setting). Then the second button in Mainform is "GetData" which take all of data of items in List1. But in this step, the List1 hasn't modified !! And i don't understand =.= – MDN Apr 27 '18 at 14:16
  • In your `Setting` form you're creating **a new instance** of `Mainform` in which the list is empty. The are many existing questions on how to access data from another form - [this one](https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form/22372271) for example. – stuartd Apr 27 '18 at 14:26
  • Thanks for your help, it's helpful for me :) – MDN Apr 27 '18 at 15:11

1 Answers1

0

In your Setting button OnClick event in your MainForm you need to pass the List1 to the Setting class:

/* declare Setting in this scope if you are going to use it multiple times */
Setting _Setting;
private void yourButtonName_OnClick(object sender, EventArgs e)
{
    /* null and disposed checking to create a new instance when necessary */ 
    if (_Setting == null || _Setting.IsDisposed)
    {
        /* passing this form list to the Setting form */
        _Setting = new _Setting(this.List1);
    }
    /* calling the form through ShowDialog will make your MainForm to
    wait until Setting form is closed */
    _Setting.ShowDialog();
    /* setting the list value */
    this.List1 = _Setting.List;
    _Setting.Dispose();
}

And you need to modify your Setting form like this

public partial class Setting : Form
{
    /* auto property */
    public List<string> List { get; internal set; }
    public Setting(List<string> listToModify)
    {
        this.List = listToModify;
    }

    private void ModifyList()
    {
        /* here, you modify this.List because you assigned MainForm.List1
        to this.List in the code above */ 
    }
}

This way, when you close Setting your MainForm.List1 is now modified and MainForm.GetData() should retreive the modified list

  • Perfect, that's all i need :) I was wrong when i tried _Setting.Show() instead of _Setting.ShowDialog(); I changed my code and it work perfectly :) Thank you so much ! – MDN Apr 27 '18 at 15:43
  • @MDN I'm glad I could help, please consider accepting the answer :) – Ivan García Topete Apr 27 '18 at 15:50