Justin Day
Justin Day

Reputation: 119

Homework how to pass values from one form to an object in a list in another form

I have the following snippet of code that allows me to pull the properties from an object in my list and assign them to variables in other forms. However, I need to be able to pull the data from my variables in the other form and use those to set the properties of the given object.

My class Account is used to populate my list accounts. On my next form AccountMenu I have a class Variables1 that contains accessible variables that are used throughout the rest of my forms to keep track of the checking balance and saving balance. When logging off from the AccountMenu, I want to be able to pass the values from Variables1 to the account that was initially used.

I know how to pass variables from one form to another, but I'm not really sure how to update the form automatically, without a button, on the original form. Thus, the solution that I see is that I have a button on my AccountMenu form that "logs" the user out, via this.close(); Additionally, I guessed that under that button, I need to have some code that assigns the variables as properties to the object. I'm just not sure how I can access the set properties of the object, since it is dynamically called with the code below.

Can someone help me figure out what I need to do? Below is some of the relevant code so that you can see how I have things set up. I am just not sure how to access "matches" from the other form in order to update that specific object properties. Thank you, anyone, who can help!

    //variable that will be used to check textbox1.Text
    string stringToCheck;
    //array of class Account
    List<Account> accounts = new List<Account>();
    public MainMenu()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //set value to user's input
        stringToCheck = textBox1.Text;
        //set a var that only returns a value if the .Name already exists
        var matches = accounts.FirstOrDefault(p => p.Name == stringToCheck);
        //check through each element of the array
        if (matches == null)
        {
            accounts.Add(new Account(stringToCheck));
            textBox1.Text = "";
            label3.Visible = true;
        }
        else if (matches != null)
        {


            //set variables in another form. not sure if these are working
            Variables1.selectedAccount = matches.Name;
            //is this calling the CheckBalance of the instance?
            Variables1.selectedCheckBalance = matches.CheckBalance;
            //same thing?
            Variables1.selectedSaveBalance = matches.SaveBalance;

            //switch to form
            AccountMenu acctMenu = new AccountMenu();
            this.Hide();
            acctMenu.Show();
        }
    }

Upvotes: 0

Views: 185

Answers (1)

jadavparesh06
jadavparesh06

Reputation: 946

As per my understanding I think what you required is kind of trigger on your parent form that needs to be called from your child application.

If that is what you required than you can go with defining an event on your AccountMenu form. and register this event from your Accounts form.

Than simply raise this event from your AccountMenu subform.

Deletegates and Events are really works like magic :)

Let me show you some code how to do this.

Code required in AccountMenu window:

 public delegate void PassDataToAccounts(string result);
 public event PassDataToAccounts OnPassDataToAccount;

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
  if (OnPassDataToAccount != null)
      OnPassDataToAccount("result");
  base.OnClosing(e);
}

Code required in Accounts window button1_Click event where the AccountMenu will open:

//set variables in another form. not sure if these are working
Variables1.selectedAccount = matches.Name;
//is this calling the CheckBalance of the instance?
Variables1.selectedCheckBalance = matches.CheckBalance;
//same thing?
Variables1.selectedSaveBalance = matches.SaveBalance;

//switch to form
AccountMenu acctMenu = new AccountMenu();
acctMenu..OnPassDataToAccount += childwindow_OnPassDataToAccount;
this.Hide();
acctMenu.Show();
}
void childwindow_OnPassDataToAccount(string result)
{
  if (result == "result")
  {
  // Processing required on your parent window can be caried out here
  //Variables1 can be processed directly here.
  }
}

Upvotes: 1

Related Questions